-
-
Notifications
You must be signed in to change notification settings - Fork 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
[feat] add an ssr
parameter to resolve
for better skipping of SSR
#2804
Conversation
🦋 Changeset detectedLatest commit: 7f11e91 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
The names could be a lot better, so I'm open to sugestions. |
One thing I'm not totally sure about: If we introduce this new API, what do we do with the |
The traditional API is still very useful, but the new one is needed because the page needs to be loaded on the server. I decided to name if |
My idea would be to let I'd also rename it from |
@Rich-Harris said he preferred removing I did a code search across GitHub and it was really only being used to deal with things like embedding an editor where I think |
Honestly I just want to have a way to deal with side effect browser code, if I had time I would just make an RFC for something like plugins for the framework, so that I can write my own page config loader and get rid of the problem. |
Don't you already have that with |
No, I mean the function that loads a page's local configuration ( |
A good point raised in the maintainer's channel is that routes don't always map to urls cleanly and so it might be hard for people to migrate to this new option in some cases if we don't support function values |
This pr is not intended to replace nor modify the existing methods to determine the configuration of a page, that would only be a side effect of its main purpose, get rid of annoying errors caused by side effect imports. A good example of this would be the rxfire library. Usually you can import it and use it along firebase to create stores local to the component like this: <script>
import { collectionData } from "rxfire/firestore";
import { collection, where, query } from "firebase/firestore";
import { firestore } from "$lib/firebase";
const citiesRef = query(
collection(firestore, 'cities'),
where('habitants', '>=', 10000)
);
const cities = collectionData(citiesRef, { idField: 'id' });
</script> In this case it is not possible to lazy load any of the libraries because the stores need to be initialized with the component, working around this issue with |
@benmccann is attempting to deploy a commit to the Svelte Team on Vercel. A member of the Team first needs to authorize it. |
❌ Deploy Preview for kit-demo failed. 🔨 Explore the source changes: f6714ae 🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61db6c97a39feb0007a86fc7 |
b091100
to
9658a5a
Compare
9658a5a
to
af22470
Compare
af22470
to
422c54d
Compare
422c54d
to
0e5f291
Compare
0e5f291
to
72d3c35
Compare
finally! thanks for everyone's hard work on this 🍻 |
Guys, this seems a very harsh decision to remove config and page level I may be talking out of place here. As all core members have agreed on this. Apologise for that. |
I also don't like the removal of this option- if element-level hydration becomes a thing for SvelteKit in the future, it would be easier to |
The previous option caused a great deal of confusion, because the page still had to be imported in order to determine whether or not SSR was necessary. Given that, in 90% of cases, the desire to disable SSR selectively was driven by the fact that pages had dependencies that couldn't be imported, the option was near-useless. Having both It wouldn't be good design to preserve the option on the offchance that it would still be relevant if Svelte gained an as-yet hypothetical feature. |
That makes sense. However, is there any other way to disable SSR per-page without a global handler? To me, it doesn't seem like a good design pattern to handle something that is meant to be page-level using a global function. For example Remix uses an optional file-naming scheme like |
For what it's worth, I thought it would be annoying to update my code to incorporate this change but instead it turned out to be the opposite. I was able to remove the ugly dynamic imports I previously had to put in |
Idk but this "solution" feels like it's moving SvelteKit one step further towards being serverside-only now that turning off SSR was made more complex of an endeavour than it should be. We've been hearing from @Rich-Harris that SPA is bad for months now. dc283c0 Not all SPA is bad though. As devs we know when it makes sense for our apps and when it doesn't. What's the next step? Removal of static adapter? |
In my case it was nothing to do with SPA. I needed to import Leaflet and that can only be done in the browser. It was significantly more annoying to import libraries like that prior to this change. |
Astoundingly, no. |
Could you explain in more detail why in your case it's become more complex? As Rich pointed out above, the end result should be simpler now. Previously if you had a client-only-library that you imported you had to do this: <script context="module">
export let ssr = false;
// ..
</script>
<script>
import { onMount } from 'svelte';
let lib;
onMount(() => {
import('lib-from-somewhere').then(module => lib = module);
});
// ...
</script> With this change you can now do <script>
import lib from 'lib-from-somewhere';
// ...
</script> which is much simpler compared to the previous version. I agree though that writing the handle hook might become cumbersome, it could be as simple as export async function handle({ request, resolve }) {
const response = await resolve(request, {
ssr: !request.path.startsWith('/admin')
});
return response;
} If you are developing an SPA (you want nothing to be SSR'd because it's an internal business app for example) then it's even as simple as export async function handle({ request, resolve }) {
const response = await resolve(request, {
ssr: false
});
return response;
} These few lines of code finally enable you to build true SPAs without having to do dynamic imports workarounds. If there are many different single pages that you want to disable SSR for, determining whether or not |
We had considered this, but it seemed extremely unlikely that you'd randomly disable a random half of pages. If you wanted to disable a lot most likely they would be under a shared path like
The primary motivation for this PR was to better support SPA mode as requested in #1650, which is quite the opposite of what you're suggesting. |
I think the main problem is that we're putting a handler in a different location from the rest of the code. Developers might expect the entire configuration of a page to be done within it, without having to search for a global option. It seems like it should be scoped and/or abstracted away to be page-level. |
I'm not sure there's a case where I'd suggest disabling just a specific page(s) though. It's generally better to use |
Honestly, if there is I haven't come across it. If |
I upgraded from v215 to v232 today and was pretty confused with the SSR change. I'm developing a 'business application behind a login' (as the docs describe!) so SSR isn't needed/wanted. If (like me) this had you stumped then do this... REMOVE ADD this to a new file called export async function handle({ request, resolve }) {
const response = await resolve(request, {
ssr: false,
});
return response;
} Docs for hooks @ https://kit.svelte.dev/docs#hooks ❤️ kit |
Another breaking change got introduced with #3384 which breaks the solution above. Solution that works with export async function handle({ event, resolve }) {
const response = await resolve(event, {
ssr: false,
});
return response;
} |
I don't know what a good solution is but I'd like to chime in saying that this affects me as well (some libraries can only be imported on the browser) and the current solution doesn't seem ideal. In terms of maintainability, now you have to go to hooks to see if it has SSR. Ideally, I'd like to be able to write a component and if it does require SSR to be turned off, it can disable SSR in pages that use it. Again, I don't know what the solution is/should be, but just adding my 2 cents. |
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0Closes #1650 by allowing users to pass a second argument to
resolve
inside thehandle
hook. This argument currently contains an option that tells wether or not the page should be loaded on the server.Props to @Rich-Harris for coming up with this solution.
Benefits: