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

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:

https://github.com/sveltejs/kit/assets/38181667/94c16d80-e20f-4be7-93ff-4b021ce0a507

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[id]%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[id]%2F%2Bpage.ts The issue is not reproducible, everything works as expected. See the video:

https://github.com/sveltejs/kit/assets/38181667/40e82b40-cf75-469c-8987-c445eb25de53

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.

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Reactions: 4
  • Comments: 16 (3 by maintainers)

Most upvoted comments

@kyngs not yet; I did it to improve visibility. Unfortently I have no time now to go deeper and fix it. If nobody fixes it next week, I will try to help.

Hasn’t it? Tbh I’ve never had any issues with it in v1

You most likely had the double invalidation bug with SK1 without realizing it https://github.com/sveltejs/kit/issues/10359 since they seem heavily related, but at least it wasn’t app breaking.