mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 12:24:47 +00:00
Compare commits
34 Commits
v0.22.3
...
gh-262-no-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2319ed9ed3 | ||
|
|
5d7a715122 | ||
|
|
448e91a970 | ||
|
|
d927597c1e | ||
|
|
aa92342e71 | ||
|
|
99b4cd43b9 | ||
|
|
20015d6613 | ||
|
|
3d39ef96c2 | ||
|
|
c867f051c2 | ||
|
|
9e1207cdd7 | ||
|
|
f126c6ac6c | ||
|
|
84aaf3be4a | ||
|
|
e751cbebd5 | ||
|
|
d2ee6ff3d5 | ||
|
|
6c68b3151c | ||
|
|
5428a7ec95 | ||
|
|
dd190af904 | ||
|
|
1f66e4c530 | ||
|
|
b0b8b78c5d | ||
|
|
7d3e191e46 | ||
|
|
61e3b6c6a0 | ||
|
|
af0bd1580d | ||
|
|
34cf953fde | ||
|
|
8299c68678 | ||
|
|
f221281f8a | ||
|
|
9dc5ba569f | ||
|
|
969430716f | ||
|
|
624f17364c | ||
|
|
c90d8ee3cd | ||
|
|
6c5630f281 | ||
|
|
bf8e56748a | ||
|
|
5b024f2c8d | ||
|
|
1af0f13799 | ||
|
|
80ece8b148 |
@@ -10,7 +10,7 @@ build: off
|
|||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
# node.js
|
# node.js
|
||||||
- nodejs_version: stable
|
- nodejs_version: 10.5
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- ps: Install-Product node $env:nodejs_version
|
- ps: Install-Product node $env:nodejs_version
|
||||||
|
|||||||
1
fallback.html
Normal file
1
fallback.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
--require source-map-support/register
|
--require source-map-support/register
|
||||||
--recursive
|
--recursive
|
||||||
test/unit/**/*.js
|
test/unit/*/*.js
|
||||||
test/common/test.js
|
test/common/test.js
|
||||||
@@ -105,8 +105,15 @@ class Watcher extends EventEmitter {
|
|||||||
|
|
||||||
const dev_port = await ports.find(10000);
|
const dev_port = await ports.find(10000);
|
||||||
|
|
||||||
const routes = create_routes();
|
try {
|
||||||
create_main_manifests({ routes, dev_port });
|
const routes = create_routes();
|
||||||
|
create_main_manifests({ routes, dev_port });
|
||||||
|
} catch (err) {
|
||||||
|
this.emit('fatal', <events.FatalEvent>{
|
||||||
|
message: err.message
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.dev_server = new DevServer(dev_port);
|
this.dev_server = new DevServer(dev_port);
|
||||||
|
|
||||||
@@ -114,6 +121,15 @@ class Watcher extends EventEmitter {
|
|||||||
watch_files(locations.routes(), ['add', 'unlink'], () => {
|
watch_files(locations.routes(), ['add', 'unlink'], () => {
|
||||||
const routes = create_routes();
|
const routes = create_routes();
|
||||||
create_main_manifests({ routes, dev_port });
|
create_main_manifests({ routes, dev_port });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const routes = create_routes();
|
||||||
|
create_main_manifests({ routes, dev_port });
|
||||||
|
} catch (err) {
|
||||||
|
this.emit('error', <events.ErrorEvent>{
|
||||||
|
message: err.message
|
||||||
|
});
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
watch_files(`${locations.app()}/template.html`, ['change'], () => {
|
watch_files(`${locations.app()}/template.html`, ['change'], () => {
|
||||||
@@ -272,7 +288,7 @@ class Watcher extends EventEmitter {
|
|||||||
if (this.closed) return;
|
if (this.closed) return;
|
||||||
this.closed = true;
|
this.closed = true;
|
||||||
|
|
||||||
this.dev_server.close();
|
if (this.dev_server) this.dev_server.close();
|
||||||
|
|
||||||
if (this.proc) this.proc.kill();
|
if (this.proc) this.proc.kill();
|
||||||
this.filewatchers.forEach(watcher => {
|
this.filewatchers.forEach(watcher => {
|
||||||
|
|||||||
@@ -2,15 +2,14 @@ import * as glob from 'glob';
|
|||||||
import { locations } from '../config';
|
import { locations } from '../config';
|
||||||
import { create_routes } from '../core';
|
import { create_routes } from '../core';
|
||||||
|
|
||||||
export function find_page(pathname: string, files: string[] = glob.sync('**/*.*', { cwd: locations.routes(), dot: true, nodir: true })) {
|
export function find_page(pathname: string, cwd = locations.routes()) {
|
||||||
const routes = create_routes({ files });
|
const { pages } = create_routes(cwd);
|
||||||
|
|
||||||
for (let i = 0; i < routes.length; i += 1) {
|
for (let i = 0; i < pages.length; i += 1) {
|
||||||
const route = routes[i];
|
const page = pages[i];
|
||||||
|
|
||||||
if (route.pattern.test(pathname)) {
|
if (page.pattern.test(pathname)) {
|
||||||
const page = route.handlers.find(handler => handler.type === 'page');
|
return page.parts[page.parts.length - 1].component.file;
|
||||||
if (page) return page.file;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,10 @@ import * as path from 'path';
|
|||||||
import * as glob from 'glob';
|
import * as glob from 'glob';
|
||||||
import { posixify, write_if_changed } from './utils';
|
import { posixify, write_if_changed } from './utils';
|
||||||
import { dev, locations } from '../config';
|
import { dev, locations } from '../config';
|
||||||
import { Route } from '../interfaces';
|
import { Page, PageComponent, ServerRoute } from '../interfaces';
|
||||||
|
|
||||||
export function create_main_manifests({ routes, dev_port }: {
|
export function create_main_manifests({ routes, dev_port }: {
|
||||||
routes: Route[];
|
routes: { components: PageComponent[], pages: Page[], server_routes: ServerRoute[] };
|
||||||
dev_port?: number;
|
dev_port?: number;
|
||||||
}) {
|
}) {
|
||||||
const path_to_routes = path.relative(`${locations.app()}/manifest`, locations.routes());
|
const path_to_routes = path.relative(`${locations.app()}/manifest`, locations.routes());
|
||||||
@@ -19,7 +19,7 @@ export function create_main_manifests({ routes, dev_port }: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function create_serviceworker_manifest({ routes, client_files }: {
|
export function create_serviceworker_manifest({ routes, client_files }: {
|
||||||
routes: Route[];
|
routes: { components: PageComponent[], pages: Page[], server_routes: ServerRoute[] };
|
||||||
client_files: string[];
|
client_files: string[];
|
||||||
}) {
|
}) {
|
||||||
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
||||||
@@ -32,41 +32,63 @@ export function create_serviceworker_manifest({ routes, client_files }: {
|
|||||||
|
|
||||||
export const shell = [\n\t${client_files.map((x: string) => `"${x}"`).join(',\n\t')}\n];
|
export const shell = [\n\t${client_files.map((x: string) => `"${x}"`).join(',\n\t')}\n];
|
||||||
|
|
||||||
export const routes = [\n\t${routes.pages.filter(r => r.id !== '_error').map((r: Route) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
|
export const routes = [\n\t${routes.pages.map((r: Page) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
|
||||||
`.replace(/^\t\t/gm, '').trim();
|
`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
write_if_changed(`${locations.app()}/manifest/service-worker.js`, code);
|
write_if_changed(`${locations.app()}/manifest/service-worker.js`, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_client(routes: Route[], path_to_routes: string, dev_port?: number) {
|
function right_pad(str: string, len: number) {
|
||||||
const page_ids = new Set(routes.pages.map(page => page.id));
|
while (str.length < len) str += ' ';
|
||||||
const server_routes_to_ignore = routes.server_routes.filter(route => !page_ids.has(route.id));
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
const pages = routes.pages.filter(page => page.id !== '_error');
|
function generate_client(
|
||||||
const error_route = routes.pages.find(page => page.id === '_error');
|
routes: { components: PageComponent[], pages: Page[], server_routes: ServerRoute[] },
|
||||||
|
path_to_routes: string,
|
||||||
|
dev_port?: number
|
||||||
|
) {
|
||||||
|
const page_ids = new Set(routes.pages.map(page =>
|
||||||
|
page.pattern.toString()));
|
||||||
|
|
||||||
|
const server_routes_to_ignore = routes.server_routes.filter(route =>
|
||||||
|
!page_ids.has(route.pattern.toString()));
|
||||||
|
|
||||||
|
const len = Math.max(...routes.components.map(c => c.name.length));
|
||||||
|
|
||||||
let code = `
|
let code = `
|
||||||
// This file is generated by Sapper — do not edit it!
|
// This file is generated by Sapper — do not edit it!
|
||||||
|
import root from '${posixify(`${path_to_routes}/index.html`)}';
|
||||||
|
import error from '${posixify(`${path_to_routes}/_error.html`)}';
|
||||||
|
|
||||||
|
${routes.components.map(component =>
|
||||||
|
`const ${component.name} = () =>
|
||||||
|
import(/* webpackChunkName: "${component.name}" */ '${posixify(`${path_to_routes}/${component.file}`)}');`)
|
||||||
|
.join('\n')}
|
||||||
|
|
||||||
export const routes = {
|
export const routes = {
|
||||||
ignore: [${server_routes_to_ignore.map(route => route.pattern).join(', ')}],
|
ignore: [${server_routes_to_ignore.map(route => route.pattern).join(', ')}],
|
||||||
|
|
||||||
pages: [
|
pages: [
|
||||||
${pages.map(page => {
|
${routes.pages.map(page => `{
|
||||||
const file = posixify(`${path_to_routes}/${page.file}`);
|
// ${page.parts[page.parts.length - 1].component.file}
|
||||||
|
pattern: ${page.pattern},
|
||||||
|
parts: [
|
||||||
|
${page.parts.map(part => {
|
||||||
|
if (part.params.length > 0) {
|
||||||
|
const props = part.params.map((param, i) => `${param}: match[${i + 1}]`);
|
||||||
|
return `{ component: ${part.component.name}, params: match => ({ ${props.join(', ')} }) }`;
|
||||||
|
}
|
||||||
|
|
||||||
if (page.id === '_error') {
|
return `{ component: ${part.component.name} }`;
|
||||||
return `{ error: true, load: () => import(/* webpackChunkName: "${page.id}" */ '${file}') }`;
|
}).join(',\n\t\t\t\t\t\t')}
|
||||||
}
|
]
|
||||||
|
}`).join(',\n\n\t\t\t\t')}
|
||||||
const params = page.params.length === 0
|
|
||||||
? '{}'
|
|
||||||
: `{ ${page.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
|
||||||
|
|
||||||
return `{ pattern: ${page.pattern}, params: ${page.params.length > 0 ? `match` : `()`} => (${params}), load: () => import(/* webpackChunkName: "${page.id}" */ '${file}') }`;
|
|
||||||
}).join(',\n\t\t\t\t')}
|
|
||||||
],
|
],
|
||||||
|
|
||||||
error: () => import(/* webpackChunkName: '_error' */ '${posixify(`${path_to_routes}/${error_route.file}`)}')
|
root,
|
||||||
|
|
||||||
|
error
|
||||||
};`.replace(/^\t\t/gm, '').trim();
|
};`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
if (dev()) {
|
if (dev()) {
|
||||||
@@ -86,15 +108,17 @@ function generate_client(routes: Route[], path_to_routes: string, dev_port?: num
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_server(routes: Route[], path_to_routes: string) {
|
function generate_server(
|
||||||
const error_route = routes.pages.find(page => page.id === '_error');
|
routes: { components: PageComponent[], pages: Page[], server_routes: ServerRoute[] },
|
||||||
|
path_to_routes: string
|
||||||
|
) {
|
||||||
const imports = [].concat(
|
const imports = [].concat(
|
||||||
routes.server_routes.map(route =>
|
routes.server_routes.map(route =>
|
||||||
`import * as route_${route.id} from '${posixify(`${path_to_routes}/${route.file}`)}';`),
|
`import * as ${route.name} from '${posixify(`${path_to_routes}/${route.file}`)}';`),
|
||||||
routes.pages.map(page =>
|
routes.components.map(component =>
|
||||||
`import page_${page.id} from '${posixify(`${path_to_routes}/${page.file}`)}';`),
|
`import ${component.name} from '${posixify(`${path_to_routes}/${component.file}`)}';`),
|
||||||
`import error from '${posixify(`${path_to_routes}/${error_route.file}`)}';`
|
`import root from '${posixify(`${path_to_routes}/index.html`)}';`,
|
||||||
|
`import error from '${posixify(`${path_to_routes}/_error.html`)}';`
|
||||||
);
|
);
|
||||||
|
|
||||||
let code = `
|
let code = `
|
||||||
@@ -103,29 +127,41 @@ function generate_server(routes: Route[], path_to_routes: string) {
|
|||||||
|
|
||||||
export const routes = {
|
export const routes = {
|
||||||
server_routes: [
|
server_routes: [
|
||||||
${routes.server_routes.map(route => {
|
${routes.server_routes.map(route => `{
|
||||||
const params = route.params.length === 0
|
// ${route.file}
|
||||||
? '{}'
|
pattern: ${route.pattern},
|
||||||
: `{ ${route.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
handlers: ${route.name},
|
||||||
|
params: ${route.params.length > 0
|
||||||
return `{ id: '${route.id}', pattern: ${route.pattern}, params: ${route.params.length > 0 ? `match` : `()`} => (${params}), handlers: route_${route.id} }`;
|
? `match => ({ ${route.params.map((param, i) => `${param}: match[${i + 1}]`).join(', ')} })`
|
||||||
}).join(',\n\t\t\t\t')}
|
: `() => ({})`}
|
||||||
|
}`).join(',\n\n\t\t\t\t')}
|
||||||
],
|
],
|
||||||
|
|
||||||
pages: [
|
pages: [
|
||||||
${routes.pages.map(page => {
|
${routes.pages.map(page => `{
|
||||||
const params = page.params.length === 0
|
// ${page.parts[page.parts.length - 1].component.file}
|
||||||
? '{}'
|
pattern: ${page.pattern},
|
||||||
: `{ ${page.params.map((part, i) => `${part}: match[${i + 1}]`).join(', ')} }`;
|
parts: [
|
||||||
|
${page.parts.map(part => {
|
||||||
|
const props = [
|
||||||
|
`name: "${part.component.name}"`,
|
||||||
|
`component: ${part.component.name}`
|
||||||
|
];
|
||||||
|
|
||||||
return `{ id: '${page.id}', pattern: ${page.pattern}, params: ${page.params.length > 0 ? `match` : `()`} => (${params}), handler: page_${page.id} }`;
|
if (part.params.length > 0) {
|
||||||
}).join(',\n\t\t\t\t')}
|
const params = part.params.map((param, i) => `${param}: match[${i + 1}]`);
|
||||||
|
props.push(`params: match => ({ ${params.join(', ')} })`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return `{ ${props.join(', ')} }`;
|
||||||
|
}).join(',\n\t\t\t\t\t\t')}
|
||||||
|
]
|
||||||
|
}`).join(',\n\n\t\t\t\t')}
|
||||||
],
|
],
|
||||||
|
|
||||||
error: {
|
root,
|
||||||
error: true,
|
|
||||||
handler: error
|
error
|
||||||
}
|
|
||||||
};`.replace(/^\t\t/gm, '').trim();
|
};`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
return code;
|
return code;
|
||||||
|
|||||||
@@ -1,186 +1,320 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import glob from 'glob';
|
|
||||||
import { locations } from '../config';
|
import { locations } from '../config';
|
||||||
import { Route } from '../interfaces';
|
import { Page, PageComponent, ServerRoute } from '../interfaces';
|
||||||
|
import { posixify } from './utils';
|
||||||
|
|
||||||
export default function create_routes({
|
export default function create_routes(cwd = locations.routes()) {
|
||||||
files
|
const components: PageComponent[] = [];
|
||||||
} = {
|
const pages: Page[] = [];
|
||||||
files: glob.sync('**/*.*', {
|
const server_routes: ServerRoute[] = [];
|
||||||
cwd: locations.routes(),
|
|
||||||
dot: true,
|
|
||||||
nodir: true
|
|
||||||
})
|
|
||||||
}) {
|
|
||||||
const all_routes = files
|
|
||||||
.filter((file: string) => !/(^|\/|\\)(_(?!error\.html)|\.(?!well-known))/.test(file))
|
|
||||||
.map((file: string) => {
|
|
||||||
if (/]\[/.test(file)) {
|
|
||||||
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file === '4xx.html' || file === '5xx.html') {
|
function walk(
|
||||||
throw new Error('As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html');
|
dir: string,
|
||||||
}
|
parent_segments: Part[][],
|
||||||
|
parent_params: string[],
|
||||||
|
stack: Array<{
|
||||||
|
component: PageComponent,
|
||||||
|
params: string[]
|
||||||
|
}>
|
||||||
|
) {
|
||||||
|
const items = fs.readdirSync(dir)
|
||||||
|
.map(basename => {
|
||||||
|
const resolved = path.join(dir, basename);
|
||||||
|
const file = path.relative(cwd, resolved);
|
||||||
|
const is_dir = fs.statSync(resolved).isDirectory();
|
||||||
|
|
||||||
const base = file.replace(/\.[^/.]+$/, '');
|
const segment = is_dir
|
||||||
const parts = base.split('/'); // glob output is always posix-style
|
? basename
|
||||||
if (/^index(\..+)?/.test(parts[parts.length - 1])) {
|
: basename.slice(0, -path.extname(basename).length);
|
||||||
const part = parts.pop();
|
|
||||||
if (parts.length > 0) parts[parts.length - 1] += part.slice(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = (
|
const parts = get_parts(segment);
|
||||||
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
|
const is_index = is_dir ? false : basename.startsWith('index.');
|
||||||
) || '_';
|
const is_page = path.extname(basename) === '.html';
|
||||||
|
|
||||||
const type = file.endsWith('.html') ? 'page' : 'route';
|
parts.forEach(part => {
|
||||||
|
if (/\]\[/.test(part.content)) {
|
||||||
const params: string[] = [];
|
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
||||||
const match_patterns: Record<string, string> = {};
|
|
||||||
const param_pattern = /\[([^\(\]]+)(?:\((.+?)\))?\]/g;
|
|
||||||
|
|
||||||
let match;
|
|
||||||
while (match = param_pattern.exec(base)) {
|
|
||||||
params.push(match[1]);
|
|
||||||
if (typeof match[2] !== 'undefined') {
|
|
||||||
if (/[\(\)\?\:]/.exec(match[2])) {
|
|
||||||
throw new Error('Sapper does not allow (, ), ? or : in RegExp routes yet');
|
|
||||||
}
|
}
|
||||||
// Make a map of the regexp patterns
|
|
||||||
match_patterns[match[1]] = `(${match[2]}?)`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO can we do all this with sub-parts? or does
|
if (part.qualifier && /[\(\)\?\:]/.test(part.qualifier.slice(1, -1))) {
|
||||||
// nesting make that impossible?
|
throw new Error(`Invalid route ${file} — cannot use (, ), ? or : in route qualifiers`);
|
||||||
let pattern_string = '';
|
}
|
||||||
let i = parts.length;
|
|
||||||
let nested = true;
|
|
||||||
while (i--) {
|
|
||||||
const part = encodeURI(parts[i].normalize()).replace(/\?/g, '%3F').replace(/#/g, '%23').replace(/%5B/g, '[').replace(/%5D/g, ']');
|
|
||||||
const dynamic = ~part.indexOf('[');
|
|
||||||
|
|
||||||
if (dynamic) {
|
|
||||||
// Get keys from part and replace with stored match patterns
|
|
||||||
const keys = part.replace(/\(.*?\)/, '').split(/[\[\]]/).filter((x, i) => { if (i % 2) return x });
|
|
||||||
let matcher = part;
|
|
||||||
keys.forEach(k => {
|
|
||||||
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
|
|
||||||
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
|
|
||||||
})
|
|
||||||
pattern_string = (nested && type === 'page') ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
|
||||||
} else {
|
|
||||||
nested = false;
|
|
||||||
pattern_string = `\\/${part}${pattern_string}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const pattern = new RegExp(`^${pattern_string}\\/?$`);
|
|
||||||
|
|
||||||
const test = (url: string) => pattern.test(url);
|
|
||||||
|
|
||||||
const exec = (url: string) => {
|
|
||||||
const match = pattern.exec(url);
|
|
||||||
if (!match) return;
|
|
||||||
|
|
||||||
const result: Record<string, string> = {};
|
|
||||||
params.forEach((param, i) => {
|
|
||||||
result[param] = match[i + 1];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return {
|
||||||
};
|
basename,
|
||||||
|
parts,
|
||||||
|
file: posixify(file),
|
||||||
|
is_dir,
|
||||||
|
is_index,
|
||||||
|
is_page
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.sort(comparator);
|
||||||
|
|
||||||
return {
|
items.forEach(item => {
|
||||||
id,
|
if (item.basename[0] === '_') {
|
||||||
base,
|
if (item.basename !== (item.is_dir ? '_default' : '_default.html')) return;
|
||||||
type,
|
|
||||||
file,
|
|
||||||
pattern,
|
|
||||||
test,
|
|
||||||
exec,
|
|
||||||
parts,
|
|
||||||
params
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const pages = all_routes
|
|
||||||
.filter(r => r.type === 'page')
|
|
||||||
.sort(comparator);
|
|
||||||
|
|
||||||
const server_routes = all_routes
|
|
||||||
.filter(r => r.type === 'route')
|
|
||||||
.sort(comparator);
|
|
||||||
|
|
||||||
return { pages, server_routes };
|
|
||||||
}
|
|
||||||
|
|
||||||
function comparator(a, b) {
|
|
||||||
if (a.parts[0] === '_error') return -1;
|
|
||||||
if (b.parts[0] === '_error') return 1;
|
|
||||||
|
|
||||||
const max = Math.max(a.parts.length, b.parts.length);
|
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
|
||||||
const a_part = a.parts[i];
|
|
||||||
const b_part = b.parts[i];
|
|
||||||
|
|
||||||
if (!a_part) return -1;
|
|
||||||
if (!b_part) return 1;
|
|
||||||
|
|
||||||
const a_sub_parts = get_sub_parts(a_part);
|
|
||||||
const b_sub_parts = get_sub_parts(b_part);
|
|
||||||
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
|
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
|
||||||
const a_sub_part = a_sub_parts[i];
|
|
||||||
const b_sub_part = b_sub_parts[i];
|
|
||||||
|
|
||||||
if (!a_sub_part) return 1; // b is more specific, so goes first
|
|
||||||
if (!b_sub_part) return -1;
|
|
||||||
|
|
||||||
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
|
|
||||||
return a_sub_part.dynamic ? 1 : -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
if (item.basename[0] === '.') {
|
||||||
return (
|
if (item.file !== '.well-known') return;
|
||||||
(b_sub_part.content.length - a_sub_part.content.length) ||
|
}
|
||||||
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
|
||||||
|
const segments = parent_segments.slice();
|
||||||
|
|
||||||
|
if (item.is_index && segments.length > 0) {
|
||||||
|
const last_segment = segments[segments.length - 1].slice();
|
||||||
|
const suffix = item.basename
|
||||||
|
.slice(0, -path.extname(item.basename).length).
|
||||||
|
replace('index', '');
|
||||||
|
|
||||||
|
if (suffix) {
|
||||||
|
const last_part = last_segment[last_segment.length - 1];
|
||||||
|
if (last_part.dynamic) {
|
||||||
|
last_segment.push({ dynamic: false, content: suffix });
|
||||||
|
} else {
|
||||||
|
last_segment[last_segment.length - 1] = {
|
||||||
|
dynamic: false,
|
||||||
|
content: `${last_part.content}${suffix}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
segments[segments.length - 1] = last_segment;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
segments.push(item.parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = parent_params.slice();
|
||||||
|
params.push(...item.parts.filter(p => p.dynamic).map(p => p.content));
|
||||||
|
|
||||||
|
if (item.is_dir) {
|
||||||
|
const index = path.join(dir, item.basename, 'index.html');
|
||||||
|
const component = fs.existsSync(index)
|
||||||
|
? {
|
||||||
|
name: `page_${get_slug(item.file)}`,
|
||||||
|
file: `${item.file}/index.html`
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (component) {
|
||||||
|
components.push(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(
|
||||||
|
path.join(dir, item.basename),
|
||||||
|
segments,
|
||||||
|
params,
|
||||||
|
stack.concat({
|
||||||
|
component: component || {
|
||||||
|
missing: true,
|
||||||
|
name: null,
|
||||||
|
file: path.join(item.file, 'index.html')
|
||||||
|
},
|
||||||
|
params
|
||||||
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If both parts dynamic, check for regexp patterns
|
else if (item.basename === 'index.html') {
|
||||||
if (a_sub_part.dynamic && b_sub_part.dynamic) {
|
const is_branch = items.some(other_item => {
|
||||||
const regexp_pattern = /\((.*?)\)/;
|
if (other_item === item) return false;
|
||||||
const a_match = regexp_pattern.exec(a_sub_part.content);
|
if (other_item.basename[0] === '_') {
|
||||||
const b_match = regexp_pattern.exec(b_sub_part.content);
|
return other_item.basename === (other_item.is_dir ? '_default' : '_default.html');
|
||||||
|
}
|
||||||
|
|
||||||
if (!a_match && b_match) {
|
if (other_item.is_dir) {
|
||||||
return 1; // No regexp, so less specific than b
|
return fs.existsSync(path.join(dir, other_item.basename, 'index.html'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return other_item.is_page;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!is_branch) {
|
||||||
|
pages.push({
|
||||||
|
pattern: get_pattern(parent_segments),
|
||||||
|
parts: stack
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (!b_match && a_match) {
|
}
|
||||||
return -1;
|
|
||||||
}
|
else if (item.is_page) {
|
||||||
if (a_match && b_match && a_match[1] !== b_match[1]) {
|
const component = {
|
||||||
return b_match[1].length - a_match[1].length;
|
name: `page_${get_slug(item.file)}`,
|
||||||
|
file: item.file
|
||||||
|
};
|
||||||
|
|
||||||
|
const parts = stack.concat({
|
||||||
|
component,
|
||||||
|
params
|
||||||
|
});
|
||||||
|
|
||||||
|
components.push(component);
|
||||||
|
if (item.basename === '_default.html') {
|
||||||
|
pages.push({
|
||||||
|
pattern: get_pattern(parent_segments),
|
||||||
|
parts
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
pages.push({
|
||||||
|
pattern: get_pattern(segments),
|
||||||
|
parts
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
server_routes.push({
|
||||||
|
name: `route_${get_slug(item.file)}`,
|
||||||
|
pattern: get_pattern(segments),
|
||||||
|
file: item.file,
|
||||||
|
params: params
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(cwd, [], [], []);
|
||||||
|
|
||||||
|
const seen_pages: Map<string, Page> = new Map();
|
||||||
|
pages.forEach(page => {
|
||||||
|
// check for missing intermediate index.html files
|
||||||
|
let i = page.parts.length;
|
||||||
|
const last_part = page.parts[i - 1];
|
||||||
|
while (i--) {
|
||||||
|
const part = page.parts[i];
|
||||||
|
if (part.component.missing) {
|
||||||
|
throw new Error(`Missing ${part.component.file}, which is required for ${last_part.component.file} to be valid`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for clashes
|
||||||
|
const pattern = page.pattern.toString();
|
||||||
|
if (seen_pages.has(pattern)) {
|
||||||
|
const file = page.parts.pop().component.file;
|
||||||
|
const other_page = seen_pages.get(pattern);
|
||||||
|
const other_file = other_page.parts.pop().component.file;
|
||||||
|
|
||||||
|
throw new Error(`The ${other_file} and ${file} pages clash`);
|
||||||
|
}
|
||||||
|
|
||||||
|
seen_pages.set(pattern, page);
|
||||||
|
});
|
||||||
|
|
||||||
|
const seen_routes: Map<string, ServerRoute> = new Map();
|
||||||
|
server_routes.forEach(route => {
|
||||||
|
const pattern = route.pattern.toString();
|
||||||
|
if (seen_routes.has(pattern)) {
|
||||||
|
const other_route = seen_routes.get(pattern);
|
||||||
|
throw new Error(`The ${other_route.file} and ${route.file} routes clash`);
|
||||||
|
}
|
||||||
|
|
||||||
|
seen_routes.set(pattern, route);
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
components,
|
||||||
|
pages,
|
||||||
|
server_routes
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
type Part = {
|
||||||
|
content: string;
|
||||||
|
dynamic: boolean;
|
||||||
|
qualifier?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function comparator(
|
||||||
|
a: { basename: string, parts: Part[], file: string, is_dir: boolean },
|
||||||
|
b: { basename: string, parts: Part[], file: string, is_dir: boolean }
|
||||||
|
) {
|
||||||
|
const max = Math.max(a.parts.length, b.parts.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < max; i += 1) {
|
||||||
|
const a_sub_part = a.parts[i];
|
||||||
|
const b_sub_part = b.parts[i];
|
||||||
|
|
||||||
|
if (!a_sub_part) return 1; // b is more specific, so goes first
|
||||||
|
if (!b_sub_part) return -1;
|
||||||
|
|
||||||
|
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
|
||||||
|
return a_sub_part.dynamic ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
||||||
|
return (
|
||||||
|
(b_sub_part.content.length - a_sub_part.content.length) ||
|
||||||
|
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If both parts dynamic, check for regexp patterns
|
||||||
|
if (a_sub_part.dynamic && b_sub_part.dynamic) {
|
||||||
|
const regexp_pattern = /\((.*?)\)/;
|
||||||
|
const a_match = regexp_pattern.exec(a_sub_part.content);
|
||||||
|
const b_match = regexp_pattern.exec(b_sub_part.content);
|
||||||
|
|
||||||
|
if (!a_match && b_match) {
|
||||||
|
return 1; // No regexp, so less specific than b
|
||||||
|
}
|
||||||
|
if (!b_match && a_match) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (a_match && b_match && a_match[1] !== b_match[1]) {
|
||||||
|
return b_match[1].length - a_match[1].length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`The ${a.base} and ${b.base} routes clash`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_sub_parts(part: string) {
|
function get_parts(part: string): Part[] {
|
||||||
return part.split(/\[(.+)\]/)
|
return part.split(/\[(.+)\]/)
|
||||||
.map((content, i) => {
|
.map((str, i) => {
|
||||||
if (!content) return null;
|
if (!str) return null;
|
||||||
|
const dynamic = i % 2 === 1;
|
||||||
|
|
||||||
|
const [, content, qualifier] = dynamic
|
||||||
|
? /([^(]+)(\(.+\))?$/.exec(str)
|
||||||
|
: [, str, null];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content,
|
content,
|
||||||
dynamic: i % 2 === 1
|
dynamic,
|
||||||
|
qualifier
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_slug(file: string) {
|
||||||
|
return file
|
||||||
|
.replace(/[\\\/]index/, '')
|
||||||
|
.replace(/_default([\/\\index])?\.html$/, 'index')
|
||||||
|
.replace(/[\/\\]/g, '_')
|
||||||
|
.replace(/\.\w+$/, '')
|
||||||
|
.replace(/\[([^(]+)(?:\([^(]+\))?\]/, '$$$1')
|
||||||
|
.replace(/[^a-zA-Z0-9_$]/g, c => {
|
||||||
|
return c === '.' ? '_' : `$${c.charCodeAt(0)}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_pattern(segments: Part[][]) {
|
||||||
|
return new RegExp(
|
||||||
|
`^` +
|
||||||
|
segments.map(segment => {
|
||||||
|
return '\\/' + segment.map(part => {
|
||||||
|
return part.dynamic
|
||||||
|
? part.qualifier || '([^\\/]+?)'
|
||||||
|
: encodeURI(part.content.normalize())
|
||||||
|
.replace(/\?/g, '%3F')
|
||||||
|
.replace(/#/g, '%23')
|
||||||
|
.replace(/%5B/g, '[')
|
||||||
|
.replace(/%5D/g, ']');
|
||||||
|
}).join('');
|
||||||
|
}).join('') +
|
||||||
|
'\\\/?$'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -18,4 +18,25 @@ export type Template = {
|
|||||||
|
|
||||||
export type Store = {
|
export type Store = {
|
||||||
get: () => any;
|
get: () => any;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PageComponent = {
|
||||||
|
missing?: boolean;
|
||||||
|
name: string;
|
||||||
|
file: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Page = {
|
||||||
|
pattern: RegExp;
|
||||||
|
parts: Array<{
|
||||||
|
component: PageComponent;
|
||||||
|
params: string[];
|
||||||
|
}>
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ServerRoute = {
|
||||||
|
name: string;
|
||||||
|
pattern: RegExp;
|
||||||
|
file: string;
|
||||||
|
params: string[];
|
||||||
};
|
};
|
||||||
@@ -11,13 +11,26 @@ import sourceMapSupport from 'source-map-support';
|
|||||||
|
|
||||||
sourceMapSupport.install();
|
sourceMapSupport.install();
|
||||||
|
|
||||||
type RouteObject = {
|
type ServerRoute = {
|
||||||
id: string;
|
|
||||||
type: 'page' | 'route';
|
|
||||||
pattern: RegExp;
|
pattern: RegExp;
|
||||||
|
handlers: Record<string, Handler>;
|
||||||
params: (match: RegExpMatchArray) => Record<string, string>;
|
params: (match: RegExpMatchArray) => Record<string, string>;
|
||||||
module: Component;
|
};
|
||||||
error?: string;
|
|
||||||
|
type Page = {
|
||||||
|
pattern: RegExp;
|
||||||
|
parts: Array<{
|
||||||
|
name: string;
|
||||||
|
component: Component;
|
||||||
|
params?: (match: RegExpMatchArray) => Record<string, string>;
|
||||||
|
}>
|
||||||
|
};
|
||||||
|
|
||||||
|
type RouteObject = {
|
||||||
|
server_routes: ServerRoute[];
|
||||||
|
pages: Page[];
|
||||||
|
root: Component;
|
||||||
|
error: Component;
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler = (req: Req, res: ServerResponse, next: () => void) => void;
|
type Handler = (req: Req, res: ServerResponse, next: () => void) => void;
|
||||||
@@ -33,6 +46,7 @@ interface Req extends ClientRequest {
|
|||||||
method: string;
|
method: string;
|
||||||
path: string;
|
path: string;
|
||||||
params: Record<string, string>;
|
params: Record<string, string>;
|
||||||
|
query: Record<string, string>;
|
||||||
headers: Record<string, string>;
|
headers: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,15 +59,10 @@ interface Component {
|
|||||||
preload: (data: any) => any | Promise<any>
|
preload: (data: any) => any | Promise<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function middleware({ App, routes, store }: {
|
export default function middleware({ routes, store }: {
|
||||||
App: Component,
|
routes: RouteObject,
|
||||||
routes: RouteObject[],
|
|
||||||
store: (req: Req) => Store
|
store: (req: Req) => Store
|
||||||
}) {
|
}) {
|
||||||
if (!App) {
|
|
||||||
throw new Error(`As of 0.12, you must supply an App component to Sapper — see https://sapper.svelte.technology/guide#0-11-to-0-12 for more information`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const output = locations.dest();
|
const output = locations.dest();
|
||||||
|
|
||||||
let emitted_basepath = false;
|
let emitted_basepath = false;
|
||||||
@@ -109,7 +118,7 @@ export default function middleware({ App, routes, store }: {
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
get_server_route_handler(routes.server_routes),
|
get_server_route_handler(routes.server_routes),
|
||||||
get_page_handler(App, routes, store)
|
get_page_handler(routes, store)
|
||||||
].filter(Boolean));
|
].filter(Boolean));
|
||||||
|
|
||||||
return middleware;
|
return middleware;
|
||||||
@@ -152,8 +161,8 @@ function serve({ prefix, pathname, cache_control }: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_server_route_handler(routes: RouteObject[]) {
|
function get_server_route_handler(routes: ServerRoute[]) {
|
||||||
function handle_route(route, req, res, next) {
|
function handle_route(route: ServerRoute, req: Req, res: ServerResponse, next: () => void) {
|
||||||
req.params = route.params(route.pattern.exec(req.path));
|
req.params = route.params(route.pattern.exec(req.path));
|
||||||
|
|
||||||
const method = req.method.toLowerCase();
|
const method = req.method.toLowerCase();
|
||||||
@@ -196,7 +205,6 @@ function get_server_route_handler(routes: RouteObject[]) {
|
|||||||
|
|
||||||
const handle_next = (err?: Error) => {
|
const handle_next = (err?: Error) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err.stack);
|
|
||||||
res.statusCode = 500;
|
res.statusCode = 500;
|
||||||
res.end(err.message);
|
res.end(err.message);
|
||||||
} else {
|
} else {
|
||||||
@@ -215,7 +223,7 @@ function get_server_route_handler(routes: RouteObject[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return function find_route(req: Req, res: ServerResponse, next) {
|
return function find_route(req: Req, res: ServerResponse, next: () => void) {
|
||||||
for (const route of routes) {
|
for (const route of routes) {
|
||||||
if (route.pattern.test(req.path)) {
|
if (route.pattern.test(req.path)) {
|
||||||
handle_route(route, req, res, next);
|
handle_route(route, req, res, next);
|
||||||
@@ -227,7 +235,7 @@ function get_server_route_handler(routes: RouteObject[]) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_page_handler(App: Component, routes: RouteObject[], store_getter: (req: Req) => Store) {
|
function get_page_handler(routes: RouteObject, store_getter: (req: Req) => Store) {
|
||||||
const output = locations.dest();
|
const output = locations.dest();
|
||||||
|
|
||||||
const get_chunks = dev()
|
const get_chunks = dev()
|
||||||
@@ -241,19 +249,29 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
const { server_routes, pages } = routes;
|
const { server_routes, pages } = routes;
|
||||||
const error_route = routes.error;
|
const error_route = routes.error;
|
||||||
|
|
||||||
function handle_route(route: RouteObject, req: Req, res: ServerResponse, status = 200, error: Error | string = null) {
|
function handle_page(page: Page, req: Req, res: ServerResponse, status = 200, error: Error | string = null) {
|
||||||
|
const get_params = page.parts[page.parts.length - 1].params || (() => ({}));
|
||||||
|
const match = error ? null : page.pattern.exec(req.path);
|
||||||
|
|
||||||
req.params = error
|
req.params = error
|
||||||
? {}
|
? {}
|
||||||
: route.params(route.pattern.exec(req.path));
|
: get_params(match);
|
||||||
|
|
||||||
const chunks: Record<string, string> = get_chunks();
|
const chunks: Record<string, string | string[]> = get_chunks();
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/html');
|
res.setHeader('Content-Type', 'text/html');
|
||||||
|
|
||||||
// preload main.js and current route
|
// preload main.js and current route
|
||||||
// TODO detect other stuff we can preload? images, CSS, fonts?
|
// TODO detect other stuff we can preload? images, CSS, fonts?
|
||||||
const link = []
|
let preloaded_chunks = Array.isArray(chunks.main) ? chunks.main : [chunks.main];
|
||||||
.concat(chunks.main, chunks[route.id] || chunks._error) // TODO this is gross
|
if (!error) {
|
||||||
|
page.parts.forEach(part => {
|
||||||
|
// using concat because it could be a string or an array. thanks webpack!
|
||||||
|
preloaded_chunks = preloaded_chunks.concat(chunks[part.name]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const link = preloaded_chunks
|
||||||
.filter(file => !file.match(/\.map$/))
|
.filter(file => !file.match(/\.map$/))
|
||||||
.map(file => `<${req.baseUrl}/client/${file}>;rel="preload";as="script"`)
|
.map(file => `<${req.baseUrl}/client/${file}>;rel="preload";as="script"`)
|
||||||
.join(', ');
|
.join(', ');
|
||||||
@@ -261,9 +279,9 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
res.setHeader('Link', link);
|
res.setHeader('Link', link);
|
||||||
|
|
||||||
const store = store_getter ? store_getter(req) : null;
|
const store = store_getter ? store_getter(req) : null;
|
||||||
const props = { params: req.params, query: req.query, path: req.path };
|
const props = { query: req.query, path: req.path };
|
||||||
|
|
||||||
if (route.error) {
|
if (error) {
|
||||||
props.error = error instanceof Error ? error : { message: error };
|
props.error = error instanceof Error ? error : { message: error };
|
||||||
props.status = status;
|
props.status = status;
|
||||||
}
|
}
|
||||||
@@ -271,53 +289,59 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
let redirect: { statusCode: number, location: string };
|
let redirect: { statusCode: number, location: string };
|
||||||
let preload_error: { statusCode: number, message: Error | string };
|
let preload_error: { statusCode: number, message: Error | string };
|
||||||
|
|
||||||
Promise.resolve(
|
Promise.all(page.parts.map(part => {
|
||||||
route.handler.preload ? route.handler.preload.call({
|
return part.component.preload
|
||||||
redirect: (statusCode: number, location: string) => {
|
? part.component.preload.call({
|
||||||
redirect = { statusCode, location };
|
redirect: (statusCode: number, location: string) => {
|
||||||
},
|
if (redirect && (redirect.statusCode !== statusCode || redirect.location !== location)) {
|
||||||
error: (statusCode: number, message: Error | string) => {
|
throw new Error(`Conflicting redirects`);
|
||||||
preload_error = { statusCode, message };
|
|
||||||
},
|
|
||||||
fetch: (url: string, opts?: any) => {
|
|
||||||
const parsed = new URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
|
|
||||||
|
|
||||||
if (opts) {
|
|
||||||
opts = Object.assign({}, opts);
|
|
||||||
|
|
||||||
const include_cookies = (
|
|
||||||
opts.credentials === 'include' ||
|
|
||||||
opts.credentials === 'same-origin' && parsed.origin === `http://127.0.0.1:${process.env.PORT}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (include_cookies) {
|
|
||||||
const cookies: Record<string, string> = {};
|
|
||||||
if (!opts.headers) opts.headers = {};
|
|
||||||
|
|
||||||
const str = []
|
|
||||||
.concat(
|
|
||||||
cookie.parse(req.headers.cookie || ''),
|
|
||||||
cookie.parse(opts.headers.cookie || ''),
|
|
||||||
cookie.parse(res.getHeader('Set-Cookie') || '')
|
|
||||||
)
|
|
||||||
.map(cookie => {
|
|
||||||
return Object.keys(cookie)
|
|
||||||
.map(name => `${name}=${encodeURIComponent(cookie[name])}`)
|
|
||||||
.join('; ');
|
|
||||||
})
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(', ');
|
|
||||||
|
|
||||||
opts.headers.cookie = str;
|
|
||||||
}
|
}
|
||||||
}
|
redirect = { statusCode, location };
|
||||||
|
},
|
||||||
|
error: (statusCode: number, message: Error | string) => {
|
||||||
|
preload_error = { statusCode, message };
|
||||||
|
},
|
||||||
|
fetch: (url: string, opts?: any) => {
|
||||||
|
const parsed = new URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
|
||||||
|
|
||||||
return fetch(parsed.href, opts);
|
if (opts) {
|
||||||
},
|
opts = Object.assign({}, opts);
|
||||||
store
|
|
||||||
}, req) : {}
|
const include_cookies = (
|
||||||
).catch(err => {
|
opts.credentials === 'include' ||
|
||||||
|
opts.credentials === 'same-origin' && parsed.origin === `http://127.0.0.1:${process.env.PORT}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (include_cookies) {
|
||||||
|
const cookies: Record<string, string> = {};
|
||||||
|
if (!opts.headers) opts.headers = {};
|
||||||
|
|
||||||
|
const str = []
|
||||||
|
.concat(
|
||||||
|
cookie.parse(req.headers.cookie || ''),
|
||||||
|
cookie.parse(opts.headers.cookie || ''),
|
||||||
|
cookie.parse(res.getHeader('Set-Cookie') || '')
|
||||||
|
)
|
||||||
|
.map(cookie => {
|
||||||
|
return Object.keys(cookie)
|
||||||
|
.map(name => `${name}=${encodeURIComponent(cookie[name])}`)
|
||||||
|
.join('; ');
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(', ');
|
||||||
|
|
||||||
|
opts.headers.cookie = str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(parsed.href, opts);
|
||||||
|
},
|
||||||
|
store
|
||||||
|
}, req)
|
||||||
|
: {};
|
||||||
|
})).catch(err => {
|
||||||
preload_error = { statusCode: 500, message: err };
|
preload_error = { statusCode: 500, message: err };
|
||||||
|
return []; // appease TypeScript
|
||||||
}).then(preloaded => {
|
}).then(preloaded => {
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
res.statusCode = redirect.statusCode;
|
res.statusCode = redirect.statusCode;
|
||||||
@@ -328,17 +352,47 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (preload_error) {
|
if (preload_error) {
|
||||||
handle_route(error_route, req, res, preload_error.statusCode, preload_error.message);
|
handle_page({
|
||||||
|
pattern: null,
|
||||||
|
parts: [
|
||||||
|
{ name: null, component: error_route }
|
||||||
|
]
|
||||||
|
}, req, res, preload_error.statusCode, preload_error.message);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const serialized = {
|
const serialized = {
|
||||||
preloaded: route.handler.preload && try_serialize(preloaded),
|
preloaded: page.parts.map((part, i) => {
|
||||||
|
return part.component.preload ? try_serialize(preloaded[i]) : null;
|
||||||
|
}),
|
||||||
store: store && try_serialize(store.get())
|
store: store && try_serialize(store.get())
|
||||||
};
|
};
|
||||||
Object.assign(props, preloaded);
|
|
||||||
|
|
||||||
const { html, head, css } = App.render({ Page: route.handler, props }, {
|
const segments = req.path.split('/').filter(Boolean);
|
||||||
|
|
||||||
|
const data = Object.assign({}, props, { params: req.params }, {
|
||||||
|
child: {}
|
||||||
|
});
|
||||||
|
let level = data.child;
|
||||||
|
for (let i = 0; i < page.parts.length; i += 1) {
|
||||||
|
const part = page.parts[i];
|
||||||
|
const get_params = part.params || (() => ({}));
|
||||||
|
|
||||||
|
Object.assign(level, {
|
||||||
|
segment: segments[i],
|
||||||
|
component: part.component,
|
||||||
|
props: Object.assign({}, props, {
|
||||||
|
params: get_params(match),
|
||||||
|
query: req.query
|
||||||
|
}, preloaded[i])
|
||||||
|
});
|
||||||
|
|
||||||
|
level.props.child = {};
|
||||||
|
level = level.props.child;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { html, head, css } = routes.root.render(data, {
|
||||||
store
|
store
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -350,7 +404,7 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
|
|
||||||
let inline_script = `__SAPPER__={${[
|
let inline_script = `__SAPPER__={${[
|
||||||
`baseUrl: "${req.baseUrl}"`,
|
`baseUrl: "${req.baseUrl}"`,
|
||||||
serialized.preloaded && `preloaded: ${serialized.preloaded}`,
|
serialized.preloaded && `preloaded: [${serialized.preloaded}]`,
|
||||||
serialized.store && `store: ${serialized.store}`
|
serialized.store && `store: ${serialized.store}`
|
||||||
].filter(Boolean).join(',')}};`;
|
].filter(Boolean).join(',')}};`;
|
||||||
|
|
||||||
@@ -359,7 +413,7 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
inline_script += `if ('serviceWorker' in navigator) navigator.serviceWorker.register('${req.baseUrl}/service-worker.js');`;
|
inline_script += `if ('serviceWorker' in navigator) navigator.serviceWorker.register('${req.baseUrl}/service-worker.js');`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const page = template()
|
const body = template()
|
||||||
.replace('%sapper.base%', () => `<base href="${req.baseUrl}/">`)
|
.replace('%sapper.base%', () => `<base href="${req.baseUrl}/">`)
|
||||||
.replace('%sapper.scripts%', () => `<script>${inline_script}</script>${scripts}`)
|
.replace('%sapper.scripts%', () => `<script>${inline_script}</script>${scripts}`)
|
||||||
.replace('%sapper.html%', () => html)
|
.replace('%sapper.html%', () => html)
|
||||||
@@ -367,7 +421,7 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
.replace('%sapper.styles%', () => (css && css.code ? `<style>${css.code}</style>` : ''));
|
.replace('%sapper.styles%', () => (css && css.code ? `<style>${css.code}</style>` : ''));
|
||||||
|
|
||||||
res.statusCode = status;
|
res.statusCode = status;
|
||||||
res.end(page);
|
res.end(body);
|
||||||
|
|
||||||
if (process.send) {
|
if (process.send) {
|
||||||
process.send({
|
process.send({
|
||||||
@@ -377,9 +431,12 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
method: req.method,
|
method: req.method,
|
||||||
status: 200,
|
status: 200,
|
||||||
type: 'text/html',
|
type: 'text/html',
|
||||||
body: page
|
body
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}).catch(err => {
|
||||||
|
res.statusCode = 500;
|
||||||
|
res.end(err.message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,13 +444,18 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
if (!server_routes.some(route => route.pattern.test(req.path))) {
|
if (!server_routes.some(route => route.pattern.test(req.path))) {
|
||||||
for (const page of pages) {
|
for (const page of pages) {
|
||||||
if (page.pattern.test(req.path)) {
|
if (page.pattern.test(req.path)) {
|
||||||
handle_route(page, req, res);
|
handle_page(page, req, res);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_route(error_route, req, res, 404, 'Not found');
|
handle_page({
|
||||||
|
pattern: null,
|
||||||
|
parts: [
|
||||||
|
{ name: null, component: error_route }
|
||||||
|
]
|
||||||
|
}, req, res, 404, 'Not found');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,39 @@
|
|||||||
import { detach, findAnchor, scroll_state, which } from './utils';
|
import { detach, findAnchor, scroll_state, which } from './utils';
|
||||||
import { Component, ComponentConstructor, Params, Query, Route, RouteData, ScrollPosition, Store, Target } from './interfaces';
|
import { Component, ComponentConstructor, Params, Query, Redirect, Routes, RouteData, ScrollPosition, Store, Target } from './interfaces';
|
||||||
|
|
||||||
const manifest = typeof window !== 'undefined' && window.__SAPPER__;
|
const initial_data = typeof window !== 'undefined' && window.__SAPPER__;
|
||||||
|
|
||||||
export let App: ComponentConstructor;
|
export let root: Component;
|
||||||
export let component: Component;
|
|
||||||
let target: Node;
|
let target: Node;
|
||||||
let store: Store;
|
let store: Store;
|
||||||
let routes: Route[];
|
let routes: Routes;
|
||||||
let error_route: Route;
|
let segments: string[] = [];
|
||||||
|
|
||||||
|
type RootProps = {
|
||||||
|
path: string;
|
||||||
|
params: Record<string, string>;
|
||||||
|
query: Record<string, string>;
|
||||||
|
child: Child;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Child = {
|
||||||
|
segment?: string;
|
||||||
|
props?: any;
|
||||||
|
component?: Component;
|
||||||
|
};
|
||||||
|
|
||||||
|
const root_props: RootProps = {
|
||||||
|
path: null,
|
||||||
|
params: null,
|
||||||
|
query: null,
|
||||||
|
child: {
|
||||||
|
segment: null,
|
||||||
|
component: null,
|
||||||
|
props: {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { root as component }; // legacy reasons — drop in a future version
|
||||||
|
|
||||||
const history = typeof window !== 'undefined' ? window.history : {
|
const history = typeof window !== 'undefined' ? window.history : {
|
||||||
pushState: (state: any, title: string, href: string) => {},
|
pushState: (state: any, title: string, href: string) => {},
|
||||||
@@ -26,9 +51,9 @@ if ('scrollRestoration' in history) {
|
|||||||
|
|
||||||
function select_route(url: URL): Target {
|
function select_route(url: URL): Target {
|
||||||
if (url.origin !== window.location.origin) return null;
|
if (url.origin !== window.location.origin) return null;
|
||||||
if (!url.pathname.startsWith(manifest.baseUrl)) return null;
|
if (!url.pathname.startsWith(initial_data.baseUrl)) return null;
|
||||||
|
|
||||||
const path = url.pathname.slice(manifest.baseUrl.length);
|
const path = url.pathname.slice(initial_data.baseUrl.length);
|
||||||
|
|
||||||
// avoid accidental clashes between server routes and pages
|
// avoid accidental clashes between server routes and pages
|
||||||
if (routes.ignore.some(pattern => pattern.test(path))) return;
|
if (routes.ignore.some(pattern => pattern.test(path))) return;
|
||||||
@@ -38,33 +63,38 @@ function select_route(url: URL): Target {
|
|||||||
|
|
||||||
const match = page.pattern.exec(path);
|
const match = page.pattern.exec(path);
|
||||||
if (match) {
|
if (match) {
|
||||||
const params = page.params(match);
|
|
||||||
|
|
||||||
const query: Record<string, string | true> = {};
|
const query: Record<string, string | true> = {};
|
||||||
if (url.search.length > 0) {
|
if (url.search.length > 0) {
|
||||||
url.search.slice(1).split('&').forEach(searchParam => {
|
url.search.slice(1).split('&').forEach(searchParam => {
|
||||||
const [, key, value] = /([^=]+)=(.*)/.exec(searchParam);
|
const [, key, value] = /([^=]+)=(.*)/.exec(searchParam);
|
||||||
query[key] = value || true;
|
query[key] = value || true;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
return { url, route: page, props: { params, query, path } };
|
return { url, path, page, match, query };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_token: {};
|
let current_token: {};
|
||||||
|
|
||||||
function render(Page: ComponentConstructor, props: any, scroll: ScrollPosition, token: {}) {
|
function render(data: any, changed_from: number, scroll: ScrollPosition, token: {}) {
|
||||||
if (current_token !== token) return;
|
if (current_token !== token) return;
|
||||||
|
|
||||||
const data = {
|
if (root) {
|
||||||
Page,
|
// first, clear out highest-level root component
|
||||||
props,
|
let level = data.child;
|
||||||
preloading: false
|
for (let i = 0; i < changed_from; i += 1) {
|
||||||
};
|
if (i === changed_from) break;
|
||||||
|
level = level.props.child;
|
||||||
|
}
|
||||||
|
|
||||||
if (component) {
|
const { component } = level;
|
||||||
component.set(data);
|
level.component = null;
|
||||||
|
root.set({ child: data.child });
|
||||||
|
|
||||||
|
// then render new stuff
|
||||||
|
level.component = component;
|
||||||
|
root.set(data);
|
||||||
} else {
|
} else {
|
||||||
// first load — remove SSR'd <head> contents
|
// first load — remove SSR'd <head> contents
|
||||||
const start = document.querySelector('#sapper-head-start');
|
const start = document.querySelector('#sapper-head-start');
|
||||||
@@ -76,7 +106,7 @@ function render(Page: ComponentConstructor, props: any, scroll: ScrollPosition,
|
|||||||
detach(end);
|
detach(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
component = new App({
|
root = new routes.root({
|
||||||
target,
|
target,
|
||||||
data,
|
data,
|
||||||
store,
|
store,
|
||||||
@@ -87,52 +117,134 @@ function render(Page: ComponentConstructor, props: any, scroll: ScrollPosition,
|
|||||||
if (scroll) {
|
if (scroll) {
|
||||||
window.scrollTo(scroll.x, scroll.y);
|
window.scrollTo(scroll.x, scroll.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.assign(root_props, data);
|
||||||
|
ready = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepare_route(Page: ComponentConstructor, props: RouteData) {
|
function changed(a: Record<string, string | true>, b: Record<string, string | true>) {
|
||||||
let redirect: { statusCode: number, location: string } = null;
|
return JSON.stringify(a) !== JSON.stringify(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
function prepare_page(target: Target): Promise<{
|
||||||
|
redirect?: Redirect;
|
||||||
|
data?: any;
|
||||||
|
changed_from?: number;
|
||||||
|
}> {
|
||||||
|
if (root) {
|
||||||
|
root.set({ preloading: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { page, path, query } = target;
|
||||||
|
const new_segments = path.split('/').filter(Boolean);
|
||||||
|
let changed_from = 0;
|
||||||
|
|
||||||
|
while (
|
||||||
|
segments[changed_from] &&
|
||||||
|
new_segments[changed_from] &&
|
||||||
|
segments[changed_from] === new_segments[changed_from]
|
||||||
|
) changed_from += 1;
|
||||||
|
|
||||||
|
let redirect: Redirect = null;
|
||||||
let error: { statusCode: number, message: Error | string } = null;
|
let error: { statusCode: number, message: Error | string } = null;
|
||||||
|
|
||||||
if (!Page.preload) {
|
const preload_context = {
|
||||||
return { Page, props, redirect, error };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!component && manifest.preloaded) {
|
|
||||||
return { Page, props: Object.assign(props, manifest.preloaded), redirect, error };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (component) {
|
|
||||||
component.set({
|
|
||||||
preloading: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(Page.preload.call({
|
|
||||||
store,
|
store,
|
||||||
fetch: (url: string, opts?: any) => window.fetch(url, opts),
|
fetch: (url: string, opts?: any) => window.fetch(url, opts),
|
||||||
redirect: (statusCode: number, location: string) => {
|
redirect: (statusCode: number, location: string) => {
|
||||||
|
if (redirect && (redirect.statusCode !== statusCode || redirect.location !== location)) {
|
||||||
|
throw new Error(`Conflicting redirects`);
|
||||||
|
}
|
||||||
redirect = { statusCode, location };
|
redirect = { statusCode, location };
|
||||||
},
|
},
|
||||||
error: (statusCode: number, message: Error | string) => {
|
error: (statusCode: number, message: Error | string) => {
|
||||||
error = { statusCode, message };
|
error = { statusCode, message };
|
||||||
}
|
}
|
||||||
}, props)).catch(err => {
|
};
|
||||||
|
|
||||||
|
return Promise.all(page.parts.map(async (part, i) => {
|
||||||
|
if (i < changed_from) return null;
|
||||||
|
|
||||||
|
const { default: Component } = await part.component();
|
||||||
|
const req = {
|
||||||
|
path,
|
||||||
|
query,
|
||||||
|
params: part.params ? part.params(target.match) : {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const preloaded = ready || !initial_data.preloaded[i]
|
||||||
|
? Component.preload ? await Component.preload.call(preload_context, req) : {}
|
||||||
|
: initial_data.preloaded[i];
|
||||||
|
|
||||||
|
return { Component, preloaded };
|
||||||
|
})).catch(err => {
|
||||||
error = { statusCode: 500, message: err };
|
error = { statusCode: 500, message: err };
|
||||||
}).then(preloaded => {
|
return [];
|
||||||
if (error) {
|
}).then(results => {
|
||||||
return error_route().then(({ default: Page }: { default: ComponentConstructor }) => {
|
if (redirect) {
|
||||||
const err = error.message instanceof Error ? error.message : new Error(error.message);
|
return { redirect };
|
||||||
Object.assign(props, { status: error.statusCode, error: err });
|
|
||||||
return { Page, props, redirect: null };
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(props, preloaded)
|
segments = new_segments;
|
||||||
return { Page, props, redirect };
|
|
||||||
|
const get_params = page.parts[page.parts.length - 1].params || (() => ({}));
|
||||||
|
const params = get_params(target.match);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const props = {
|
||||||
|
path,
|
||||||
|
query,
|
||||||
|
params,
|
||||||
|
error: typeof error.message === 'string' ? new Error(error.message) : error.message,
|
||||||
|
status: error.statusCode
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: Object.assign({}, props, {
|
||||||
|
preloading: false,
|
||||||
|
child: {
|
||||||
|
component: routes.error,
|
||||||
|
props
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = { path, query };
|
||||||
|
const data = {
|
||||||
|
path,
|
||||||
|
preloading: false,
|
||||||
|
child: Object.assign({}, root_props.child)
|
||||||
|
};
|
||||||
|
if (changed(query, root_props.query)) data.query = query;
|
||||||
|
if (changed(params, root_props.params)) data.params = params;
|
||||||
|
|
||||||
|
let level = data.child;
|
||||||
|
for (let i = 0; i < page.parts.length; i += 1) {
|
||||||
|
const part = page.parts[i];
|
||||||
|
const get_params = part.params || (() => ({}));
|
||||||
|
|
||||||
|
if (i < changed_from) {
|
||||||
|
level.props.path = path;
|
||||||
|
level.props.query = query;
|
||||||
|
level.props.child = Object.assign({}, level.props.child);
|
||||||
|
} else {
|
||||||
|
level.segment = new_segments[i];
|
||||||
|
level.component = results[i].Component;
|
||||||
|
level.props = Object.assign({}, level.props, props, {
|
||||||
|
params: get_params(target.match),
|
||||||
|
}, results[i].preloaded);
|
||||||
|
level.props.child = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
level = level.props.child;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { data, changed_from };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function navigate(target: Target, id: number): Promise<any> {
|
async function navigate(target: Target, id: number): Promise<any> {
|
||||||
if (id) {
|
if (id) {
|
||||||
// popstate or initial navigation
|
// popstate or initial navigation
|
||||||
cid = id;
|
cid = id;
|
||||||
@@ -148,20 +260,19 @@ function navigate(target: Target, id: number): Promise<any> {
|
|||||||
|
|
||||||
const loaded = prefetching && prefetching.href === target.url.href ?
|
const loaded = prefetching && prefetching.href === target.url.href ?
|
||||||
prefetching.promise :
|
prefetching.promise :
|
||||||
target.route.load().then(mod => prepare_route(mod.default, target.props));
|
prepare_page(target);
|
||||||
|
|
||||||
prefetching = null;
|
prefetching = null;
|
||||||
|
|
||||||
const token = current_token = {};
|
const token = current_token = {};
|
||||||
|
const { redirect, data, changed_from } = await loaded;
|
||||||
|
|
||||||
return loaded.then(({ Page, props, redirect }) => {
|
if (redirect) {
|
||||||
if (redirect) {
|
await goto(redirect.location, { replaceState: true });
|
||||||
return goto(redirect.location, { replaceState: true });
|
} else {
|
||||||
}
|
render(data, changed_from, scroll_history[id], token);
|
||||||
|
|
||||||
render(Page, props, scroll_history[id], token);
|
|
||||||
document.activeElement.blur();
|
document.activeElement.blur();
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_click(event: MouseEvent) {
|
function handle_click(event: MouseEvent) {
|
||||||
@@ -225,16 +336,16 @@ function handle_popstate(event: PopStateEvent) {
|
|||||||
|
|
||||||
let prefetching: {
|
let prefetching: {
|
||||||
href: string;
|
href: string;
|
||||||
promise: Promise<{ Page: ComponentConstructor, props: any }>;
|
promise: Promise<{ redirect?: Redirect, data?: any, changed_from?: number }>;
|
||||||
} = null;
|
} = null;
|
||||||
|
|
||||||
export function prefetch(href: string) {
|
export function prefetch(href: string) {
|
||||||
const selected = select_route(new URL(href, document.baseURI));
|
const target: Target = select_route(new URL(href, document.baseURI));
|
||||||
|
|
||||||
if (selected && (!prefetching || href !== prefetching.href)) {
|
if (target && (!prefetching || href !== prefetching.href)) {
|
||||||
prefetching = {
|
prefetching = {
|
||||||
href,
|
href,
|
||||||
promise: selected.route.load().then(mod => prepare_route(mod.default, selected.props))
|
promise: prepare_page(target)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,19 +367,18 @@ function trigger_prefetch(event: MouseEvent | TouchEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let inited: boolean;
|
let inited: boolean;
|
||||||
|
let ready = false;
|
||||||
|
|
||||||
export function init(opts: { App: ComponentConstructor, target: Node, routes: Route[], store?: (data: any) => Store }) {
|
export function init(opts: { App: ComponentConstructor, target: Node, routes: Routes, store?: (data: any) => Store }) {
|
||||||
if (opts instanceof HTMLElement) {
|
if (opts instanceof HTMLElement) {
|
||||||
throw new Error(`The signature of init(...) has changed — see https://sapper.svelte.technology/guide#0-11-to-0-12 for more information`);
|
throw new Error(`The signature of init(...) has changed — see https://sapper.svelte.technology/guide#0-11-to-0-12 for more information`);
|
||||||
}
|
}
|
||||||
|
|
||||||
App = opts.App;
|
|
||||||
target = opts.target;
|
target = opts.target;
|
||||||
routes = opts.routes;
|
routes = opts.routes;
|
||||||
error_route = opts.routes.error;
|
|
||||||
|
|
||||||
if (opts && opts.store) {
|
if (opts && opts.store) {
|
||||||
store = opts.store(manifest.store);
|
store = opts.store(initial_data.store);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!inited) { // this check makes HMR possible
|
if (!inited) { // this check makes HMR possible
|
||||||
|
|||||||
@@ -11,15 +11,23 @@ export interface ComponentConstructor {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface Component {
|
export interface Component {
|
||||||
|
set: (data: any) => void;
|
||||||
destroy: () => void;
|
destroy: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Route = {
|
export type Page = {
|
||||||
pattern: RegExp;
|
pattern: RegExp;
|
||||||
load: () => Promise<{ default: ComponentConstructor }>;
|
parts: Array<{
|
||||||
error?: boolean;
|
component: () => Promise<{ default: ComponentConstructor }>;
|
||||||
params?: (match: RegExpExecArray) => Record<string, string>;
|
params?: (match: RegExpExecArray) => Record<string, string>;
|
||||||
ignore?: boolean;
|
}>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Routes = {
|
||||||
|
ignore: RegExp[];
|
||||||
|
root: ComponentConstructor;
|
||||||
|
error: () => Promise<{ default: ComponentConstructor }>;
|
||||||
|
pages: Page[]
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ScrollPosition = {
|
export type ScrollPosition = {
|
||||||
@@ -29,6 +37,13 @@ export type ScrollPosition = {
|
|||||||
|
|
||||||
export type Target = {
|
export type Target = {
|
||||||
url: URL;
|
url: URL;
|
||||||
route: Route;
|
path: string;
|
||||||
props: RouteData;
|
page: Page;
|
||||||
|
match: RegExpExecArray;
|
||||||
|
query: Record<string, string | true>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Redirect = {
|
||||||
|
statusCode: number;
|
||||||
|
location: string;
|
||||||
};
|
};
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
{#if preloading}
|
|
||||||
<progress class='preloading-progress' value=0.5/>
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<svelte:component this={Page} {...props}/>
|
|
||||||
|
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
import { init, prefetchRoutes } from '../../../runtime.js';
|
import { init, prefetchRoutes } from '../../../runtime.js';
|
||||||
import { Store } from 'svelte/store.js';
|
import { Store } from 'svelte/store.js';
|
||||||
import { routes } from './manifest/client.js';
|
import { routes } from './manifest/client.js';
|
||||||
import App from './App.html';
|
|
||||||
|
|
||||||
window.init = () => {
|
window.init = () => {
|
||||||
return init({
|
return init({
|
||||||
target: document.querySelector('#sapper'),
|
target: document.querySelector('#sapper'),
|
||||||
App,
|
|
||||||
routes,
|
routes,
|
||||||
store: data => new Store(data)
|
store: data => new Store(data)
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import serve from 'serve-static';
|
|||||||
import sapper from '../../../dist/middleware.ts.js';
|
import sapper from '../../../dist/middleware.ts.js';
|
||||||
import { Store } from 'svelte/store.js';
|
import { Store } from 'svelte/store.js';
|
||||||
import { routes } from './manifest/server.js';
|
import { routes } from './manifest/server.js';
|
||||||
import App from './App.html'
|
|
||||||
|
|
||||||
let pending;
|
let pending;
|
||||||
let ended;
|
let ended;
|
||||||
@@ -87,7 +86,6 @@ const middlewares = [
|
|||||||
},
|
},
|
||||||
|
|
||||||
sapper({
|
sapper({
|
||||||
App,
|
|
||||||
routes,
|
routes,
|
||||||
store: () => {
|
store: () => {
|
||||||
return new Store({
|
return new Store({
|
||||||
|
|||||||
20
test/app/routes/[x]/[y]/[z].html
Normal file
20
test/app/routes/[x]/[y]/[z].html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<span>z: {segment} {count}</span>
|
||||||
|
<a href="foo/bar/qux"></a>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import counts from '../_counts.js';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
preload() {
|
||||||
|
return {
|
||||||
|
count: counts.z += 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
oncreate() {
|
||||||
|
this.set({
|
||||||
|
segment: this.get().params.z
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
20
test/app/routes/[x]/[y]/index.html
Normal file
20
test/app/routes/[x]/[y]/index.html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<span>y: {segment} {count}</span>
|
||||||
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import counts from '../_counts.js';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
preload() {
|
||||||
|
return {
|
||||||
|
count: counts.y += 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
oncreate() {
|
||||||
|
this.set({
|
||||||
|
segment: this.get().params.y
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
5
test/app/routes/[x]/_counts.js
Normal file
5
test/app/routes/[x]/_counts.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export default {
|
||||||
|
x: process.browser ? 1 : 0,
|
||||||
|
y: process.browser ? 1 : 0,
|
||||||
|
z: process.browser ? 1 : 0
|
||||||
|
};
|
||||||
20
test/app/routes/[x]/index.html
Normal file
20
test/app/routes/[x]/index.html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<span>x: {segment} {count}</span>
|
||||||
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import counts from './_counts.js';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
preload() {
|
||||||
|
return {
|
||||||
|
count: counts.x += 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
oncreate() {
|
||||||
|
this.set({
|
||||||
|
segment: this.get().params.x
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
26
test/app/routes/_default.html
Normal file
26
test/app/routes/_default.html
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<svelte:head>
|
||||||
|
<title>Sapper project template</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
|
<h1>Great success!</h1>
|
||||||
|
|
||||||
|
<a href='.'>home</a>
|
||||||
|
<a href='about'>about</a>
|
||||||
|
<a href='slow-preload'>slow preload</a>
|
||||||
|
<a href='redirect-from'>redirect</a>
|
||||||
|
<a href='blog/nope'>broken link</a>
|
||||||
|
<a href='blog/throw-an-error'>error link</a>
|
||||||
|
<a href='credentials?creds=include'>credentials</a>
|
||||||
|
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
|
||||||
|
|
||||||
|
<div class='hydrate-test'></div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 2.8em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-weight: 700;
|
||||||
|
margin: 0 0 0.5em 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
25
test/app/routes/blog/_default.html
Normal file
25
test/app/routes/blog/_default.html
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<svelte:head>
|
||||||
|
<title>Blog</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
|
<h1>Recent posts</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{#each posts as post}
|
||||||
|
<!-- we're using the non-standard `rel=prefetch` attribute to
|
||||||
|
tell Sapper to load the data for the page as soon as
|
||||||
|
the user hovers over the link or taps it, instead of
|
||||||
|
waiting for the 'click' event -->
|
||||||
|
<li><a rel='prefetch' href='blog/{post.slug}'>{post.title}</a></li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
preload({ params, query }) {
|
||||||
|
return fetch(`blog.json`).then(r => r.json()).then(posts => {
|
||||||
|
return { posts };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
@@ -1,25 +1 @@
|
|||||||
<svelte:head>
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
<title>Blog</title>
|
|
||||||
</svelte:head>
|
|
||||||
|
|
||||||
<h1>Recent posts</h1>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
{#each posts as post}
|
|
||||||
<!-- we're using the non-standard `rel=prefetch` attribute to
|
|
||||||
tell Sapper to load the data for the page as soon as
|
|
||||||
the user hovers over the link or taps it, instead of
|
|
||||||
waiting for the 'click' event -->
|
|
||||||
<li><a rel='prefetch' href='blog/{post.slug}'>{post.title}</a></li>
|
|
||||||
{/each}
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
preload({ params, query }) {
|
|
||||||
return fetch(`blog.json`).then(r => r.json()).then(posts => {
|
|
||||||
return { posts };
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
@@ -1,26 +1,6 @@
|
|||||||
<svelte:head>
|
{#if preloading}
|
||||||
<title>Sapper project template</title>
|
<progress class='preloading-progress' value=0.5/>
|
||||||
</svelte:head>
|
{/if}
|
||||||
|
|
||||||
<h1>Great success!</h1>
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
|
|
||||||
<a href='.'>home</a>
|
|
||||||
<a href='about'>about</a>
|
|
||||||
<a href='slow-preload'>slow preload</a>
|
|
||||||
<a href='redirect-from'>redirect</a>
|
|
||||||
<a href='blog/nope'>broken link</a>
|
|
||||||
<a href='blog/throw-an-error'>error link</a>
|
|
||||||
<a href='credentials?creds=include'>credentials</a>
|
|
||||||
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
|
|
||||||
|
|
||||||
<div class='hydrate-test'></div>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
h1 {
|
|
||||||
text-align: center;
|
|
||||||
font-size: 2.8em;
|
|
||||||
text-transform: uppercase;
|
|
||||||
font-weight: 700;
|
|
||||||
margin: 0 0 0.5em 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
1
test/app/routes/missing-index/ok.html
Normal file
1
test/app/routes/missing-index/ok.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>it works</h1>
|
||||||
1
test/app/routes/preload-values/index.html
Normal file
1
test/app/routes/preload-values/index.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svelte:component this={child.component} {...child.props}/>
|
||||||
@@ -32,6 +32,8 @@ Nightmare.action('prefetchRoutes', function(done) {
|
|||||||
|
|
||||||
const cli = path.resolve(__dirname, '../../sapper');
|
const cli = path.resolve(__dirname, '../../sapper');
|
||||||
|
|
||||||
|
const wait = ms => new Promise(f => setTimeout(f, ms));
|
||||||
|
|
||||||
describe('sapper', function() {
|
describe('sapper', function() {
|
||||||
process.chdir(path.resolve(__dirname, '../app'));
|
process.chdir(path.resolve(__dirname, '../app'));
|
||||||
|
|
||||||
@@ -41,7 +43,7 @@ describe('sapper', function() {
|
|||||||
rimraf.sync('.sapper');
|
rimraf.sync('.sapper');
|
||||||
rimraf.sync('start.js');
|
rimraf.sync('start.js');
|
||||||
|
|
||||||
this.timeout(process.env.CI ? 30000 : 10000);
|
this.timeout(process.env.CI ? 30000 : 15000);
|
||||||
|
|
||||||
// TODO reinstate dev tests
|
// TODO reinstate dev tests
|
||||||
// run({
|
// run({
|
||||||
@@ -97,13 +99,13 @@ describe('sapper', function() {
|
|||||||
];
|
];
|
||||||
// Client scripts that should show up in the extraction directory.
|
// Client scripts that should show up in the extraction directory.
|
||||||
const expectedClientRegexes = [
|
const expectedClientRegexes = [
|
||||||
/client\/[^/]+\/_(\.\d+)?\.js/,
|
|
||||||
/client\/[^/]+\/about(\.\d+)?\.js/,
|
|
||||||
/client\/[^/]+\/blog_\$slug\$(\.\d+)?\.js/,
|
|
||||||
/client\/[^/]+\/blog(\.\d+)?\.js/,
|
|
||||||
/client\/[^/]+\/main(\.\d+)?\.js/,
|
/client\/[^/]+\/main(\.\d+)?\.js/,
|
||||||
/client\/[^/]+\/show_url(\.\d+)?\.js/,
|
/client\/[^/]+\/page_index(\.\d+)?\.js/,
|
||||||
/client\/[^/]+\/slow_preload(\.\d+)?\.js/,
|
/client\/[^/]+\/page_about(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/page_blog_\$slug(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/page_blog(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/page_show\$45url(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/page_slow\$45preload(\.\d+)?\.js/,
|
||||||
];
|
];
|
||||||
const allPages = walkSync(dest);
|
const allPages = walkSync(dest);
|
||||||
|
|
||||||
@@ -266,8 +268,9 @@ function run({ mode, basepath = '' }) {
|
|||||||
})
|
})
|
||||||
.then(requests => {
|
.then(requests => {
|
||||||
assert.deepEqual(requests.map(r => r.url), []);
|
assert.deepEqual(requests.map(r => r.url), []);
|
||||||
return nightmare.path();
|
|
||||||
})
|
})
|
||||||
|
.then(() => wait(100))
|
||||||
|
.then(() => nightmare.path())
|
||||||
.then(path => {
|
.then(path => {
|
||||||
assert.equal(path, `${basepath}/about`);
|
assert.equal(path, `${basepath}/about`);
|
||||||
return nightmare.title();
|
return nightmare.title();
|
||||||
@@ -629,6 +632,41 @@ function run({ mode, basepath = '' }) {
|
|||||||
assert.equal(html.indexOf('%sapper'), -1);
|
assert.equal(html.indexOf('%sapper'), -1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('only recreates components when necessary', () => {
|
||||||
|
return nightmare
|
||||||
|
.goto(`${base}/foo/bar/baz`)
|
||||||
|
.init()
|
||||||
|
.evaluate(() => document.querySelector('#sapper').textContent)
|
||||||
|
.then(text => {
|
||||||
|
assert.deepEqual(text.split('\n').filter(Boolean), [
|
||||||
|
'x: foo 1',
|
||||||
|
'y: bar 1',
|
||||||
|
'z: baz 1'
|
||||||
|
]);
|
||||||
|
|
||||||
|
return nightmare.click(`a`)
|
||||||
|
.then(() => wait(100))
|
||||||
|
.then(() => {
|
||||||
|
return nightmare.evaluate(() => document.querySelector('#sapper').textContent);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(text => {
|
||||||
|
assert.deepEqual(text.split('\n').filter(Boolean), [
|
||||||
|
'x: foo 1',
|
||||||
|
'y: bar 1',
|
||||||
|
'z: qux 2'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses a fallback index component if none is provided', () => {
|
||||||
|
return nightmare.goto(`${base}/missing-index/ok`)
|
||||||
|
.page.title()
|
||||||
|
.then(title => {
|
||||||
|
assert.equal(title, 'it works');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('headers', () => {
|
describe('headers', () => {
|
||||||
@@ -641,7 +679,7 @@ function run({ mode, basepath = '' }) {
|
|||||||
'text/html'
|
'text/html'
|
||||||
);
|
);
|
||||||
|
|
||||||
const str = ['main', '_\\.\\d+']
|
const str = ['main', '.+?\\.\\d+']
|
||||||
.map(file => {
|
.map(file => {
|
||||||
return `<${basepath}/client/[^/]+/${file}\\.js>;rel="preload";as="script"`;
|
return `<${basepath}/client/[^/]+/${file}\\.js>;rel="preload";as="script"`;
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,366 +0,0 @@
|
|||||||
const assert = require('assert');
|
|
||||||
const { create_routes } = require('../../dist/core.ts.js');
|
|
||||||
|
|
||||||
describe('create_routes', () => {
|
|
||||||
it('encodes characters not allowed in path', () => {
|
|
||||||
const { server_routes } = create_routes({
|
|
||||||
files: [
|
|
||||||
'"',
|
|
||||||
'#',
|
|
||||||
'?'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
server_routes.map(r => r.pattern),
|
|
||||||
[
|
|
||||||
/^\/%22\/?$/,
|
|
||||||
/^\/%23\/?$/,
|
|
||||||
/^\/%3F\/?$/
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sorts routes correctly', () => {
|
|
||||||
const { pages, server_routes } = create_routes({
|
|
||||||
files: [
|
|
||||||
'index.html',
|
|
||||||
'about.html',
|
|
||||||
'post/f[xx].html',
|
|
||||||
'[wildcard].html',
|
|
||||||
'post/foo.html',
|
|
||||||
'post/[id].html',
|
|
||||||
'post/bar.html',
|
|
||||||
'post/[id].json.js',
|
|
||||||
'post/[id([0-9-a-z]{3,})].html',
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'index.html',
|
|
||||||
'about.html',
|
|
||||||
'post/bar.html',
|
|
||||||
'post/foo.html',
|
|
||||||
'post/f[xx].html',
|
|
||||||
'post/[id([0-9-a-z]{3,})].html', // RegExp is more specific
|
|
||||||
'post/[id].html',
|
|
||||||
'[wildcard].html'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
server_routes.map(r => r.file),
|
|
||||||
[
|
|
||||||
'post/[id].json.js'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('distinguishes and sorts regexp routes correctly', () => {
|
|
||||||
const { pages } = create_routes({
|
|
||||||
files: [
|
|
||||||
'[slug].html',
|
|
||||||
'[slug([a-z]{2})].html',
|
|
||||||
'[slug([0-9-a-z]{3,})].html',
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'[slug([0-9-a-z]{3,})].html',
|
|
||||||
'[slug([a-z]{2})].html',
|
|
||||||
'[slug].html',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('prefers index page to nested route', () => {
|
|
||||||
let { pages, server_routes } = create_routes({
|
|
||||||
files: [
|
|
||||||
'api/examples/[slug].js',
|
|
||||||
'api/examples/index.js',
|
|
||||||
'blog/[slug].html',
|
|
||||||
'api/gists/[id].js',
|
|
||||||
'api/gists/index.js',
|
|
||||||
'_error.html',
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/rss.xml.js',
|
|
||||||
'guide/index.html',
|
|
||||||
'index.html'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'_error.html',
|
|
||||||
'index.html',
|
|
||||||
'guide/index.html',
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/[slug].html'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
server_routes.map(r => r.file),
|
|
||||||
[
|
|
||||||
'blog/rss.xml.js',
|
|
||||||
'api/examples/index.js',
|
|
||||||
'api/examples/[slug].js',
|
|
||||||
'api/gists/index.js',
|
|
||||||
'api/gists/[id].js',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
({ pages, server_routes } = create_routes({
|
|
||||||
files: [
|
|
||||||
'_error.html',
|
|
||||||
'api/blog/[slug].js',
|
|
||||||
'api/blog/index.js',
|
|
||||||
'api/guide/contents.js',
|
|
||||||
'api/guide/index.js',
|
|
||||||
'blog/[slug].html',
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/rss.xml.js',
|
|
||||||
'gist/[id].js',
|
|
||||||
'gist/create.js',
|
|
||||||
'guide/index.html',
|
|
||||||
'index.html',
|
|
||||||
'repl/index.html'
|
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'_error.html',
|
|
||||||
'index.html',
|
|
||||||
'guide/index.html',
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/[slug].html',
|
|
||||||
'repl/index.html'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
server_routes.map(r => r.file),
|
|
||||||
[
|
|
||||||
'blog/rss.xml.js',
|
|
||||||
'gist/create.js',
|
|
||||||
'gist/[id].js',
|
|
||||||
'api/guide/index.js',
|
|
||||||
'api/guide/contents.js',
|
|
||||||
'api/blog/index.js',
|
|
||||||
'api/blog/[slug].js',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// RegExp routes
|
|
||||||
({ pages } = create_routes({
|
|
||||||
files: [
|
|
||||||
'blog/[slug].html',
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/[slug([^0-9]+)].html',
|
|
||||||
]
|
|
||||||
}));
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'blog/index.html',
|
|
||||||
'blog/[slug([^0-9]+)].html',
|
|
||||||
'blog/[slug].html',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('generates params', () => {
|
|
||||||
const { pages } = create_routes({
|
|
||||||
files: ['index.html', 'about.html', '[wildcard].html', 'post/[id].html']
|
|
||||||
});
|
|
||||||
|
|
||||||
let file;
|
|
||||||
let params;
|
|
||||||
for (let i = 0; i < pages.length; i += 1) {
|
|
||||||
const route = pages[i];
|
|
||||||
if (params = route.exec('/post/123')) {
|
|
||||||
file = route.file;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.equal(file, 'post/[id].html');
|
|
||||||
assert.deepEqual(params, {
|
|
||||||
id: '123'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('ignores files and directories with leading underscores', () => {
|
|
||||||
const { pages } = create_routes({
|
|
||||||
files: ['index.html', '_foo.html', 'a/_b/c/d.html', 'e/f/g/h.html', 'i/_j.html']
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
[
|
|
||||||
'index.html',
|
|
||||||
'e/f/g/h.html'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('ignores files and directories with leading dots except .well-known', () => {
|
|
||||||
const { server_routes } = create_routes({
|
|
||||||
files: ['.well-known/dnt-policy.txt.js', '.unknown/foo.txt.js']
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
server_routes.map(r => r.file),
|
|
||||||
['.well-known/dnt-policy.txt.js']
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('matches /foo/:bar before /:baz/qux', () => {
|
|
||||||
const a = create_routes({
|
|
||||||
files: ['foo/[bar].html', '[baz]/qux.html']
|
|
||||||
});
|
|
||||||
const b = create_routes({
|
|
||||||
files: ['[baz]/qux.html', 'foo/[bar].html']
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
a.pages.map(r => r.file),
|
|
||||||
['foo/[bar].html', '[baz]/qux.html']
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
b.pages.map(r => r.file),
|
|
||||||
['foo/[bar].html', '[baz]/qux.html']
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails if routes are indistinguishable', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['[foo].html', '[bar]/index.html']
|
|
||||||
});
|
|
||||||
}, /The \[foo\] and \[bar\]\/index routes clash/);
|
|
||||||
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['[foo([0-9-a-z]+)].html', '[bar([0-9-a-z]+)]/index.html']
|
|
||||||
});
|
|
||||||
}, /The \[foo\(\[0-9-a-z\]\+\)\] and \[bar\(\[0-9-a-z\]\+\)\]\/index routes clash/);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
it('matches nested routes', () => {
|
|
||||||
const page = create_routes({
|
|
||||||
files: ['settings/[submenu].html']
|
|
||||||
}).pages[0];
|
|
||||||
|
|
||||||
assert.deepEqual(page.exec('/settings/foo'), {
|
|
||||||
submenu: 'foo'
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(page.exec('/settings'), {
|
|
||||||
submenu: null
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('prefers index routes to nested routes', () => {
|
|
||||||
const { pages } = create_routes({
|
|
||||||
files: ['settings/[submenu].html', 'settings.html']
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(
|
|
||||||
pages.map(r => r.file),
|
|
||||||
['settings.html', 'settings/[submenu].html']
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('matches deeply nested routes', () => {
|
|
||||||
const page = create_routes({
|
|
||||||
files: ['settings/[a]/[b]/index.html']
|
|
||||||
}).pages[0];
|
|
||||||
|
|
||||||
assert.deepEqual(page.exec('/settings/foo/bar'), {
|
|
||||||
a: 'foo',
|
|
||||||
b: 'bar'
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(page.exec('/settings/foo'), {
|
|
||||||
a: 'foo',
|
|
||||||
b: null
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.deepEqual(page.exec('/settings'), {
|
|
||||||
a: null,
|
|
||||||
b: null
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('matches a dynamic part within a part', () => {
|
|
||||||
const route = create_routes({
|
|
||||||
files: ['things/[slug].json.js']
|
|
||||||
}).server_routes[0];
|
|
||||||
|
|
||||||
assert.deepEqual(route.exec('/things/foo.json'), {
|
|
||||||
slug: 'foo'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('matches multiple dynamic parts within a part', () => {
|
|
||||||
const route = create_routes({
|
|
||||||
files: ['things/[id]_[slug].json.js']
|
|
||||||
}).server_routes[0];
|
|
||||||
|
|
||||||
assert.deepEqual(route.exec('/things/someid_someslug.json'), {
|
|
||||||
id: 'someid',
|
|
||||||
slug: 'someslug'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails if dynamic params are not separated', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['[foo][bar].js']
|
|
||||||
});
|
|
||||||
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('errors when trying to use reserved characters in route regexp', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['[lang([a-z]{2}(?:-[a-z]{2,4})?)]']
|
|
||||||
});
|
|
||||||
}, /Sapper does not allow \(, \), \? or \: in RegExp routes yet/);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('errors on 4xx.html', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['4xx.html']
|
|
||||||
});
|
|
||||||
}, /As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html/);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('errors on 5xx.html', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
create_routes({
|
|
||||||
files: ['5xx.html']
|
|
||||||
});
|
|
||||||
}, /As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html/);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('treats foo/index.json.js the same as foo.json.js', () => {
|
|
||||||
const route = create_routes({
|
|
||||||
files: ['foo/index.json.js']
|
|
||||||
}).server_routes[0];
|
|
||||||
|
|
||||||
assert.ok(route.test('/foo.json'));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
159
test/unit/create_routes/index.js
Normal file
159
test/unit/create_routes/index.js
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const assert = require('assert');
|
||||||
|
const { create_routes } = require('../../../dist/core.ts.js');
|
||||||
|
|
||||||
|
describe('create_routes', () => {
|
||||||
|
it('creates routes', () => {
|
||||||
|
const { components, pages, server_routes } = create_routes(path.join(__dirname, 'samples/basic'));
|
||||||
|
|
||||||
|
const page_index = { name: 'page_index', file: '_default.html' };
|
||||||
|
const page_about = { name: 'page_about', file: 'about.html' };
|
||||||
|
const page_blog = { name: 'page_blog', file: 'blog/index.html' };
|
||||||
|
const page_blog_index = { name: 'page_blog_index', file: 'blog/_default.html' };
|
||||||
|
const page_blog_$slug = { name: 'page_blog_$slug', file: 'blog/[slug].html' };
|
||||||
|
|
||||||
|
assert.deepEqual(components, [
|
||||||
|
page_index,
|
||||||
|
page_about,
|
||||||
|
page_blog,
|
||||||
|
page_blog_index,
|
||||||
|
page_blog_$slug
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(pages, [
|
||||||
|
{
|
||||||
|
pattern: /^\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: page_index, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/about\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: page_about, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/blog\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: page_blog, params: [] },
|
||||||
|
{ component: page_blog_index, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/blog\/([^\/]+?)\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: page_blog, params: [] },
|
||||||
|
{ component: page_blog_$slug, params: ['slug'] }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(server_routes, [
|
||||||
|
{
|
||||||
|
name: 'route_blog_json',
|
||||||
|
pattern: /^\/blog.json\/?$/,
|
||||||
|
file: 'blog/index.json.js',
|
||||||
|
params: []
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'route_blog_$slug_json',
|
||||||
|
pattern: /^\/blog\/([^\/]+?).json\/?$/,
|
||||||
|
file: 'blog/[slug].json.js',
|
||||||
|
params: ['slug']
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes invalid characters', () => {
|
||||||
|
const { components, pages } = create_routes(path.join(__dirname, 'samples/encoding'));
|
||||||
|
|
||||||
|
// had to remove ? and " because windows
|
||||||
|
|
||||||
|
// const quote = { name: 'page_$34', file: '".html' };
|
||||||
|
const hash = { name: 'page_$35', file: '#.html' };
|
||||||
|
// const question_mark = { name: 'page_$63', file: '?.html' };
|
||||||
|
|
||||||
|
assert.deepEqual(components, [
|
||||||
|
// quote,
|
||||||
|
hash,
|
||||||
|
// question_mark
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(pages.map(p => p.pattern), [
|
||||||
|
// /^\/%22\/?$/,
|
||||||
|
/^\/%23\/?$/,
|
||||||
|
// /^\/%3F\/?$/
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows regex qualifiers', () => {
|
||||||
|
const { pages } = create_routes(path.join(__dirname, 'samples/qualifiers'));
|
||||||
|
|
||||||
|
assert.deepEqual(pages.map(p => p.pattern), [
|
||||||
|
/^\/([0-9-a-z]{3,})\/?$/,
|
||||||
|
/^\/([a-z]{2})\/?$/,
|
||||||
|
/^\/([^\/]+?)\/?$/
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sorts routes correctly', () => {
|
||||||
|
const { pages } = create_routes(path.join(__dirname, 'samples/sorting'));
|
||||||
|
|
||||||
|
assert.deepEqual(pages.map(p => p.parts.map(part => part.component.file)), [
|
||||||
|
['_default.html'],
|
||||||
|
['about.html'],
|
||||||
|
['post/index.html', 'post/_default.html'],
|
||||||
|
['post/index.html', 'post/bar.html'],
|
||||||
|
['post/index.html', 'post/foo.html'],
|
||||||
|
['post/index.html', 'post/f[xx].html'],
|
||||||
|
['post/index.html', 'post/[id([0-9-a-z]{3,})].html'],
|
||||||
|
['post/index.html', 'post/[id].html'],
|
||||||
|
['[wildcard].html']
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores files and directories with leading underscores', () => {
|
||||||
|
const { server_routes } = create_routes(path.join(__dirname, 'samples/hidden-underscore'));
|
||||||
|
|
||||||
|
assert.deepEqual(server_routes.map(r => r.file), [
|
||||||
|
'index.js',
|
||||||
|
'e/f/g/h.js'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores files and directories with leading dots except .well-known', () => {
|
||||||
|
const { server_routes } = create_routes(path.join(__dirname, 'samples/hidden-dot'));
|
||||||
|
|
||||||
|
assert.deepEqual(server_routes.map(r => r.file), [
|
||||||
|
'.well-known/dnt-policy.txt.js'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails on clashes', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
const { pages } = create_routes(path.join(__dirname, 'samples/clash-pages'));
|
||||||
|
}, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/);
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
const { server_routes } = create_routes(path.join(__dirname, 'samples/clash-routes'));
|
||||||
|
console.log(server_routes);
|
||||||
|
}, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails if dynamic params are not separated', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes(path.join(__dirname, 'samples/invalid-params'));
|
||||||
|
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('errors when trying to use reserved characters in route regexp', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes(path.join(__dirname, 'samples/invalid-qualifier'));
|
||||||
|
}, /Invalid route \[foo\(\[a-z\]\(\[0-9\]\)\)\].js — cannot use \(, \), \? or \: in route qualifiers/);
|
||||||
|
});
|
||||||
|
});
|
||||||
0
test/unit/create_routes/samples/basic/_default.html
Normal file
0
test/unit/create_routes/samples/basic/_default.html
Normal file
0
test/unit/create_routes/samples/basic/about.html
Normal file
0
test/unit/create_routes/samples/basic/about.html
Normal file
0
test/unit/create_routes/samples/basic/index.html
Normal file
0
test/unit/create_routes/samples/basic/index.html
Normal file
0
test/unit/create_routes/samples/encoding/#.html
Normal file
0
test/unit/create_routes/samples/encoding/#.html
Normal file
0
test/unit/create_routes/samples/encoding/index.html
Normal file
0
test/unit/create_routes/samples/encoding/index.html
Normal file
0
test/unit/create_routes/samples/sorting/about.html
Normal file
0
test/unit/create_routes/samples/sorting/about.html
Normal file
0
test/unit/create_routes/samples/sorting/index.html
Normal file
0
test/unit/create_routes/samples/sorting/index.html
Normal file
Reference in New Issue
Block a user