kit
kit copied to clipboard
feat: handleLoad hooks
Adds handleLoad and handleServerLoad hooks that run before any invocation of a corresponding load function. closes https://github.com/sveltejs/kit/issues/9542
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
⚠️ No Changeset found
Latest commit: 15f508a4892bafbbdbd9190ad9bddd074071b918
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Noting the brief discussion Rich and I had about this: The fact that you need to be very careful about which things to read in handleLoad feels like a footgun. We're not sure yet if this is an unavoidable fact and that people writing these hooks are probably more aware of the nuances of load invalidation, or if we need to tweak the design a bit (for example only tracking stuff after the inner load was called)
Is it necessary to track anything at all inside the hooks? It feels like that should happen inside the root layout instead and the hook should only be used for logging, modifying, ...
If you implement an auth guard you may want to ensure it runs on every request
Yeah sure, but would it be a problem to always run the hook when the load function chain starts? It feels more intuitive to me, very similar to how the handle hook works.
handleLoad runs for each load function, not once at the beginning for all.
Ah alright. But still, why do we need to track anything then? If your page load function depends on something that runs inside handleLoad it will always be there if it always runs. I don't see why you would need tracking. Maybe I just need an example that makes use of that functionality
I believe there was discussion around providing an untracked version of event properties alongside the main tracked one at some point?
Something like this would greatly help us at Sentry. For example to obtain route information. Right now we rely on a brittle mechanism of avoiding to invoke the invalidation trigger.
Worth noting that we don't alter any behaviour based on reading these values so I don't think it should cause an invalidation.
@Lms24 did fly a bit under the radar, but a mechanism to not cause reruns from reading properties exists in 2.0: https://kit.svelte.dev/docs/load#rerunning-load-functions-untracking-dependencies
In fact, it's used in this PR - else handleLoad doesn't make much sense to have in many cases.
Hi, is there anything I can help with to get this merged?
Hi, is there anything I can help with to get this merged?
Hi Lukas. There's still some deliberation ongoing due to the concern mentioned in https://github.com/sveltejs/kit/pull/11313#issuecomment-1856144691 and we're not sure how to address this yet.
Gonna go ahead and close this, since a) we never found a satisfactory answer to the concerns about invalidation, b) we're moving to a world where load is deprioritised in favour of remote functions, and c) the underlying issue is solved by #13899