mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 12:24:47 +00:00
golf things down a bit
This commit is contained in:
@@ -1,7 +1,20 @@
|
||||
import { Manifest, Target, ScrollPosition, Component, Redirect, ComponentLoader, ComponentConstructor, RootProps } from './types';
|
||||
import RootComponent from '__ROOT__';
|
||||
import ErrorComponent from '__ERROR__';
|
||||
import {
|
||||
Target,
|
||||
ScrollPosition,
|
||||
Component,
|
||||
Redirect,
|
||||
ComponentLoader,
|
||||
ComponentConstructor,
|
||||
RootProps,
|
||||
Page
|
||||
} from './types';
|
||||
import goto from './goto';
|
||||
|
||||
export const manifest: Manifest = __MANIFEST__;
|
||||
const ignore = __IGNORE__;
|
||||
export const components: ComponentLoader[] = __COMPONENTS__;
|
||||
export const pages: Page[] = __PAGES__;
|
||||
|
||||
let ready = false;
|
||||
let root_component: Component;
|
||||
@@ -61,16 +74,16 @@ export { _history as history };
|
||||
export const scroll_history: Record<string, ScrollPosition> = {};
|
||||
|
||||
export function select_route(url: URL): Target {
|
||||
if (url.origin !== window.location.origin) return null;
|
||||
if (url.origin !== location.origin) return null;
|
||||
if (!url.pathname.startsWith(initial_data.baseUrl)) return null;
|
||||
|
||||
const path = url.pathname.slice(initial_data.baseUrl.length);
|
||||
|
||||
// avoid accidental clashes between server routes and pages
|
||||
if (manifest.ignore.some(pattern => pattern.test(path))) return;
|
||||
if (ignore.some(pattern => pattern.test(path))) return;
|
||||
|
||||
for (let i = 0; i < manifest.pages.length; i += 1) {
|
||||
const page = manifest.pages[i];
|
||||
for (let i = 0; i < pages.length; i += 1) {
|
||||
const page = pages[i];
|
||||
|
||||
const match = page.pattern.exec(path);
|
||||
if (match) {
|
||||
@@ -88,8 +101,8 @@ export function select_route(url: URL): Target {
|
||||
|
||||
export function scroll_state() {
|
||||
return {
|
||||
x: window.scrollX,
|
||||
y: window.scrollY
|
||||
x: scrollX,
|
||||
y: scrollY
|
||||
};
|
||||
}
|
||||
|
||||
@@ -159,7 +172,7 @@ function render(data: any, nullable_depth: number, scroll: ScrollPosition, token
|
||||
|
||||
Object.assign(data, root_data);
|
||||
|
||||
root_component = new manifest.root({
|
||||
root_component = new RootComponent({
|
||||
target,
|
||||
data,
|
||||
store,
|
||||
@@ -168,7 +181,7 @@ function render(data: any, nullable_depth: number, scroll: ScrollPosition, token
|
||||
}
|
||||
|
||||
if (scroll) {
|
||||
window.scrollTo(scroll.x, scroll.y);
|
||||
scrollTo(scroll.x, scroll.y);
|
||||
}
|
||||
|
||||
Object.assign(root_props, data);
|
||||
@@ -195,7 +208,7 @@ export function prepare_page(target: Target): Promise<{
|
||||
|
||||
const preload_context = {
|
||||
store,
|
||||
fetch: (url: string, opts?: any) => window.fetch(url, opts),
|
||||
fetch: (url: string, opts?: any) => fetch(url, opts),
|
||||
redirect: (statusCode: number, location: string) => {
|
||||
if (redirect && (redirect.statusCode !== statusCode || redirect.location !== location)) {
|
||||
throw new Error(`Conflicting redirects`);
|
||||
@@ -208,8 +221,8 @@ export function prepare_page(target: Target): Promise<{
|
||||
};
|
||||
|
||||
if (!root_preload) {
|
||||
root_preload = manifest.root.preload
|
||||
? initial_data.preloaded[0] || manifest.root.preload.call(preload_context, {
|
||||
root_preload = RootComponent.preload
|
||||
? initial_data.preloaded[0] || RootComponent.preload.call(preload_context, {
|
||||
path,
|
||||
query,
|
||||
params: {}
|
||||
@@ -221,7 +234,7 @@ export function prepare_page(target: Target): Promise<{
|
||||
if (i < changed_from) return null;
|
||||
if (!part) return null;
|
||||
|
||||
return load_component(part.component).then(Component => {
|
||||
return load_component(components[part.i]).then(Component => {
|
||||
const req = {
|
||||
path,
|
||||
query,
|
||||
@@ -276,7 +289,7 @@ export function prepare_page(target: Target): Promise<{
|
||||
data: Object.assign({}, props, {
|
||||
preloading: false,
|
||||
child: {
|
||||
component: manifest.error,
|
||||
component: ErrorComponent,
|
||||
props
|
||||
}
|
||||
})
|
||||
|
||||
@@ -8,7 +8,7 @@ export default function goto(href: string, opts = { replaceState: false }) {
|
||||
promise = navigate(target, null).then(() => {});
|
||||
if (history) history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
|
||||
} else {
|
||||
window.location.href = href;
|
||||
location.href = href;
|
||||
promise = new Promise(f => {}); // never resolves
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { manifest, load_component } from "../app";
|
||||
import { components, pages, load_component } from "../app";
|
||||
|
||||
export default function prefetchRoutes(pathnames: string[]) {
|
||||
if (!manifest) throw new Error(`You must call init() first`);
|
||||
|
||||
return manifest.pages
|
||||
return pages
|
||||
.filter(route => {
|
||||
if (!pathnames) return true;
|
||||
return pathnames.some(pathname => route.pattern.test(pathname));
|
||||
})
|
||||
.reduce((promise: Promise<any>, route) => promise.then(() => {
|
||||
return Promise.all(route.parts.map(part => part && load_component(part.component)));
|
||||
return Promise.all(route.parts.map(part => part && load_component(components[part.i])));
|
||||
}), Promise.resolve());
|
||||
}
|
||||
@@ -26,15 +26,15 @@ export default function start(opts: {
|
||||
set_target(opts.target);
|
||||
if (opts.store) set_store(opts.store);
|
||||
|
||||
window.addEventListener('click', handle_click);
|
||||
window.addEventListener('popstate', handle_popstate);
|
||||
addEventListener('click', handle_click);
|
||||
addEventListener('popstate', handle_popstate);
|
||||
|
||||
// prefetch
|
||||
window.addEventListener('touchstart', trigger_prefetch);
|
||||
window.addEventListener('mousemove', handle_mousemove);
|
||||
addEventListener('touchstart', trigger_prefetch);
|
||||
addEventListener('mousemove', handle_mousemove);
|
||||
|
||||
return Promise.resolve().then(() => {
|
||||
const { hash, href } = window.location;
|
||||
const { hash, href } = location;
|
||||
|
||||
const deep_linked = hash && document.getElementById(hash.slice(1));
|
||||
scroll_history[uid] = deep_linked ?
|
||||
@@ -44,7 +44,7 @@ export default function start(opts: {
|
||||
history.replaceState({ id: uid }, '', href);
|
||||
|
||||
if (!initial_data.error) {
|
||||
const target = select_route(new URL(window.location.href));
|
||||
const target = select_route(new URL(location.href));
|
||||
if (target) return navigate(target, uid);
|
||||
}
|
||||
});
|
||||
@@ -83,7 +83,7 @@ function handle_click(event: MouseEvent) {
|
||||
const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString';
|
||||
const href = String(svg ? (<SVGAElement>a).href.baseVal : a.href);
|
||||
|
||||
if (href === window.location.href) {
|
||||
if (href === location.href) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
@@ -99,7 +99,7 @@ function handle_click(event: MouseEvent) {
|
||||
const url = new URL(href);
|
||||
|
||||
// Don't handle hash changes
|
||||
if (url.pathname === window.location.pathname && url.search === window.location.search) return;
|
||||
if (url.pathname === location.pathname && url.search === location.search) return;
|
||||
|
||||
const target = select_route(url);
|
||||
if (target) {
|
||||
@@ -122,17 +122,17 @@ function handle_popstate(event: PopStateEvent) {
|
||||
scroll_history[cid] = scroll_state();
|
||||
|
||||
if (event.state) {
|
||||
const url = new URL(window.location.href);
|
||||
const url = new URL(location.href);
|
||||
const target = select_route(url);
|
||||
if (target) {
|
||||
navigate(target, event.state.id);
|
||||
} else {
|
||||
window.location.href = window.location.href;
|
||||
location.href = location.href;
|
||||
}
|
||||
} else {
|
||||
// hashchange
|
||||
set_uid(uid + 1);
|
||||
set_cid(uid);
|
||||
history.replaceState({ id: cid }, '', window.location.href);
|
||||
history.replaceState({ id: cid }, '', location.href);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ export type ComponentLoader = {
|
||||
export type Page = {
|
||||
pattern: RegExp;
|
||||
parts: Array<{
|
||||
component: ComponentLoader;
|
||||
i: number;
|
||||
params?: (match: RegExpExecArray) => Record<string, string>;
|
||||
}>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user