Skip to content

Route invalidation check is broken due to comparing to intent id, not url and route #8398

@Algoinde

Description

@Algoinde

Describe the bug

When investigating the invalidation mishappenings in my project, I came upon these lines:

https://github.com/sveltejs/kit/blame/4ff91b0e98f571b5e3b0f6e4fc30481426fd6255/packages/kit/src/runtime/client/client.js#L712

The id in that function is taken from intent that is returned from get_navigation_intent. The id in this intent from what I see is used as a cache key. It is, in fact, a URL, not a route ID:

function get_navigation_intent(url, invalidating) {
	if (is_external_url(url, base)) return;

	const path = decode_pathname(url.pathname.slice(base.length) || '/');

	for (const route of routes) {
		const params = route.exec(path);

		if (params) {
			const id = url.pathname + url.search;
			/** @type {import('./types').NavigationIntent} */
			const intent = { id, invalidating, route, params: decode_params(params), url };
			return intent;
		}
	}
}

Later this is used in the aforementioned comparison:

//id == '/whatever/url/you/have/1/1231/'
//current.route.id == '/[page]/url/[who]/[verb]/[id]/[subid]'
const url_changed = current.url ? id !== current.url.pathname + current.url.search : false;
const route_changed = current.route ? id !== current.route.id : false;

As a result of this, route_changed is always true, which I think will invalidate any layouts all the time if they happen to have something that accesses event.route.id in the stack.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-4tqaxj?file=src/routes/stuff/+layout.js

Switch between about and notAbout in the header.

route.id

System Info

System:
    OS: Linux 4.4 Ubuntu 22.04 LTS 22.04 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor
    Memory: 7.40 GB / 31.95 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.16.0/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0-next.106 => 1.0.0-next.106
    @sveltejs/kit: ^1.0.0-next.589 => 1.0.0-next.589
    svelte: ^3.54.0 => 3.55.0
    vite: ^4.0.0 => 4.0.3

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions