mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 13:55:21 +00:00
execute error page hooks
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
ComponentLoader,
|
ComponentLoader,
|
||||||
ComponentConstructor,
|
ComponentConstructor,
|
||||||
Route,
|
Route,
|
||||||
|
Query,
|
||||||
Page
|
Page
|
||||||
} from './types';
|
} from './types';
|
||||||
import goto from './goto';
|
import goto from './goto';
|
||||||
@@ -80,6 +81,20 @@ export { _history as history };
|
|||||||
|
|
||||||
export const scroll_history: Record<string, ScrollPosition> = {};
|
export const scroll_history: Record<string, ScrollPosition> = {};
|
||||||
|
|
||||||
|
export function extract_query(search: string) {
|
||||||
|
const query = Object.create(null);
|
||||||
|
if (search.length > 0) {
|
||||||
|
search.slice(1).split('&').forEach(searchParam => {
|
||||||
|
let [, key, value] = /([^=]*)(?:=(.*))?/.exec(decodeURIComponent(searchParam));
|
||||||
|
value = (value || '').replace(/\+/g, ' ');
|
||||||
|
if (typeof query[key] === 'string') query[key] = [<string>query[key]];
|
||||||
|
if (typeof query[key] === 'object') (query[key] as string[]).push(value);
|
||||||
|
else query[key] = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
export function select_target(url: URL): Target {
|
export function select_target(url: URL): Target {
|
||||||
if (url.origin !== location.origin) return null;
|
if (url.origin !== location.origin) return null;
|
||||||
if (!url.pathname.startsWith(initial_data.baseUrl)) return null;
|
if (!url.pathname.startsWith(initial_data.baseUrl)) return null;
|
||||||
@@ -89,22 +104,14 @@ export function select_target(url: URL): Target {
|
|||||||
// avoid accidental clashes between server routes and page routes
|
// avoid accidental clashes between server routes and page routes
|
||||||
if (ignore.some(pattern => pattern.test(path))) return;
|
if (ignore.some(pattern => pattern.test(path))) return;
|
||||||
|
|
||||||
|
const query: Query = extract_query(url.search)
|
||||||
|
|
||||||
for (let i = 0; i < routes.length; i += 1) {
|
for (let i = 0; i < routes.length; i += 1) {
|
||||||
const route = routes[i];
|
const route = routes[i];
|
||||||
|
|
||||||
const match = route.pattern.exec(path);
|
const match = route.pattern.exec(path);
|
||||||
if (match) {
|
|
||||||
const query: Record<string, string | string[]> = Object.create(null);
|
|
||||||
if (url.search.length > 0) {
|
|
||||||
url.search.slice(1).split('&').forEach(searchParam => {
|
|
||||||
let [, key, value] = /([^=]*)(?:=(.*))?/.exec(decodeURIComponent(searchParam));
|
|
||||||
value = (value || '').replace(/\+/g, ' ');
|
|
||||||
if (typeof query[key] === 'string') query[key] = [<string>query[key]];
|
|
||||||
if (typeof query[key] === 'object') (query[key] as string[]).push(value);
|
|
||||||
else query[key] = value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (match) {
|
||||||
const part = route.parts[route.parts.length - 1];
|
const part = route.parts[route.parts.length - 1];
|
||||||
const params = part.params ? part.params(match) : {};
|
const params = part.params ? part.params(match) : {};
|
||||||
|
|
||||||
@@ -115,6 +122,29 @@ export function select_target(url: URL): Target {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function handle_error(url: URL) {
|
||||||
|
const { pathname, search } = location;
|
||||||
|
const { session, preloaded, status, error } = initial_data;
|
||||||
|
|
||||||
|
const props = {
|
||||||
|
error,
|
||||||
|
status,
|
||||||
|
session,
|
||||||
|
level0: {},
|
||||||
|
level1: {
|
||||||
|
props: {
|
||||||
|
status,
|
||||||
|
error
|
||||||
|
},
|
||||||
|
component: ErrorComponent
|
||||||
|
},
|
||||||
|
segments: preloaded
|
||||||
|
|
||||||
|
}
|
||||||
|
const query = extract_query(search)
|
||||||
|
render(null, [], props, { path: pathname, query, params: {} });
|
||||||
|
}
|
||||||
|
|
||||||
export function scroll_state() {
|
export function scroll_state() {
|
||||||
return {
|
return {
|
||||||
x: pageXOffset,
|
x: pageXOffset,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
scroll_history,
|
scroll_history,
|
||||||
scroll_state,
|
scroll_state,
|
||||||
select_target,
|
select_target,
|
||||||
|
handle_error,
|
||||||
set_target,
|
set_target,
|
||||||
uid,
|
uid,
|
||||||
set_uid,
|
set_uid,
|
||||||
@@ -34,10 +35,12 @@ export default function start(opts: {
|
|||||||
|
|
||||||
history.replaceState({ id: uid }, '', href);
|
history.replaceState({ id: uid }, '', href);
|
||||||
|
|
||||||
if (!initial_data.error) {
|
const url = new URL(location.href)
|
||||||
const target = select_target(new URL(location.href));
|
|
||||||
if (target) return navigate(target, uid, false, hash);
|
if (initial_data.error) return handle_error(url);
|
||||||
}
|
|
||||||
|
const target = select_target(url);
|
||||||
|
if (target) return navigate(target, uid, false, hash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,4 +130,4 @@ function handle_popstate(event: PopStateEvent) {
|
|||||||
set_cid(uid);
|
set_cid(uid);
|
||||||
history.replaceState({ id: cid }, '', location.href);
|
history.replaceState({ id: cid }, '', location.href);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -243,11 +243,12 @@ export function get_page_handler(
|
|||||||
preloaded: `[${preloaded.map(data => try_serialize(data)).join(',')}]`,
|
preloaded: `[${preloaded.map(data => try_serialize(data)).join(',')}]`,
|
||||||
session: session && try_serialize(session, err => {
|
session: session && try_serialize(session, err => {
|
||||||
throw new Error(`Failed to serialize session data: ${err.message}`);
|
throw new Error(`Failed to serialize session data: ${err.message}`);
|
||||||
})
|
}),
|
||||||
|
error: error && try_serialize(props.error)
|
||||||
};
|
};
|
||||||
|
|
||||||
let script = `__SAPPER__={${[
|
let script = `__SAPPER__={${[
|
||||||
error && `error:1`,
|
error && `error:${serialized.error},status:${status}`,
|
||||||
`baseUrl:"${req.baseUrl}"`,
|
`baseUrl:"${req.baseUrl}"`,
|
||||||
serialized.preloaded && `preloaded:${serialized.preloaded}`,
|
serialized.preloaded && `preloaded:${serialized.preloaded}`,
|
||||||
serialized.session && `session:${serialized.session}`
|
serialized.session && `session:${serialized.session}`
|
||||||
|
|||||||
@@ -292,4 +292,4 @@ async function _build(
|
|||||||
console.log(event.result.print());
|
console.log(event.result.print());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,17 @@
|
|||||||
|
<script>
|
||||||
|
import { onMount, onDestroy } from 'svelte'
|
||||||
|
|
||||||
|
export let status, error = {};
|
||||||
|
|
||||||
|
let mounted = false;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
mounted = 'success';
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
<h1>{status}</h1>
|
<h1>{status}</h1>
|
||||||
|
|
||||||
<p>{error.message}</p>
|
<h2>{mounted}</h2>
|
||||||
|
|
||||||
|
<p>{error.message}</p>
|
||||||
|
|||||||
@@ -112,6 +112,17 @@ describe('errors', function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('execute error page hooks', async () => {
|
||||||
|
await page.goto(`${base}/some-throw-page`);
|
||||||
|
await start();
|
||||||
|
await wait(50);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
await page.$eval('h2', node => node.textContent),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
it('does not serve error page for async non-page error', async () => {
|
it('does not serve error page for async non-page error', async () => {
|
||||||
await page.goto(`${base}/async-throw.json`);
|
await page.goto(`${base}/async-throw.json`);
|
||||||
|
|
||||||
@@ -134,4 +145,4 @@ describe('errors', function() {
|
|||||||
await wait(50);
|
await wait(50);
|
||||||
assert.equal(await title(), 'No error here');
|
assert.equal(await title(), 'No error here');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user