mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-11 19:04:30 +00:00
@@ -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
components/default-layout.html
Normal file
1
components/default-layout.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
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
"runtime",
|
"runtime",
|
||||||
"webpack",
|
"webpack",
|
||||||
"sapper",
|
"sapper",
|
||||||
|
"components",
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"directories": {
|
"directories": {
|
||||||
@@ -67,7 +68,7 @@
|
|||||||
"cy:open": "cypress open",
|
"cy:open": "cypress open",
|
||||||
"test": "mocha --opts mocha.opts",
|
"test": "mocha --opts mocha.opts",
|
||||||
"pretest": "npm run build",
|
"pretest": "npm run build",
|
||||||
"build": "rollup -c",
|
"build": "rm -rf dist && rollup -c",
|
||||||
"dev": "rollup -cw",
|
"dev": "rollup -cw",
|
||||||
"prepublishOnly": "npm test",
|
"prepublishOnly": "npm test",
|
||||||
"update_mime_types": "curl http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | grep -e \"^[^#]\" > src/middleware/mime-types.md"
|
"update_mime_types": "curl http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | grep -e \"^[^#]\" > src/middleware/mime-types.md"
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ export default [
|
|||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
typescript({
|
typescript({
|
||||||
typescript: require('typescript')
|
typescript: require('typescript'),
|
||||||
|
target: "ES2017"
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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());
|
||||||
@@ -14,12 +14,16 @@ export function create_main_manifests({ routes, dev_port }: {
|
|||||||
const client_manifest = generate_client(routes, path_to_routes, dev_port);
|
const client_manifest = generate_client(routes, path_to_routes, dev_port);
|
||||||
const server_manifest = generate_server(routes, path_to_routes);
|
const server_manifest = generate_server(routes, path_to_routes);
|
||||||
|
|
||||||
|
write_if_changed(
|
||||||
|
`${locations.app()}/manifest/default-layout.html`,
|
||||||
|
`<svelte:component this={child.component} {...child.props}/>`
|
||||||
|
);
|
||||||
write_if_changed(`${locations.app()}/manifest/client.js`, client_manifest);
|
write_if_changed(`${locations.app()}/manifest/client.js`, client_manifest);
|
||||||
write_if_changed(`${locations.app()}/manifest/server.js`, server_manifest);
|
write_if_changed(`${locations.app()}/manifest/server.js`, server_manifest);
|
||||||
}
|
}
|
||||||
|
|
||||||
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,42 +36,67 @@ 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: { root: PageComponent, 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!
|
||||||
export const routes = {
|
import root from '${posixify(`${path_to_routes}/${routes.root.file}`)}';
|
||||||
|
import error from '${posixify(`${path_to_routes}/_error.html`)}';
|
||||||
|
|
||||||
|
${routes.components.map(component =>
|
||||||
|
`const ${component.name} = () =>
|
||||||
|
import(/* webpackChunkName: "${component.name}" */ '${get_file(path_to_routes, component)}');`)
|
||||||
|
.join('\n')}
|
||||||
|
|
||||||
|
export const manifest = {
|
||||||
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,
|
||||||
};`.replace(/^\t\t/gm, '').trim();
|
|
||||||
|
error
|
||||||
|
};
|
||||||
|
|
||||||
|
// this is included for legacy reasons
|
||||||
|
export const routes = {};`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
if (dev()) {
|
if (dev()) {
|
||||||
const sapper_dev_client = posixify(
|
const sapper_dev_client = posixify(
|
||||||
@@ -86,47 +115,72 @@ 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: { root: PageComponent, 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 '${get_file(path_to_routes, component)}';`),
|
||||||
`import error from '${posixify(`${path_to_routes}/${error_route.file}`)}';`
|
`import root from '${posixify(`${path_to_routes}/${routes.root.file}`)}';`,
|
||||||
|
`import error from '${posixify(`${path_to_routes}/_error.html`)}';`
|
||||||
);
|
);
|
||||||
|
|
||||||
let code = `
|
let code = `
|
||||||
// This file is generated by Sapper — do not edit it!
|
// This file is generated by Sapper — do not edit it!
|
||||||
${imports.join('\n')}
|
${imports.join('\n')}
|
||||||
|
|
||||||
export const routes = {
|
export const manifest = {
|
||||||
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();
|
|
||||||
|
// this is included for legacy reasons
|
||||||
|
export const routes = {};`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
return code;
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_file(path_to_routes: string, component: PageComponent) {
|
||||||
|
if (component.default) {
|
||||||
|
return `./default-layout.html`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return posixify(`${path_to_routes}/${component.file}`);
|
||||||
}
|
}
|
||||||
@@ -1,186 +1,306 @@
|
|||||||
|
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({
|
const default_layout_file = posixify(path.resolve(
|
||||||
files
|
__dirname,
|
||||||
} = {
|
'../components/default-layout.html'
|
||||||
files: glob.sync('**/*.*', {
|
));
|
||||||
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') {
|
export default function create_routes(cwd = locations.routes()) {
|
||||||
throw new Error('As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html');
|
const components: PageComponent[] = [];
|
||||||
}
|
const pages: Page[] = [];
|
||||||
|
const server_routes: ServerRoute[] = [];
|
||||||
|
|
||||||
const base = file.replace(/\.[^/.]+$/, '');
|
const default_layout: PageComponent = {
|
||||||
const parts = base.split('/'); // glob output is always posix-style
|
default: true,
|
||||||
if (/^index(\..+)?/.test(parts[parts.length - 1])) {
|
name: '_default_layout',
|
||||||
const part = parts.pop();
|
file: null
|
||||||
if (parts.length > 0) parts[parts.length - 1] += part.slice(5);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
const id = (
|
function walk(
|
||||||
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
|
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 type = file.endsWith('.html') ? 'page' : 'route';
|
const segment = is_dir
|
||||||
|
? basename
|
||||||
|
: basename.slice(0, -path.extname(basename).length);
|
||||||
|
|
||||||
const params: string[] = [];
|
const parts = get_parts(segment);
|
||||||
const match_patterns: Record<string, string> = {};
|
const is_index = is_dir ? false : basename.startsWith('index.');
|
||||||
const param_pattern = /\[([^\(\]]+)(?:\((.+?)\))?\]/g;
|
const is_page = path.extname(basename) === '.html';
|
||||||
|
|
||||||
let match;
|
parts.forEach(part => {
|
||||||
while (match = param_pattern.exec(base)) {
|
if (/\]\[/.test(part.content)) {
|
||||||
params.push(match[1]);
|
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
||||||
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] === '_') return;
|
||||||
base,
|
|
||||||
type,
|
if (item.basename[0] === '.') {
|
||||||
file,
|
if (item.file !== '.well-known') return;
|
||||||
pattern,
|
}
|
||||||
test,
|
|
||||||
exec,
|
const segments = parent_segments.slice();
|
||||||
parts,
|
|
||||||
params
|
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, '_layout.html');
|
||||||
|
const layout = fs.existsSync(index)
|
||||||
|
? {
|
||||||
|
name: `${get_slug(item.file)}__layout`,
|
||||||
|
file: `${item.file}/_layout.html`
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (layout) {
|
||||||
|
components.push(layout);
|
||||||
|
} else if (components.indexOf(default_layout) === -1) {
|
||||||
|
components.push(default_layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(
|
||||||
|
path.join(dir, item.basename),
|
||||||
|
segments,
|
||||||
|
params,
|
||||||
|
stack.concat({
|
||||||
|
component: layout || default_layout,
|
||||||
|
params
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (item.is_page) {
|
||||||
|
const component = {
|
||||||
|
name: get_slug(item.file),
|
||||||
|
file: item.file
|
||||||
|
};
|
||||||
|
|
||||||
|
const parts = stack.concat({
|
||||||
|
component,
|
||||||
|
params
|
||||||
|
});
|
||||||
|
|
||||||
|
components.push(component);
|
||||||
|
if (item.basename === 'index.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
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const pages = all_routes
|
const root_file = path.join(cwd, '_layout.html');
|
||||||
.filter(r => r.type === 'page')
|
const root = fs.existsSync(root_file)
|
||||||
.sort(comparator);
|
? {
|
||||||
|
name: 'main',
|
||||||
|
file: '_layout.html'
|
||||||
|
}
|
||||||
|
: default_layout;
|
||||||
|
|
||||||
const server_routes = all_routes
|
walk(cwd, [], [], []);
|
||||||
.filter(r => r.type === 'route')
|
|
||||||
.sort(comparator);
|
|
||||||
|
|
||||||
return { pages, server_routes };
|
// check for clashes
|
||||||
|
const seen_pages: Map<string, Page> = new Map();
|
||||||
|
pages.forEach(page => {
|
||||||
|
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 {
|
||||||
|
root,
|
||||||
|
components,
|
||||||
|
pages,
|
||||||
|
server_routes
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function comparator(a, b) {
|
type Part = {
|
||||||
if (a.parts[0] === '_error') return -1;
|
content: string;
|
||||||
if (b.parts[0] === '_error') return 1;
|
dynamic: boolean;
|
||||||
|
qualifier?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function comparator(
|
||||||
|
a: { basename: string, parts: Part[], file: string, is_index: boolean },
|
||||||
|
b: { basename: string, parts: Part[], file: string, is_index: boolean }
|
||||||
|
) {
|
||||||
|
if (a.is_index !== b.is_index) return a.is_index ? -1 : 1;
|
||||||
|
|
||||||
const max = Math.max(a.parts.length, b.parts.length);
|
const max = Math.max(a.parts.length, b.parts.length);
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
for (let i = 0; i < max; i += 1) {
|
||||||
const a_part = a.parts[i];
|
const a_sub_part = a.parts[i];
|
||||||
const b_part = b.parts[i];
|
const b_sub_part = b.parts[i];
|
||||||
|
|
||||||
if (!a_part) return -1;
|
if (!a_sub_part) return 1; // b is more specific, so goes first
|
||||||
if (!b_part) return 1;
|
if (!b_sub_part) return -1;
|
||||||
|
|
||||||
const a_sub_parts = get_sub_parts(a_part);
|
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
|
||||||
const b_sub_parts = get_sub_parts(b_part);
|
return a_sub_part.dynamic ? 1 : -1;
|
||||||
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
|
}
|
||||||
|
|
||||||
for (let i = 0; i < max; i += 1) {
|
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
||||||
const a_sub_part = a_sub_parts[i];
|
return (
|
||||||
const b_sub_part = b_sub_parts[i];
|
(b_sub_part.content.length - a_sub_part.content.length) ||
|
||||||
|
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!a_sub_part) return 1; // b is more specific, so goes first
|
// If both parts dynamic, check for regexp patterns
|
||||||
if (!b_sub_part) return -1;
|
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_sub_part.dynamic !== b_sub_part.dynamic) {
|
if (!a_match && b_match) {
|
||||||
return a_sub_part.dynamic ? 1 : -1;
|
return 1; // No regexp, so less specific than b
|
||||||
}
|
}
|
||||||
|
if (!b_match && a_match) {
|
||||||
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
|
return -1;
|
||||||
return (
|
|
||||||
(b_sub_part.content.length - a_sub_part.content.length) ||
|
|
||||||
(a_sub_part.content < b_sub_part.content ? -1 : 1)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
if (a_match && b_match && a_match[1] !== b_match[1]) {
|
||||||
// If both parts dynamic, check for regexp patterns
|
return b_match[1].length - a_match[1].length;
|
||||||
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 = {
|
||||||
|
default?: 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 Manifest = {
|
||||||
|
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;
|
||||||
@@ -26,6 +39,20 @@ type Store = {
|
|||||||
get: () => any
|
get: () => any
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
path: string;
|
||||||
|
query: Record<string, string>;
|
||||||
|
params: Record<string, string>;
|
||||||
|
error?: { message: string };
|
||||||
|
status?: number;
|
||||||
|
child: {
|
||||||
|
segment: string;
|
||||||
|
component: Component;
|
||||||
|
props: Props;
|
||||||
|
};
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
|
||||||
interface Req extends ClientRequest {
|
interface Req extends ClientRequest {
|
||||||
url: string;
|
url: string;
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
@@ -33,6 +60,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,17 +73,19 @@ 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(opts: {
|
||||||
App: Component,
|
manifest: Manifest,
|
||||||
routes: RouteObject[],
|
store: (req: Req) => Store,
|
||||||
store: (req: Req) => Store
|
routes?: any // legacy
|
||||||
}) {
|
}) {
|
||||||
if (!App) {
|
if (opts.routes) {
|
||||||
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`);
|
throw new Error(`As of Sapper 0.15, opts.routes should be opts.manifest`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = locations.dest();
|
const output = locations.dest();
|
||||||
|
|
||||||
|
const { manifest, store } = opts;
|
||||||
|
|
||||||
let emitted_basepath = false;
|
let emitted_basepath = false;
|
||||||
|
|
||||||
const middleware = compose_handlers([
|
const middleware = compose_handlers([
|
||||||
@@ -108,8 +138,8 @@ export default function middleware({ App, routes, store }: {
|
|||||||
cache_control: 'max-age=31536000'
|
cache_control: 'max-age=31536000'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
get_server_route_handler(routes.server_routes),
|
get_server_route_handler(manifest.server_routes),
|
||||||
get_page_handler(App, routes, store)
|
get_page_handler(manifest, store)
|
||||||
].filter(Boolean));
|
].filter(Boolean));
|
||||||
|
|
||||||
return middleware;
|
return middleware;
|
||||||
@@ -152,8 +182,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 +226,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 +244,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 +256,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(manifest: Manifest, store_getter: (req: Req) => Store) {
|
||||||
const output = locations.dest();
|
const output = locations.dest();
|
||||||
|
|
||||||
const get_chunks = dev()
|
const get_chunks = dev()
|
||||||
@@ -238,22 +267,37 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
? () => fs.readFileSync(`${locations.app()}/template.html`, 'utf-8')
|
? () => fs.readFileSync(`${locations.app()}/template.html`, 'utf-8')
|
||||||
: (str => () => str)(fs.readFileSync(`${locations.dest()}/template.html`, 'utf-8'));
|
: (str => () => str)(fs.readFileSync(`${locations.dest()}/template.html`, 'utf-8'));
|
||||||
|
|
||||||
const { server_routes, pages } = routes;
|
const { server_routes, pages } = manifest;
|
||||||
const error_route = routes.error;
|
const error_route = manifest.error;
|
||||||
|
|
||||||
function handle_route(route: RouteObject, req: Req, res: ServerResponse, status = 200, error: Error | string = null) {
|
function handle_error(req: Req, res: ServerResponse, statusCode: number, error: Error | string) {
|
||||||
req.params = error
|
handle_page({
|
||||||
? {}
|
pattern: null,
|
||||||
: route.params(route.pattern.exec(req.path));
|
parts: [
|
||||||
|
{ name: null, component: error_route }
|
||||||
|
]
|
||||||
|
}, req, res, statusCode, error);
|
||||||
|
}
|
||||||
|
|
||||||
const chunks: Record<string, string> = get_chunks();
|
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);
|
||||||
|
|
||||||
|
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,63 +305,77 @@ 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 };
|
|
||||||
|
|
||||||
if (route.error) {
|
|
||||||
props.error = error instanceof Error ? error : { message: error };
|
|
||||||
props.status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
const preload_context = {
|
||||||
route.handler.preload ? route.handler.preload.call({
|
redirect: (statusCode: number, location: string) => {
|
||||||
redirect: (statusCode: number, location: string) => {
|
if (redirect && (redirect.statusCode !== statusCode || redirect.location !== location)) {
|
||||||
redirect = { statusCode, location };
|
throw new Error(`Conflicting redirects`);
|
||||||
},
|
}
|
||||||
error: (statusCode: number, message: Error | string) => {
|
redirect = { statusCode, location };
|
||||||
preload_error = { statusCode, message };
|
},
|
||||||
},
|
error: (statusCode: number, message: Error | string) => {
|
||||||
fetch: (url: string, opts?: any) => {
|
preload_error = { statusCode, message };
|
||||||
const parsed = new URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
|
},
|
||||||
|
fetch: (url: string, opts?: any) => {
|
||||||
|
const parsed = new URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
|
||||||
|
|
||||||
if (opts) {
|
if (opts) {
|
||||||
opts = Object.assign({}, opts);
|
opts = Object.assign({}, opts);
|
||||||
|
|
||||||
const include_cookies = (
|
const include_cookies = (
|
||||||
opts.credentials === 'include' ||
|
opts.credentials === 'include' ||
|
||||||
opts.credentials === 'same-origin' && parsed.origin === `http://127.0.0.1:${process.env.PORT}`
|
opts.credentials === 'same-origin' && parsed.origin === `http://127.0.0.1:${process.env.PORT}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (include_cookies) {
|
if (include_cookies) {
|
||||||
const cookies: Record<string, string> = {};
|
const cookies: Record<string, string> = {};
|
||||||
if (!opts.headers) opts.headers = {};
|
if (!opts.headers) opts.headers = {};
|
||||||
|
|
||||||
const str = []
|
const str = []
|
||||||
.concat(
|
.concat(
|
||||||
cookie.parse(req.headers.cookie || ''),
|
cookie.parse(req.headers.cookie || ''),
|
||||||
cookie.parse(opts.headers.cookie || ''),
|
cookie.parse(opts.headers.cookie || ''),
|
||||||
cookie.parse(res.getHeader('Set-Cookie') || '')
|
cookie.parse(res.getHeader('Set-Cookie') || '')
|
||||||
)
|
)
|
||||||
.map(cookie => {
|
.map(cookie => {
|
||||||
return Object.keys(cookie)
|
return Object.keys(cookie)
|
||||||
.map(name => `${name}=${encodeURIComponent(cookie[name])}`)
|
.map(name => `${name}=${encodeURIComponent(cookie[name])}`)
|
||||||
.join('; ');
|
.join('; ');
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join(', ');
|
.join(', ');
|
||||||
|
|
||||||
opts.headers.cookie = str;
|
opts.headers.cookie = str;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fetch(parsed.href, opts);
|
return fetch(parsed.href, opts);
|
||||||
},
|
},
|
||||||
store
|
store
|
||||||
}, req) : {}
|
};
|
||||||
).catch(err => {
|
|
||||||
|
const root_preloaded = manifest.root.preload
|
||||||
|
? manifest.root.preload.call(preload_context, {
|
||||||
|
path: req.path,
|
||||||
|
query: req.query,
|
||||||
|
params: {}
|
||||||
|
})
|
||||||
|
: {};
|
||||||
|
|
||||||
|
Promise.all([root_preloaded].concat(page.parts.map(part => {
|
||||||
|
return part.component.preload
|
||||||
|
? part.component.preload.call(preload_context, {
|
||||||
|
path: req.path,
|
||||||
|
query: req.query,
|
||||||
|
params: part.params ? part.params(match) : {}
|
||||||
|
})
|
||||||
|
: {};
|
||||||
|
}))).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 +386,52 @@ 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_error(req, res, preload_error.statusCode, preload_error.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const serialized = {
|
const serialized = {
|
||||||
preloaded: route.handler.preload && try_serialize(preloaded),
|
preloaded: `[${preloaded.map(data => try_serialize(data)).join(',')}]`,
|
||||||
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 props: Props = {
|
||||||
|
path: req.path,
|
||||||
|
query: req.query,
|
||||||
|
params: {},
|
||||||
|
child: null
|
||||||
|
};
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
props.error = error instanceof Error ? error : { message: error };
|
||||||
|
props.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = Object.assign({}, props, preloaded[0], {
|
||||||
|
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)
|
||||||
|
}, preloaded[i + 1])
|
||||||
|
});
|
||||||
|
|
||||||
|
level.props.child = <Props["child"]>{};
|
||||||
|
level = level.props.child;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { html, head, css } = manifest.root.render(data, {
|
||||||
store
|
store
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -349,6 +442,7 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
|
|||||||
.join('');
|
.join('');
|
||||||
|
|
||||||
let inline_script = `__SAPPER__={${[
|
let inline_script = `__SAPPER__={${[
|
||||||
|
error && `error:1`,
|
||||||
`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}`
|
||||||
@@ -359,7 +453,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 +461,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 +471,17 @@ 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 => {
|
||||||
|
if (error) {
|
||||||
|
// we encountered an error while rendering the error page — oops
|
||||||
|
res.statusCode = 500;
|
||||||
|
res.end(`<pre>${escape_html(err.message)}</pre>`);
|
||||||
|
} else {
|
||||||
|
handle_error(req, res, 500, err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,13 +489,13 @@ 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_error(req, res, 404, 'Not found');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,3 +526,15 @@ function try_serialize(data: any) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escape_html(html: string) {
|
||||||
|
const chars: Record<string, string> = {
|
||||||
|
'"' : 'quot',
|
||||||
|
"'": '#39',
|
||||||
|
'&': 'amp',
|
||||||
|
'<' : 'lt',
|
||||||
|
'>' : 'gt'
|
||||||
|
};
|
||||||
|
|
||||||
|
return html.replace(/["'&<>]/g, c => `&${chars[c]};`);
|
||||||
|
}
|
||||||
@@ -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, Manifest, 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 manifest: Manifest;
|
||||||
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,45 +51,50 @@ 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 (manifest.ignore.some(pattern => pattern.test(path))) return;
|
||||||
|
|
||||||
for (let i = 0; i < routes.pages.length; i += 1) {
|
for (let i = 0; i < manifest.pages.length; i += 1) {
|
||||||
const page = routes.pages[i];
|
const page = manifest.pages[i];
|
||||||
|
|
||||||
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,9 @@ function render(Page: ComponentConstructor, props: any, scroll: ScrollPosition,
|
|||||||
detach(end);
|
detach(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
component = new App({
|
Object.assign(data, root_data);
|
||||||
|
|
||||||
|
root = new manifest.root({
|
||||||
target,
|
target,
|
||||||
data,
|
data,
|
||||||
store,
|
store,
|
||||||
@@ -87,52 +119,149 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
let root_preload: Promise<any>;
|
||||||
|
let root_data: any;
|
||||||
|
|
||||||
|
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 => {
|
};
|
||||||
|
|
||||||
|
if (!root_preload) {
|
||||||
|
root_preload = manifest.root.preload
|
||||||
|
? initial_data.preloaded[0] || manifest.root.preload.call(preload_context, {
|
||||||
|
path,
|
||||||
|
query,
|
||||||
|
params: {}
|
||||||
|
})
|
||||||
|
: {};
|
||||||
|
}
|
||||||
|
|
||||||
|
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 + 1]
|
||||||
|
? Component.preload ? await Component.preload.call(preload_context, req) : {}
|
||||||
|
: initial_data.preloaded[i + 1];
|
||||||
|
|
||||||
|
return { Component, preloaded };
|
||||||
|
})).catch(err => {
|
||||||
error = { statusCode: 500, message: err };
|
error = { statusCode: 500, message: err };
|
||||||
}).then(preloaded => {
|
return [];
|
||||||
if (error) {
|
}).then(async results => {
|
||||||
return error_route().then(({ default: Page }: { default: ComponentConstructor }) => {
|
if (!root_data) root_data = await root_preload;
|
||||||
const err = error.message instanceof Error ? error.message : new Error(error.message);
|
|
||||||
Object.assign(props, { status: error.statusCode, error: err });
|
if (redirect) {
|
||||||
return { Page, props, redirect: null };
|
return { redirect };
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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: manifest.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 +277,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 +353,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 +384,28 @@ 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,
|
||||||
|
manifest: Manifest,
|
||||||
|
store?: (data: any) => Store,
|
||||||
|
routes?: any // legacy
|
||||||
|
}) {
|
||||||
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;
|
if (opts.routes) {
|
||||||
|
throw new Error(`As of Sapper 0.15, opts.routes should be opts.manifest`);
|
||||||
|
}
|
||||||
|
|
||||||
target = opts.target;
|
target = opts.target;
|
||||||
routes = opts.routes;
|
manifest = opts.manifest;
|
||||||
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
|
||||||
@@ -292,8 +429,10 @@ export function init(opts: { App: ComponentConstructor, target: Node, routes: Ro
|
|||||||
|
|
||||||
history.replaceState({ id: uid }, '', href);
|
history.replaceState({ id: uid }, '', href);
|
||||||
|
|
||||||
const target = select_route(new URL(window.location.href));
|
if (!initial_data.error) {
|
||||||
if (target) return navigate(target, uid);
|
const target = select_route(new URL(window.location.href));
|
||||||
|
if (target) return navigate(target, uid);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,9 +452,9 @@ export function goto(href: string, opts = { replaceState: false }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function prefetchRoutes(pathnames: string[]) {
|
export function prefetchRoutes(pathnames: string[]) {
|
||||||
if (!routes) throw new Error(`You must call init() first`);
|
if (!manifest) throw new Error(`You must call init() first`);
|
||||||
|
|
||||||
return routes.pages
|
return manifest.pages
|
||||||
.filter(route => {
|
.filter(route => {
|
||||||
if (!pathnames) return true;
|
if (!pathnames) return true;
|
||||||
return pathnames.some(pathname => route.pattern.test(pathname));
|
return pathnames.some(pathname => route.pattern.test(pathname));
|
||||||
|
|||||||
@@ -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 Manifest = {
|
||||||
|
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,13 +1,11 @@
|
|||||||
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 { manifest } 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,
|
manifest,
|
||||||
routes,
|
|
||||||
store: data => new Store(data)
|
store: data => new Store(data)
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import express from 'express';
|
|||||||
import serve from 'serve-static';
|
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 { manifest } from './manifest/server.js';
|
||||||
import App from './App.html'
|
|
||||||
|
|
||||||
let pending;
|
let pending;
|
||||||
let ended;
|
let ended;
|
||||||
@@ -87,8 +86,7 @@ const middlewares = [
|
|||||||
},
|
},
|
||||||
|
|
||||||
sapper({
|
sapper({
|
||||||
App,
|
manifest,
|
||||||
routes,
|
|
||||||
store: () => {
|
store: () => {
|
||||||
return new Store({
|
return new Store({
|
||||||
title: 'Stored title'
|
title: 'Stored title'
|
||||||
|
|||||||
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]/_layout.html
Normal file
20
test/app/routes/[x]/[y]/_layout.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]/_layout.html
Normal file
20
test/app/routes/[x]/_layout.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>
|
||||||
15
test/app/routes/_layout.html
Normal file
15
test/app/routes/_layout.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{#if preloading}
|
||||||
|
<progress class='preloading-progress' value=0.5/>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<svelte:component this={child.component} {rootPreloadFunctionRan} {...child.props}/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
preload() {
|
||||||
|
return {
|
||||||
|
rootPreloadFunctionRan: true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
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-root.html
Normal file
1
test/app/routes/preload-root.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>root preload function ran: {rootPreloadFunctionRan}</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}/>
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
<p>URL is {url}</p>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
preload({ url }) {
|
|
||||||
if (url) return { url };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
@@ -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,12 @@ 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\/[^/]+\/index(\.\d+)?\.js/,
|
||||||
/client\/[^/]+\/slow_preload(\.\d+)?\.js/,
|
/client\/[^/]+\/about(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/blog_\$slug(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/blog(\.\d+)?\.js/,
|
||||||
|
/client\/[^/]+\/slow\$45preload(\.\d+)?\.js/,
|
||||||
];
|
];
|
||||||
const allPages = walkSync(dest);
|
const allPages = walkSync(dest);
|
||||||
|
|
||||||
@@ -266,8 +267,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();
|
||||||
@@ -367,16 +369,6 @@ function run({ mode, basepath = '' }) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passes entire request object to preload', () => {
|
|
||||||
return nightmare
|
|
||||||
.goto(`${base}/show-url`)
|
|
||||||
.init()
|
|
||||||
.evaluate(() => document.querySelector('p').innerHTML)
|
|
||||||
.then(html => {
|
|
||||||
assert.equal(html, `URL is /show-url`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('calls a delete handler', () => {
|
it('calls a delete handler', () => {
|
||||||
return nightmare
|
return nightmare
|
||||||
.goto(`${base}/delete-test`)
|
.goto(`${base}/delete-test`)
|
||||||
@@ -629,6 +621,49 @@ 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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('runs preload in root component', () => {
|
||||||
|
return nightmare.goto(`${base}/preload-root`)
|
||||||
|
.page.title()
|
||||||
|
.then(title => {
|
||||||
|
assert.equal(title, 'root preload function ran: true');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('headers', () => {
|
describe('headers', () => {
|
||||||
@@ -641,7 +676,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'));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
165
test/unit/create_routes/index.js
Normal file
165
test/unit/create_routes/index.js
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const assert = require('assert');
|
||||||
|
const { create_routes } = require('../../../dist/core.ts.js');
|
||||||
|
|
||||||
|
|
||||||
|
const _default_layout = {
|
||||||
|
default: true,
|
||||||
|
name: '_default_layout',
|
||||||
|
file: null
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('create_routes', () => {
|
||||||
|
it('creates routes', () => {
|
||||||
|
const { components, pages, server_routes } = create_routes(path.join(__dirname, 'samples/basic'));
|
||||||
|
|
||||||
|
const index = { name: 'index', file: 'index.html' };
|
||||||
|
const about = { name: 'about', file: 'about.html' };
|
||||||
|
const blog = { name: 'blog', file: 'blog/index.html' };
|
||||||
|
const blog_$slug = { name: 'blog_$slug', file: 'blog/[slug].html' };
|
||||||
|
|
||||||
|
assert.deepEqual(components, [
|
||||||
|
index,
|
||||||
|
about,
|
||||||
|
_default_layout,
|
||||||
|
blog,
|
||||||
|
blog_$slug
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(pages, [
|
||||||
|
{
|
||||||
|
pattern: /^\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: index, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/about\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: about, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/blog\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: _default_layout, params: [] },
|
||||||
|
{ component: blog, params: [] }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
pattern: /^\/blog\/([^\/]+?)\/?$/,
|
||||||
|
parts: [
|
||||||
|
{ component: _default_layout, params: [] },
|
||||||
|
{ component: 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: '$34', file: '".html' };
|
||||||
|
const hash = { name: '$35', file: '#.html' };
|
||||||
|
// const question_mark = { name: '$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)), [
|
||||||
|
['index.html'],
|
||||||
|
['about.html'],
|
||||||
|
[_default_layout.file, 'post/index.html'],
|
||||||
|
[_default_layout.file, 'post/bar.html'],
|
||||||
|
[_default_layout.file, 'post/foo.html'],
|
||||||
|
[_default_layout.file, 'post/f[xx].html'],
|
||||||
|
[_default_layout.file, 'post/[id([0-9-a-z]{3,})].html'],
|
||||||
|
[_default_layout.file, '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/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/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
@@ -6,9 +6,9 @@
|
|||||||
"noEmitOnError": true,
|
"noEmitOnError": true,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"lib": ["es5", "es6", "dom"],
|
"lib": ["es5", "es6", "dom"],
|
||||||
"importHelpers": true
|
"importHelpers": true,
|
||||||
|
"target": "ES5"
|
||||||
},
|
},
|
||||||
"target": "ES5",
|
|
||||||
"include": [
|
"include": [
|
||||||
"src"
|
"src"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user