-
-
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: server and client init
hook
#13103
Conversation
🦋 Changeset detectedLatest commit: a9c85b0 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 |
311e6b6
to
2a051d3
Compare
I've removed the |
preview: https://svelte-dev-git-preview-kit-13103-svelte.vercel.app/ this is an automated message |
@Rich-Harris the |
|
Actually no, I stand by it — while the server is created anew for each request, the |
|
Co-authored-by: Rich Harris <hello@rich-harris.dev>
You can but it will incur in race conditions with multiple workers (since multiple requests can be made while the app is waiting for init) you can probably fix it by immediately setting options.hook before calling init tho but I remember I still had trouble for some reason I can look into why if you prefer |
Now i remember: what happens is that if you create multiple servers at the same time We could do something like this if (module.init && !!this.#options.hooks) {
await module.init();
} but that would always be false since we set if (module.init && !!this.#options.hooks) {
await module.init();
} if(module.init && !!this.#options.hooks){
this.#options.hooks = {
handle: module.handle || (({ event, resolve }) => resolve(event)),
handleError: module.handleError || (({ error }) => console.error(error)),
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request)),
reroute: module.reroute || (() => {})
};
await module.init();
}else{
this.#options.hooks = {
handle: module.handle || (({ event, resolve }) => resolve(event)),
handleError: module.handleError || (({ error }) => console.error(error)),
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request)),
reroute: module.reroute || (() => {})
};
} but it's a lot of duplication and i liked the boolean solution more |
Ah, interesting. I'm surprised the race condition can manifest in real life since we're just importing local modules. Would just really love to be able to avoid hiding more state in virtual modules, because it makes it quite hard to understand how everything fits together. Just pushed something that seems to work, though I have to run now... |
I mean we could just keep the |
I tried that, it doesn't work because the next |
@Rich-Harris it would be good to also have #13104 merged...currently an |
would this close #927 as well? |
I would say yes |
I don't think it will close it completely - some those use cases sound like they want to be able to do stuff at a lower level before the SvelteKit runtime even starts. |
@Rich-Harris i don't know how this was not a conflict but we had to update the exports in |
This provides an
init
hook that will be awaited on the server and on the client upon app initialisation. A couple of notes:hooks.ts
andhooks.{server|client}.ts
thenhooks.ts
will have priority.This is still missing
but i wanted to get this out to at least get an initial feedback.
It could close #6183 (it's not exactly what's described in the issue however)
closes #927
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
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits