kit icon indicating copy to clipboard operation
kit copied to clipboard

feat: handleLoad hooks

Open dummdidumm opened this issue 1 year ago • 11 comments

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 test and lint the project with pnpm lint and pnpm 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 changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

dummdidumm avatar Dec 14 '23 15:12 dummdidumm

⚠️ 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

changeset-bot[bot] avatar Dec 14 '23 15:12 changeset-bot[bot]

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)

dummdidumm avatar Dec 14 '23 16:12 dummdidumm

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, ...

david-plugge avatar Dec 14 '23 21:12 david-plugge

If you implement an auth guard you may want to ensure it runs on every request

dummdidumm avatar Dec 15 '23 07:12 dummdidumm

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.

david-plugge avatar Dec 15 '23 07:12 david-plugge

handleLoad runs for each load function, not once at the beginning for all.

dummdidumm avatar Dec 15 '23 08:12 dummdidumm

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

david-plugge avatar Dec 15 '23 08:12 david-plugge

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 avatar Dec 15 '23 08:12 Lms24

@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.

dummdidumm avatar Dec 15 '23 08:12 dummdidumm

Hi, is there anything I can help with to get this merged?

Lms24 avatar Jan 26 '24 14:01 Lms24

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.

teemingc avatar Jan 26 '24 16:01 teemingc

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

Rich-Harris avatar Aug 17 '25 16:08 Rich-Harris