Skip to content
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

Open
delucis opened this issue Apr 6, 2024 · 1 comment
Open

Imports guide recommends a buggy use of import.meta.url #7785

delucis opened this issue Apr 6, 2024 · 1 comment
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@delucis
Copy link
Member

delucis commented Apr 6, 2024

๐Ÿ“š 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:

const url = new URL('../../package.json', import.meta.url);

However, import.meta.urlโ€™s value varies between dev and build, for example during dev, a page at src/pages/dir/index.astro has an import.meta.url of src/pages/dir/index.astro. However, during a build this might be something like dist/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 like package.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.

@delucis delucis added the improve documentation Enhance existing documentation (e.g. add an example, improve description) label Apr 6, 2024
@sarah11918 sarah11918 added the help wanted Issues looking for someone to run with them! label May 4, 2024
@sarah11918
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

No branches or pull requests

2 participants