A reproduction of a bug with load()
and its context
and session
parameters.
- Run
yarn
- Run
yarn dev
- Point your browser at http://localhost:3000/
- Notice the page contains "Info received from top-level layout: undefined"
- Edit
src/routes/$layout.svelte
- Change the
load()
parameters fromload({ page, context })
toload({ page, context, session })
- Save the file and look at the browser
- Notice the page contains "Info received from top-level layout: Info from layout load() function"
- Wonder why adding a
session
parameter caused the context to be successfully passed down
Here's what is happening here:
- The load() function in
$layout.svelte
returns some context data, which is then received by the load() function inindex.svelte
via itscontext
parameter. - The load() function in
index.svelte
also takes asession
parameter. This is enough to access thesession
getter and thus setnode.uses.session
totrue
in theRenderer.hydrate
method in Svelte-Kit'sstart.js
module. - The
index.svelte
route has an onMount() function that sets a value in the$session
store. (I do it inonMount()
because that ensures it doesn't run during SSR: the session store is readonly during SSR.) - When the session changes, Svelte-Kit notices that the
index.svelte
component depends on the session value, and so it re-runs the logic in theif (changed_since_last_render)
block in the hydrate() method... but only for theindex.svelte
component. Because the$layout.svelte
component does not depend on the session value (nothing in itsload()
function or anywhere else accessed thesession
getter, so itsnode.uses.session
value wasfalse
), it was not re-run by the change to the session store, and thus itsload()
function did not fire. - Because the
$layout.svelte
load() function did not re-run, it did not populate the context received byindex.svelte
, unlike the first time it ran. (Check the console log for lines starting with "Received context" to verify that). - If you edit
$layout.svelte
and have it access thesession
parameter (by destructuring it) in load(), then suddenly the context will get passed through toindex.svelte
.
Result: very unexpected behavior from a user's perspective. Changing whether or not $layout.svelte
's load() function takes a session argument has changed what context value is received by index.svelte
's load() function. I suspect that what's actually desired here is that if a component's load() function uses context
, and any of its ancestor components returned a context
output from their load() functions, then those ancestor components need to be re-run any time the descendant needs to be re-run.