fix: import.meta.env.BASE_URL will be '/' in client loaded component on dev mode#4886
Conversation
🦋 Changeset detectedLatest commit: e227d9f The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| include: rendererClientEntries, | ||
| }, | ||
| define: { | ||
| 'import.meta.env.BASE_URL': settings.config.base ? `'${settings.config.base}'` : 'undefined', |
There was a problem hiding this comment.
Would it be the same as the following?
settings.config.base || 'undefined'
There was a problem hiding this comment.
If base is not set, would it not show /undefined/ instead of / in that case here? Since you added undefined in strings?
There was a problem hiding this comment.
Refer to Vite Define docs: https://vitejs.dev/config/shared-options.html#define
and https://github.com/withastro/astro/blob/main/packages/astro/src/core/create-vite.ts#L96
We do need to use strings.
There was a problem hiding this comment.
If base is not set, it would show / in that case here.
What's more, settings.config.base and '${settings.config.base}' are different, notice the single quotes
There was a problem hiding this comment.
@yuhang-dong yup, now I see it. Thanks for the accurate pointers!
|
I don't think I'm eligible to post a 'approved' |
Changes
Hi, I have found that
import.meta.env.BASE_URLwill be set/in client loaded component on dev mode.And after searching from issues, I found #3567 describe the same situation.
In the conversion, I have found pr#3955 just fix prod mode.
You can visit the below link to see

import.meta.env.BASE_URLwill be set '/' in client loaded component on dev mode:https://stackblitz.com/edit/github-vhnc6s-13dn4n?file=src%2Fpages%2Findex.astro
So, I defined the
import.meta.env.BASE_URLtosetting.config.base.Testing
This situation need e2e test, so I added it on
[packages/astro/e2e/astro-envs.test.js]Docs
N/A