-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix(create-vite): fix broken vite logo when base changed #12374
fix(create-vite): fix broken vite logo when base changed #12374
Conversation
After using create-vite to generate a React project, in a production build the Vite logo will break when setting the base URL to anything except the default `/`. This occurs because the `img` `src` directly refers to the generated `/vite.svg` and React does not preprocess `src` attributes to add the configured `base`. As suggested in vitejs#7358 (found via vitejs#10601), the solution is to import `vite.svg` in the same way as `react.svg` already is. This MR implements that solution in to the templates to prevent confusion in the future. Note: If using `npm run dev` from the generated `package.json`, the issue will not show up. However, the issue will show up when using `npm run build` followed by `npm run preview`. ```shell npm run build -- --base /test npm run preview -- --base /test npm run dev -- --base /test ```
Run & review this pull request in StackBlitz Codeflow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
This seems to affect all templates (except Vue because it auto imports it), maybe we should update them all at once. |
I'll take a look at the other templates @bluwy. 👍 It's been a few years since I last did any serious front-end dev (my experience is in backend DevEx/Platforms engineering) and I only started trying out Vite today on a whim, but this is a good excuse to brush up. I'll let you know if I have any issues. I'll mark this as draft in the meantime. |
Re-opened for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Thanks for taking the time to check all of them. It is good you didn't modify the Vue template, as these URLs in the SFC templates are processed as you discovered 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great changes! Thanks for fixing the rest of the templates.
As part of my changes in vitejs#12374, I unintentionally removed the quotes for the Vite logos from the vanilla templates, perhaps because the rest of the templates didn't need them. While HTML5 allows no quotes for attribute values if the value doesn't contain a space, keeping a consistent style is important for cognitive load when reading and maintaining. Hence, I've marked this as a style change. While not a functional change in these templates, it could be if someone later changed the import path to contain a space (perhaps after generating a project using the template).
Description
After using create-vite to generate a React project, in a production build the Vite logo will break when setting the base URL to anything except the default
/
. This came up for me when trying to build GitHub Pages in a repo of mine, which requires the base to match the repo name.For example, this build process will result in a broken Vite logo:
# To replicate the issue npm run build -- --base /test npm run preview -- --base /test
This occurs because the
img
src
directly refers to the generated/vite.svg
and React does not preprocesssrc
attributes to add the configuredbase
.As suggested in #7358 (found via #10601), the solution is to import
vite.svg
in the same way asreact.svg
already is. This MR implements that solution in to the templates to prevent confusion in the future.Note: This may also apply to the preact templates. However, I've not modified them as I'm not currently familiar with preact.
Without the fix, using a modified base:
With the fix, using a modified base:
Additional context
If using
npm run dev
from the generatedpackage.json
, the issue will not show up. However, the issue will show up when usingnpm run build
followed bynpm run preview
.I've tested this by modifying a generated project to verify the results of a fix. However, I've not tested this using
npm create vite...
as I'm unsure how to perform a representative test via that method.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).