-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Vite 4.2 causes change in SSR behaviour #9487
Comments
Same, having this problem since I updated dependencies last week. Had to bump down the version to 4.1.4. |
This seem to be the same as vitejs/vite#12483. If you want to import a client-only module, you need to dynamically import it in |
@bluwy Thanks for the linked issue, I had missed that when I checked. The I am aware of the
If you have a large number of imports which all rely on client side only code, then this is far from enjoyable. In my specific case, I only need to employ SSR in dev as a workaround for another issue, but I can't imagine I am the only one in this situation. |
Sorry I've missed this in my notifications. I don't think the Aside the "author client-only code" experience, I think the core issue is expected, but in any case if you want to suppress it (not recommended), you can use this Vite plugin: {
name: 'unsafe-hide-ssr-error',
configureServer(server) {
const _ssrLoadModule = server.ssrLoadModule;
server.ssrLoadModule = function () {
return _ssrLoadModule.apply(this, arguments).catch((e) => {
if (e.message.includes('window is not defined')) {
return {};
}
throw e;
});
};
}
} This should make the page load. You can use configure Vite's logger if you want to suppress the error messages. Closing as wontfix. |
Describe the bug
Since the update to vite
4.2
, it seems that the way modules are loaded with SSR has changed.In the attached example, the
+layout.svelte
file contains a guard so that it is only rendered on the client. The relevant+page.svelte
includes an import to a 3rd party module which useswindow
. This is obviously not present for SSR.On Vite
4.1
the guard prevents the page from being rendered and so the offending module is never loaded.On Vite
4.2
the module is loaded regardless.Reproduction
A reproduction of the issue can be found at https://github.com/acurrieclark/vite-ssr-error
To reproduce:
npm i
npm run dev
The error will be triggered
Then downgrade
vite
to4.1.4
and repeat the above steps.Logs
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: