Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat invalidation as navigation #6522

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-ladybugs-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] treat invalidation as a form of navigation
15 changes: 14 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,24 @@ export function create_client({ target, base, trailing_slash }) {
if (!invalidating) {
const url = new URL(location.href);

/** @type {import('types').Navigation} */
const navigation = { from: url, to: url, type: 'invalidation' };

let cancelled = false;
const cancellable = { ...navigation, cancel: () => (cancelled = true) };

callbacks.before_navigate.forEach((cb) => cb(cancellable));

invalidating = Promise.resolve().then(async () => {
await update(url, []);
if (!cancelled) {
stores.navigating.set(navigation);
await update(url, []);
callbacks.after_navigate.forEach((cb) => cb(navigation));
}

invalidating = null;
force_invalidation = false;
stores.navigating.set(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this result in a wrong navigation state when the user clicks on a link while the invalidating is in progress?
Would probably only be noticeable on a slow connection.

});
}

Expand Down
8 changes: 4 additions & 4 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ declare module '$app/navigation' {
export function prefetchRoutes(routes?: string[]): Promise<void>;

/**
* A navigation interceptor that triggers before we navigate to a new URL, whether by clicking a link, calling `goto(...)`, or using the browser back/forward controls.
* Calling `cancel()` will prevent the navigation from completing.
* A navigation interceptor that triggers before we navigate to a new URLwhether by clicking a link, calling `goto(...)`, or using the browser back/forward controls — or `invalidate(...)` data.
* Calling `cancel()` will prevent the navigation/invalidation from completing.
*
* When navigating to an external URL, `navigation.to` will be `null`.
*
Expand All @@ -172,7 +172,7 @@ declare module '$app/navigation' {
): void;

/**
* A lifecycle function that runs the supplied `callback` when the current component mounts, and also whenever we navigate to a new URL.
* A lifecycle function that runs the supplied `callback` when the current component mounts, and also whenever we navigate to a new URL or`invalidate(...)` data.
*
* `afterNavigate` must be called during a component initialization. It remains active as long as the component is mounted.
*/
Expand Down Expand Up @@ -218,7 +218,7 @@ declare module '$app/stores' {
export const page: Readable<Page>;
/**
* A readable store.
* When navigating starts, its value is a `Navigation` object with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
* When navigating (or invalidating) starts, its value is a `Navigation` object with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
* When navigating finishes, its value reverts to `null`.
*/
export const navigating: Readable<Navigation | null>;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export interface LoadEvent<
depends: (...deps: string[]) => void;
}

export type NavigationType = 'load' | 'unload' | 'link' | 'goto' | 'popstate';
export type NavigationType = 'load' | 'unload' | 'link' | 'goto' | 'popstate' | 'invalidation';

export interface Navigation {
from: URL | null;
Expand Down