Skip to content
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 handleLoad hooks on client and server #9543

Closed
wants to merge 4 commits into from

Conversation

Lms24
Copy link
Contributor

@Lms24 Lms24 commented Mar 28, 2023

This PR is a PoC implementation of our feature request from #9542. It adds support for a handleLoad hook in hooks.client.js as well as handleLoad and handleServerLoad hooks in hooks.server.js.

The usage of these hooks is very similar to the usage of the server-side handle hook, where users are provided with an event and a resolve function.

Please take a look at #9542 for further details.

At this stage, this PR definitely needs more tests and of course a proper review. I'm more than happy to continue working on this if you decide to go forward with the proposed handleLoad hooks. Please feel free to request changes or apply changes yourself - whatever you prefer :)

Also, happy to split this PR up into the individual hooks if you prefer that!

EDIT: I only ran pnpm changeset after opening this PR (sorry!). Should I re-open a new PR or does this still work?


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • 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:.

@changeset-bot
Copy link

changeset-bot bot commented Mar 28, 2023

🦋 Changeset detected

Latest commit: f7de955

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

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

@david-plugge
Copy link
Contributor

david-plugge commented Mar 28, 2023

Awesome stuff!

Would this also allow some sort of locals for shared load functions?

// hooks.client.js

export const handleLoad = ({event, resolve}) => {
  event.locals.myFetch = createFetchClient(event);
  //       ^ better choose a different name

  return resolve(event);
}
// routes/some/page/+page.js

export const load = async ({ locals }) => {
  const res = locals.myFetch(...)
  ...
}

This would make libraries so much easier to build while also resolving the waterfall problem (await parent()) for global state.

@Lms24
Copy link
Contributor Author

Lms24 commented Mar 28, 2023

I haven't touched the type of LoadEvent or ServerLoadEvent types which we'd probably need to do if we wanted to add properties, like locals there. But generally, yes, I guess adding properties to event in the hook could work. Depending on how #9530 is going, we might actually use the handleLoad hook to instrument event.fetch.

I'd argue though that event type expansion is a bit out of scope for this intial PR/feature but I'd leave it up to the Kit maintainers to decide on that :)

@dummdidumm
Copy link
Member

Re changeset: Just amend the PR, changesets will pick it up, and we sqash everything anyway.

@david-plugge
Copy link
Contributor

Seems like this pr got forgotten. I would absolutely love to see shared locals in the future and this pr would be a good first step.

@dummdidumm
Copy link
Member

What do you mean by "shared locals"? Could you add your use case to the related issue?

@david-plugge
Copy link
Contributor

What do you mean by "shared locals"? Could you add your use case to the related issue?

Basicly event.locals for shared load functions. Would make it easier to support custom fetch functions and sdks like supabase.

@devunt
Copy link

devunt commented Dec 3, 2023

Would like to see this get merged since we are also leveraging dynamic load function generation on +page.ts file. For now we put empty +page.ts file besides of +page.svelte file everytime so vite transform plugin can pick it up.

@dummdidumm
Copy link
Member

Closing in favor of #11313 - thank you!

@dummdidumm dummdidumm closed this Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants