Skip to content

Commit

Permalink
Merge remote-tracking branch 'tanhauhau/tanhauhau/invalidate' into gh…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 1, 2021
2 parents 55100af + e63d7c6 commit 2c957a8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
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 @@ -14,6 +14,7 @@ export const goto = import.meta.env.SSR ? guard('goto') : goto_;
export const invalidate = import.meta.env.SSR ? guard('invalidate') : invalidate_;
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 @@ -46,3 +47,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;
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;
};

0 comments on commit 2c957a8

Please sign in to comment.