kit: Parent Layout Data is not sent to page when invalidated by `await parent()`
Describe the bug
When a +layout.server.ts load function is re-ran on the server side via await parent(), the data it returns is not sent down to the page.
This should either be considered a bug since the function is called and the data is already calculated, but it is not sent to the page, or there should be some way to manually invalidate it on the server-side (invalidate is client-only).
Note that invalidate does produce the expected result when called from the client.
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-xhrebt?file=src/routes/+layout.server.ts
Logs
No response
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 7.17.0 - /usr/local/bin/npm
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.0.0
@sveltejs/kit: ^1.5.0 => 1.10.0
svelte: ^3.54.0 => 3.55.1
vite: ^4.0.0 => 4.1.4
Severity
serious, but I can work around it
Additional Information
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 7
- Comments: 15 (10 by maintainers)
This example with the latest release still has the same issue:
Expected:
Server logs:
/layout +layout.server.js returning {"returnedFromLoadButNotUpdating":"56:58"}$page.data:{"returnedFromLoadButNotUpdating":"56:58"}Actual:
Server logs:
/layout +layout.server.js returning {"returnedFromLoadButNotUpdating":"56:58"}$page.data:{"returnedFromLoadButNotUpdating":"56:16"}<–@yuliankarapetkov not a fix, but note that
"some_key"does not match the required pattern described in theinvalidate()docs:I have a similar issue where after using
invalidate('some_key'), the datapropgets updated unlike$page.data:We are experiencing the same behaviour. The
parent layout loadfunction isinvalidatedand thereforererun. But its data is not sent to thechild loadfunction where it hasawait parent(). To be more concise, theinvalidatetriggers thererunof theparent layout dataand we can see that theloadfunctions order is correct:parent layout -> child pagewith the updatedparentData. However, navigating awayfrom a child page to another child page, we witnessed that on thenavigated tochild page, theparent datais not up-to-date even though theparent layout loadwasinvalidatedandrerunas described above. The same behaviour is also happening when navigatingback to the previous child page, which did trigger thischild load functionwhere we have alsoawait parent(). But theparent datais not up-to-date. The assumption is that thelayout parent datais somehow cached (without the cache being updated) or it is not sent as mentioned in thisthreadto the child pages. Theurl.pathnamedid solve this issue but it is not performant as it reruns with every page change under this layout of course. It is important to tell that this behaviour is witnessed starting fromSvelteKit version 2. The same mechanism of havingparent layout loadwithchild pagesdepending on theirparent dataworked fine with the up-to-dateparent datawheneverinvalidated.@dummdidumm
I don’t want to rerun it on every page change, I want it to rerun when I tell it to. I want the behavior of
invalidate(...), but called from the+page.server.ts.My use case is for trpc-svelte-query’s SSR. In the root layout, I return a
Mapof SSR’d query keys to their values. TheMapis stored inevent.localsand written to by child pages. Currently, on a data request, there is no way to tell sveltekit to return theMapso I have to ignore SSR in those cases, which means I lose prefetching on hover, for example.