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

invalidate resource #1280

Closed
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function guard(name) {
export const goto = import.meta.env.SSR ? guard('goto') : goto_;
export const prefetch = import.meta.env.SSR ? guard('prefetch') : prefetch_;
export const prefetchRoutes = import.meta.env.SSR ? guard('prefetchRoutes') : prefetchRoutes_;
export const invalidate = import.meta.env.SSR ? guard('invalidate') : invalidate_;

/**
* @type {import('$app/navigation').goto}
Expand Down Expand Up @@ -40,3 +41,8 @@ async function prefetchRoutes_(pathnames) {

await Promise.all(promises);
}

async function invalidate_(resource) {
const info = router.parse(new URL(location.href));
return router.renderer.update({ ...info, invalidates: resource });
}
18 changes: 14 additions & 4 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,12 @@ export class Renderer {
}
}

const result = await this._load({ route, path: info.path, query: info.query });
const result = await this._load({
route,
path: info.path,
query: info.query,
invalidates: info.invalidates
});
if (result) return result;
}

Expand Down Expand Up @@ -418,7 +423,8 @@ export class Renderer {
path: false,
query: false,
session: false,
context: false
context: false,
invalidates: new Set()
},
loaded: null,
context
Expand Down Expand Up @@ -461,7 +467,10 @@ export class Renderer {
node.uses.context = true;
return { ...context };
},
fetch: this.started ? fetch : initial_fetch
fetch(resource, info) {
node.uses.invalidates.add(resource);
return this.started ? fetch(resource, info) : initial_fetch(resource, info);
}
};

if (error) {
Expand All @@ -485,7 +494,7 @@ export class Renderer {
* @param {import('./types').NavigationCandidate} selected
* @returns {Promise<import('./types').NavigationResult>}
*/
async _load({ route, path, query }) {
async _load({ route, path, query, invalidates }) {
const hash = `${path}?${query}`;

if (this.cache.has(hash)) {
Expand Down Expand Up @@ -538,6 +547,7 @@ export class Renderer {
changed.params.some((param) => previous.uses.params.has(param)) ||
(changed.query && previous.uses.query) ||
(changed.session && previous.uses.session) ||
(invalidates && previous.uses.invalidates.has(invalidates)) ||
(context_changed && previous.uses.context);

if (changed_since_last_render) {
Expand Down
49 changes: 25 additions & 24 deletions packages/kit/src/runtime/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ import { Page, LoadOutput } from '../../../types/page';
import { CSRComponent, CSRRoute } from '../../../types/internal';

export type NavigationInfo = {
id: string;
routes: CSRRoute[];
path: string;
query: URLSearchParams;
id: string;
Copy link
Member

Choose a reason for hiding this comment

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

looks like tabs got converted to spaces in this file

routes: CSRRoute[];
path: string;
query: URLSearchParams;
};

export type NavigationCandidate = {
route: CSRRoute;
path: string;
query: URLSearchParams;
route: CSRRoute;
path: string;
query: URLSearchParams;
};

export type NavigationResult = {
reload?: boolean;
redirect?: string;
state?: NavigationState;
props?: Record<string, any>;
reload?: boolean;
redirect?: string;
state?: NavigationState;
props?: Record<string, any>;
};

export type BranchNode = {
module: CSRComponent;
loaded: LoadOutput;
uses: {
params: Set<string>;
path: boolean;
query: boolean;
session: boolean;
context: boolean;
};
context: Record<string, any>;
module: CSRComponent;
loaded: LoadOutput;
uses: {
params: Set<string>;
path: boolean;
query: boolean;
session: boolean;
context: boolean;
invalidates: Set<string>;
};
context: Record<string, any>;
};

export type NavigationState = {
page: Page;
branch: BranchNode[];
session_id: number;
page: Page;
branch: BranchNode[];
session_id: number;
};