-
-
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: emit assets from SSR build #11430
Conversation
I'm puzzled about why we didn't get a bug report before. I imagine the idea of ffaf50d was avoiding duplicating the generation of assets, as normally all of them will already be there from the client build as you explained. I don't know if we can optimize this given that the build process for the client and ssr is independent in Vite right now, and that the user may configure Vite to avoid wiping the dist folder. Your fix seems to be the safest choice. Let's check with ecosystem-ci first, and merge it if we are all green. |
/ecosystem-ci run |
📝 Ran ecosystem CI: Open
|
I don't quite understand this. Isn't it intentional to not emit assets in SSR builds? It's expected that the assets come from the client build. Otherwise both builds contain duplicated assets and that increases the final client+server bundle size. This seems like a big change instead of a fix. Maybe we could add an experimental option for now until we have a plan for the caveat. As a workaround, you can do something like this too. But I agree that this isn't nice. |
@bluwy I'm assuming that most frameworks will have the |
The current behaviour comes from this (pre-v2) commit, which doesn't appear to be linked to a PR so the motivation is a mystery. But I think it's reasonable to suggest that it was just an oversight — the assumption was that you'd always import assets into modules that Vite sees during the non-SSR build, and that assumption turned out to be wrong. The workaround you suggest is what I'm doing currently (taking care to make sure that the resulting imports are tree-shaken from the client bundle), and it's basically fine if a little undiscoverable. In the SvelteKit case it adds the constraint that assets can't be in Given the availability of the workaround, 4.1 seems totally reasonable. Thanks! |
Ah so you're expecting that both the client and server builds the assets to the same directory. That could work if the user/metaframework sets it up, and we could document it too. Currently Vite's SSR setup recommendation would duplicate the assets by default with this PR, eg with |
Hello, I don't know if I'm off the topic here, but here is a situation that I hope could be helpful, maybe. Before Sveltekit 1.0 (so Vite 4.0), if you imported a component from an external package (from your /node_module/external_package) and this component was importing assets like Now, "external assets" seems hashed and import in client/immutable like before, but vite or svelting not resolve the path, and serve an "undefined" url for all assets coming from external package. NB: even if your vite config is (sorry for my english) |
This is great. VPS goes to great length to work around the fact that SSR assets aren't emitted: https://vite-plugin-ssr.com/includeAssetsImportedByServer#page-content (its implementation is far from trivial, to say the least). I'm 💯 for merging this. |
@7antra that sounds unrelated to this change. I'd suggest opening a new issue with a minimal reproduction, ideally on this repo if you can reproduce it without SvelteKit, or on https://github.com/sveltejs/kit/issues if not |
@Rich-Harris it looks like you need to run |
I did, and nothing changed 🤷 |
That's strange, it added the missing trailing comma for me. |
This will emit the assets to But the assets should be emitted to (As a tangent: this is one argument more in the growing list of reasons for not having two seperate builds. There really should be only a single |
By default yes but in our case we use
Co-sign, though it can easily be more than two (we have a third build for service workers, for example, though we control the build config tightly for that — it doesn't use |
@Rich-Harris we discussed the PR in a team meeting and decided to move forward with an opt-in experimental option so we can get it in 4.1. We may review this option in Vite 5 if we find a better way but it is good if frameworks are able to experiment with it with Vite 4. I think we should name this option |
@Rich-Harris are you saying that's true today or something we'd implement in the future? While we do use that option today, it looks to me like the assets are sent to |
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 change seems fine to me though it's not entirely clear to me how we'd use it on the SvelteKit side
I thought it was, because of https://github.com/sveltejs/kit/blob/9e560183348b69996b13397001996961591544c6/packages/kit/src/exports/vite/index.js#L441, but
We would need to copy the contents of |
Yeah, okay. That's what I was guessing. It's a bit clunky as you said, but a better user experience, so probably worth doing it. I don't know if we can skip the copy step by making |
That won't work, I tried. The asset path, relative to |
I think we could still merge this PR, and you can check in SvelteKit how to best use the new option. If you end up discovering that this isn't the right way to go, we can deprecate it and remove it in Vite 5. I'll merge it before releasing 4.1-beta.1 tomorrow, except if I see you have other ideas. |
@patak-dev @Rich-Harris Can you by any chance consider backporting this to Vite v3? |
We don't usually backport features @iMrDJAi, is there any reason preventing you to update your project to Vite v4? 3 to 4 should normally be a painless update https://vitejs.dev/guide/migration.html |
@patak-dev Upgrading to Vite v4 was much complicated than I expected, some dependencies are no longer functional. This is due to #11101. Unfortunately, this is not an option to me. :/ |
@iMrDJAi are these issues tracked? Have you created issues for them? Please link them here for reference if that is the case. |
@patak-dev Related issues has been already reported (#11299). Some libraries have incorrect |
I tried using
I didn't try, but I think it's fixable by 1. moving files from I think it's pretty clear at this point that we're hitting a limit regarding the fact that Vite builds the client and the server in isolation and independently of each other. (I agree with Rich-Harris and I think we can have both flexbiility while having Vite build the client and the server hand-in-hand.) |
Does this only apply to .css files? If so, it should be fixed by #5619 (comment) |
I see, thanks for the pointer.
…On Fri 3. Mar 2023 at 17:56, Rich Harris ***@***.***> wrote:
These duplicated assets don't have the same hash
Does this only apply to .css files? If so, it should be fixed by #5619
(comment)
<#5619 (comment)>
—
Reply to this email directly, view it on GitHub
<#11430 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHVQRVJEOKAWFL7TLWSBEDW2IPFDANCNFSM6AAAAAATDX24YM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Current workaround: explicitly specifying |
This experimental feature is now used by SvelteKit, Astro, Redwood, and others. We're looking to stabilize it in Vite 5. If you have feedback before we move forward, let's use this discussion: |
Description
This closes #11429, by removing the code that causes the bug
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).