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)

Most upvoted comments

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 the invalidate() docs:

If the argument is given as a string or URL, it must resolve to the same URL that was passed to fetch or depends (including query parameters). To create a custom identifier, use a string beginning with [a-z]+: (e.g. custom:state) — this is a valid URL.

I have a similar issue where after using invalidate('some_key'), the data prop gets updated unlike $page.data:

// In child component:
invalidate('some_key');

// In parent
export let data;

$: console.log($page.data); // logs old value;
$: console.log(data); // logs new value; 

We are experiencing the same behaviour. The parent layout load function is invalidated and therefore rerun. But its data is not sent to the child load function where it has await parent(). To be more concise, the invalidate triggers the rerun of the parent layout data and we can see that the load functions order is correct: parent layout -> child page with the updated parentData. However, navigating away from a child page to another child page, we witnessed that on the navigated to child page, the parent data is not up-to-date even though the parent layout load was invalidated and rerun as described above. The same behaviour is also happening when navigating back to the previous child page, which did trigger this child load function where we have also await parent(). But the parent data is not up-to-date. The assumption is that the layout parent data is somehow cached (without the cache being updated) or it is not sent as mentioned in this thread to the child pages. The url.pathname did 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 from SvelteKit version 2. The same mechanism of having parent layout load with child pages depending on their parent data worked fine with the up-to-date parent data whenever invalidated.

@dummdidumm

In the given example, you would want to rerun this on every page change

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 Map of SSR’d query keys to their values. The Map is stored in event.locals and written to by child pages. Currently, on a data request, there is no way to tell sveltekit to return the Map so I have to ignore SSR in those cases, which means I lose prefetching on hover, for example.