sveltejs / sveltejs/kit

Race condition: client side navigation vs SSR

Open
#10,876 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

Quick navigation renders wrong page.

When browser navigating routes A -> B -> C, where A is a separate app, we render B, while address bar showsC.

Critical effect is this: User is served a page with broken interactive elements, if these elements are using [slug] params

Initial investigation

When quickly navigating back from a different site, the second navigation doesn't even trigger an http request, even before the scripts.

I was able to correct this effect locally by adding a location.href check into the rendered html

https://github.com/JakeBeaver/kit/blob/c1477090331570414d4104788347470fee18a2a4/packages/kit/src/runtime/server/page/render.js#L359

    blocks.push(`if (${event.url.href} !== location.href) location.refresh();`);

While this is most definitelly not the fix, it proves that browser navigation during first hydration(?) is ignored completely, but I do not know what interferes with the browser back button before client/start.js runs?

Reproduction

Project:

Create a skeleton project with the following, or clone this repo: https://github.com/JakeBeaver/sveltekit-quick-navigation-reproduction

Content:
  1. /+page.svelte: empty
  2. /load/+page.svelte: empty
  3. /load/+page.server.js: load() that returns a promise
  4. /layout.svelte: navbar, {$page.route.id}, slot
  5. /slug/[slug]/+page.svelte: {$page.params.slug}

Steps:

Start on root route /

  1. Use navbar to open /slug/1
  2. Use address bar to navigate outside of the sveltekit app, like https://github.com/sveltejs/kit
  3. Click the browser back button twice quickly
Result

/slug/[slug] gets rendered but $page.params.slug is missing

image

Logs
C:\repos\my-app> npm run dev                                                                         

> my-app@0.0.1 dev
> vite dev



  VITE v4.4.9  ready in 1311 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
System Info
System:
    OS: Windows 10 10.0.22000
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 12.55 GB / 31.74 GB
  Binaries:
    Node: 18.17.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (115.0.1901.200)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 
    @sveltejs/kit: ^1.20.4 => 1.22.4 
    svelte: ^4.0.5 => 4.1.2 
    vite: ^4.4.2 => 4.4.9
Severity

blocking an upgrade

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the quick back-button navigation with the linked skeleton project, then inspect packages/kit/src/runtime/server/page/render.js and the client/start.js entry point. Verify how navigation from an external site is handled before hydration. Done means the rendered route matches the address bar and [slug] params remain available for interactive elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.