Skip to content

Commit

Permalink
Shrink client manifest (#593)
Browse files Browse the repository at this point in the history
* shrink client manifest - fixes #590

* changeset
  • Loading branch information
Rich Harris authored Mar 23, 2021
1 parent f601b51 commit 82cbe2b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-mirrors-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Shrink client manifest
36 changes: 17 additions & 19 deletions packages/kit/src/core/create_app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,32 @@ function generate_client_manifest(manifest_data, base) {
${manifest_data.routes
.map((route) => {
if (route.type === 'page') {
const params = route.params.length
? '(m) => ({ ' +
route.params
const params =
route.params.length > 0 &&
'(m) => ({ ' +
route.params
.map((param, i) => {
return param.startsWith('...')
? `${param.slice(3)}: d(m[${i + 1}])`
: `${param}: d(m[${i + 1}])`;
})
.join(', ') +
'})'
: 'empty';
return `{
// ${route.parts[route.parts.length - 1]}
type: 'page',
pattern: ${route.pattern},
params: ${params},
parts: [${route.parts.map((part) => `components[${component_indexes[part]}]`).join(', ')}]
}`;
'})';
const tuple = [
route.pattern,
`[${route.parts.map((part) => `components[${component_indexes[part]}]`).join(', ')}]`,
params
]
.filter(Boolean)
.join(', ');
return `// ${route.parts[route.parts.length - 1]}\n\t[${tuple}]`;
} else {
return `{
type: 'endpoint',
pattern: ${route.pattern},
reload: true
}`;
return `// ${route.file}\n\t[${route.pattern}]`;
}
})
.join(',\n\n\t\t\t')}
.join(',\n\n\t')}
]`.replace(/^\t/gm, '');

return trim(`
Expand Down
8 changes: 2 additions & 6 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ function prefetch_(href) {
/** @param {string[]} [pathnames] */
async function prefetchRoutes_(pathnames) {
const matching = pathnames
? router.routes.filter((route) => pathnames.some((pathname) => route.pattern.test(pathname)))
? router.routes.filter((route) => pathnames.some((pathname) => route[0].test(pathname)))
: router.routes;

console.log(matching);

const promises = matching.map(
(r) => r.type === 'page' && Promise.all(r.parts.map((load) => load()))
);
const promises = matching.map((r) => r.length !== 1 && Promise.all(r[1].map((load) => load())));

await Promise.all(promises);
}
11 changes: 6 additions & 5 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ export class Renderer {
async _get_navigation_result(info) {
for (let i = 0; i < info.routes.length; i += 1) {
const route = info.routes[i];
const [pattern, parts, params] = route;

if (route.type === 'endpoint') {
if (route.length === 1) {
return { reload: true };
}

Expand All @@ -251,19 +252,19 @@ export class Renderer {
let j = i + 1;
while (j < info.routes.length) {
const next = info.routes[j];
if (next.pattern.toString() === route.pattern.toString()) {
if (next.type === 'page') next.parts.forEach((loader) => loader());
if (next[0].toString() === pattern.toString()) {
if (next.length !== 1) next[1].forEach((loader) => loader());
j += 1;
} else {
break;
}
}

const nodes = route.parts.map((loader) => loader());
const nodes = parts.map((loader) => loader());
const page = {
host: this.host,
path: info.path,
params: route.params(route.pattern.exec(info.path)),
params: params ? params(route[0].exec(info.path)) : {},
query: info.query
};

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class Router {

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

const routes = this.routes.filter((route) => route.pattern.test(path));
const routes = this.routes.filter(([pattern]) => pattern.test(path));

if (routes.length > 0) {
return {
Expand Down
18 changes: 6 additions & 12 deletions packages/kit/types.internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ export type SSRPagePart = {
load: SSRComponentLoader;
};

export type GetParams = (match: RegExpExecArray) => Record<string, string>;

export type SSRPage = {
type: 'page';
pattern: RegExp;
params: (match: RegExpExecArray) => Record<string, string>;
params: GetParams;
parts: SSRPagePart[];
style: string;
css: string[];
Expand All @@ -144,23 +146,15 @@ export type SSRPage = {
export type SSREndpoint = {
type: 'endpoint';
pattern: RegExp;
params: (match: RegExpExecArray) => Record<string, string>;
params: GetParams;
load: () => Promise<any>; // TODO
};

export type SSRRoute = SSREndpoint | SSRPage;

export type CSRPage = {
type: 'page';
pattern: RegExp;
params: (match: RegExpExecArray) => Record<string, string>;
parts: CSRComponentLoader[];
};
export type CSRPage = [RegExp, CSRComponentLoader[], GetParams?];

export type CSREndpoint = {
type: 'endpoint';
pattern: RegExp;
};
export type CSREndpoint = [RegExp];

export type CSRRoute = CSREndpoint | CSRPage;

Expand Down

0 comments on commit 82cbe2b

Please sign in to comment.