-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Avoid loading +page.js
/+layout.js
files on server in SPA mode
#12580
Comments
Update: It seems like this also stops the build process, not just the dev server. Is there really no way to only target a browser if that's the only place your code is going to run in? |
please provide a link to a repository with a complete and minimal reproduction. You mention a custom adapter and the filename you use |
My apologies – the custom adapter and the missing I created a repository to reproduce the problem: https://github.com/janopae/reproduce-sveltekit-tries-to-run-ssr-code-even-with-ssr-disabled |
It seems like SvelteKit has to execute the So we need either another way to add layout code that won't be used at compile time to determine wether SSR is enabled (would be a weird twist on the naming of the file), or we need a way to disable SSR from the begin with (e. g. in the |
you can't use localStorage in +layout.ts without an if(browser) guard, spa mode or not. also you have to put code in exported functions, not top level. |
Im pretty sure your code works IF you put it inside a load function. The file has to be evaluated so sk can read the ssr setting but if SSR is off, the load function is not executed on the server. |
Alright. I think this is an unexpected gotcha that could be avoided with a general Personally, I reconsidered the use of SvelteKit for my use case, as there have been too many gotchas that took me a lot of time to resolve, and they keep coming, mostly relating to disabling SSR:
Especially, developing a browser extension using sk seems impossible at the moment, because both prerendered and "fallback" routing code can only be executed with the CSP setting I'd love to see the SvelteKit homege communicating its focus on deployments with a node server, as currently, the website presents SvelteKit as "super flexible" and especially mentions SPAs without node backends as a use case. |
Thanks for the thorough overview @janopae. I'm early on my SPA journey with SvelteKit, and you point out some gotchas I have yet to hit. I personally didn't reach for SvelteKit for the "typical" reasons. I wanted to use Svelte to create a client application, but there simply aren't viable CSR routers for Svelte other than SvelteKit. My use case is building a local-first application that relies on SQLite in the browser, and I use Personally, I expected a |
I think it makes sense to not have the I've felt the pain of that myself plenty of times when building client-side apps, and I understand the frustration when considering past comments denouncing SPAs. However, we want SvelteKit to be the best tool for the job, even for SPAs!
There are other options such as Routify, and numerous other declarative ones available. Routify v2 specifically should work just fine if you're using Svelte 3/4 |
Funny, @Rich-Harris spoke on this exact in a recent interview!
|
@ghostdevv Appreciate the thorough response here!
Yeah that's all I'm getting at. I totally agree that Also have a suggestion from maintaining Astro: it may help to remove dynamic value support for And I appreciate those links to community routers! I just prefer to use solutions that are first-party maintained and have a diverse contributor graph. I hadn't noticed the latest Routify v3 release though. May be worth a second look! In a perfect world, I'd use a SPA router within Astro ;) |
+page.js
files on server in SPA mode
+page.js
files on server in SPA mode+page.js
/+layout.js
files on server in SPA mode
Thanks for pointing this out guys. I think it's an important issue, so added it to the SvelteKit 3 milestone to make sure we take a look at what can be done better here. I've heard at least a few options:
This is important as many people build SPAs in order to support things like capacitor and tauri and want to be able to directly reach for browser-only APIs without having to add |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This issue exists in another form. When you run This means that the code you write has to satisfy two constraints:
If you squint, this is fine — JS runtimes are constantly converging, and indeed there's a cross-platform effort to define an interoperable platform. But it means that you can't, I don't know, import from This is, of course, the same problem with client-side only SPAs — the code in your Nor is this a dev-only problem. At build time, we evaluate all the files that define route config (albeit in a child Node process) so that we know which routes need to be prerendered, and so that we can pass route config to adapters (so that e.g. With the forthcoming release of the Vite Environment API, we're investigating ways to do SSR in non-Node environments. For example, #12637 has a proof-of-concept implementation of server-side rendering in So there's a common requirement here: we need to be able to express which server runtime applies to a given route without running code. I don't like the 'stop supporting runtime config' or 'do a best-effort static analysis' approaches. Runtime config is very useful (e.g. setting stuff based on environment variables) and necessary for things like If we accept the premise that you should be able to run code to generate your config, the only thing we need to be able to determine without running code is where to run the code. In other words the only thing that can't be runtime config is, well, the There's a few ways we could go about this. It could be a new file ( Note that this all overlaps with Anyone have thoughts on what the nicest version of this looks like? |
Currently leaning towards the pragma (
For disabling server-side evaluation altogether, |
What happens to |
Minifiers will strip it out |
As the same pragma syntax is used for (This might just be bikeshedding) |
Describe the bug
Even though I did everything the docs provide in order to disable any form of server side rendering or static site generation, the dev server tries to execute my code on the server.
Reproduction
npm create svelte@latest my-app cd my-app npm install
Edit
src/routes/+layout.ts
according to https://kit.svelte.dev/docs/single-page-appsEdit
svelte.config.js
according to https://kit.svelte.dev/docs/single-page-apps#usageCreate some code that will only work on client side
Start the dev server
Open the website in browser, and there will be an error complaining about
localStorage
not being definded.Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: