Skip to content

Regression in v2: parent() returns old data even after the parent was invalidated using invalidate() #11696

@kyngs

Description

@kyngs

Describe the bug

This is hard to explain, but I'll do my best.
Imagine the following route structure:

routes/
├─ +page.svelte
├─ +layout.ts
├─ [id]/
│  ├─ +page.svelte
│  ├─ +page.ts
├─ add/
│  ├─ +page.svelte

The root page shows a basket of fruits, which is loaded from an API in the root +layout.ts. It also includes a button that links to the add page.
The add page includes a button that adds fruit to the basket by posting to the API. After doing so, it invalidates the root +layout.ts load function and then navigates to the root page.
The [id] page only verifies whether a fruit with such an ID exists in the basket. It does that using the +page.ts load function, which fetches the parent using await parent() and then observes, whether a fruit with such an ID is in the parent data.

Now, let's imagine I do this:

  • I navigate to the add page
  • I press the button to add a fruit
  • I get redirected to the root page, where the fruit is now listed.
  • I click the fruit, which redirects me to /[id] with the fruit id.
  • However, the page tells me that no such fruit exists.
  • I click back and navigate to the root page, which now does not list the fruit at all.
  • I refreshed the page, and now everything works.

Here's a video where I do that:

simplescreenrecorder-2024-01-20_22.15.00.mp4

However, when I downgrade to sveltekit v1, or use invalidateAll() instead of invalidate(), everything works correctly, and the parent() function returns correct data.

Reproduction

SvelteKit v2: https://stackblitz.com/edit/stackblitz-starters-dp4avt?file=src%2Froutes%2F%5Bid%5D%2F%2Bpage.ts
The issue here is easily reproducible as shown in the video

SvelteKit v1: https://stackblitz.com/edit/stackblitz-starters-et87cn?file=src%2Froutes%2F%5Bid%5D%2F%2Bpage.ts
The issue is not reproducible, everything works as expected. See the video:

simplescreenrecorder-2024-01-20_22.24.28.mp4

And as a bonus, SvelteKit v2 using invalidateAll(): https://stackblitz.com/edit/stackblitz-starters-4rh3gc?file=src%2Froutes%2Fadd%2F%2Bpage.svelte
The issue is also not reproducible, everything works as expected.

Logs

Not applicable

System Info

System:
    OS: Linux 6.6 Arch Linux
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 35.83 GB / 62.70 GB
    Container: Yes
    Shell: 3.7.0 - /usr/bin/fish
  Binaries:
    Node: 18.18.2 - /usr/bin/node
    Yarn: 1.22.21 - /usr/bin/yarn
    npm: 10.2.5 - /usr/bin/npm
    pnpm: 8.14.0 - /usr/bin/pnpm
    bun: 1.0.18 - /usr/bin/bun
  Browsers:
    Chromium: 120.0.6099.199
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.1.1 
    @sveltejs/kit: ^2.0.0 => 2.4.1 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1 
    svelte: ^4.2.7 => 4.2.9 
    vite: ^5.0.3 => 5.0.12

Severity

blocking an upgrade

Additional Information

PS: Even though using invalidateAll is a workaround, I've decided to use the "blocking an upgrade" severity as rerunning all the load functions is not acceptable.
I hope I explained the issue well, if you have any questions, ask me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp1-importantSvelteKit cannot be used by a large number of people, basic functionality is missing, etc.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions