Feat: onLeave
hook for load functions
#12724
Labels
feature request
New feature or request
needs-decision
Not sure if we want to do this yet, also design work needed
Describe the problem
I have the need to use WS and SSE subscriptions on different pages across my site.
Currently, my known best way to do it is by initializing the subscription on .svelte files and using either an
$effect
oronMount
to setup the cleanup funcion.There was some discussion here about whether initializing subscriptions in
+layout.ts
files would provide any benefit over doing it on .svelte files and I found these:sveltejs/svelte#12318 (reply in thread)
Of course, this only applies to adapters that only run the load functions on client side (such as adapter-static, which is the one I use).
Describe the proposed solution
Adding an
onLeave
hook to load functions that allows passing a callback that is executed when navigating out of the page or layout.This would start the subscription when the page is preloaded, make the
notifications
subscription available to all child layouts and pages, cancel the current subscription and resubscribe viainvalidateAll
/invalidate
, all without having to put data fetching and manipulation logic in .svelte files, which I consider should only hold UI logic, specially when usingadapter-static
configured for true SPAs, whereload
functions are free from restrictions arising from data having to cross the server->browser gap.The proposed function signature could be one of the following
If multiple
+layout.ts
/+page.ts
files define anonLeave
callback, they should be executed serially in reverse order (starting from the innermost+layout.ts
/+page.ts
) to prevent child layout callbacks from accessing objects that have been already disposed of on parentonLeave
callbacks.Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: