-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imports guide recommends a buggy use of import.meta.url
#7785
Comments
Do we know what the proper guidance here is? Or just that what we have might set people up for problems? Would love some tech guidance on this one! |
Pinging for freshness, in case anyone wants to pick this up! |
So, what we are recommending is what Vite recommends for client-side code. For SSR Vite has the following warning at the very bottom of this page: Warning Does not work with SSR This pattern does not work if you are using Vite for Server-Side Rendering, because Recommending something here is tricky because the appropriate solution will depend on the intended use case.
|
Related to #8417 |
Something more like:
and scrap the example, or add an example that is something that really would predictably and reliably work well? |
STILL HELP WANTED On Talking and Doc'ing we decided to update the example.
|
We could replace an example with something pure. The text would be "If you want to parse a Media Type string, for example, you can do so like this:" ---
// Example: import the "util" builtin from Node.js
import util from 'node:util';
export interface Props {
mimeType: string,
}
const mime = new util.MIMEType(Astro.props.mime)
---
<span>Type: {mime.type}</span>
<span>SubType: {mime.subtype}</span> |
Pinging for freshness and still happy to have contributions here! |
📚 Subject area/topic
Imports
📋 Page(s) affected (or suggested, for new content)
https://docs.astro.build/en/guides/imports/#node-builtins
📋 Description of content that is out-of-date or incorrect
The “Node Builtins” section in the Imports guide is three years old and probably misleading.
It suggests building a relative path to another file like this:
However,
import.meta.url
’s value varies between dev and build, for example during dev, a page atsrc/pages/dir/index.astro
has animport.meta.url
ofsrc/pages/dir/index.astro
. However, during a build this might be something likedist/chunks/prerender_CX4g21Zw.mjs
instead.In simple cases this technique can appear to work. For example, in
src/pages/index.astro
the folder depth is usually the same between dev and build and if you’re loading something at the root level likepackage.json
, you will end up in the right place.But in pages or components in subdirectories, or when trying to reference siblings in the file structure, you’re very likely to hit inconsistencies between dev and build (not to mention SSR uses of
node:fs
where you might even be referencing a file outside your project which is missing completely when deployed).🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)
https://stackblitz.com/edit/github-9wjfwu?file=src%2Fpages%2Findex.astro
This example shows the documented technique apparently working in dev mode.
If you quit the dev server (Ctrl + C) and preview the build instead by running
npm run build && npm run preview
, the values in the sub directory page will be off.The text was updated successfully, but these errors were encountered: