diff --git a/src/core/create_manifests.ts b/src/core/create_manifests.ts index da6f41d..421b2f3 100644 --- a/src/core/create_manifests.ts +++ b/src/core/create_manifests.ts @@ -153,6 +153,7 @@ function generate_client( return `// This file is generated by Sapper — do not edit it!\n` + template .replace(/__ROOT__/g, stringify(get_file(path_to_routes, manifest_data.root), false)) + .replace(/__ROOT_PRELOAD__/g, manifest_data.root.has_preload ? stringify(get_file(path_to_routes, manifest_data.root), false) : './internal') .replace(/__ERROR__/g, stringify(posixify(`${path_to_routes}/_error.html`), false)) .replace(/__IGNORE__/g, `[${server_routes_to_ignore.map(route => route.pattern).join(', ')}]`) .replace(/__COMPONENTS__/g, components) diff --git a/templates/internal.mjs b/templates/internal.mjs index 0eba064..59fa2f2 100644 --- a/templates/internal.mjs +++ b/templates/internal.mjs @@ -5,4 +5,6 @@ export const stores = { page: writable(null) }; -export const CONTEXT_KEY = {}; \ No newline at end of file +export const CONTEXT_KEY = {}; + +export const preload = () => ({}); \ No newline at end of file diff --git a/templates/src/app/app.ts b/templates/src/app/app.ts index 54250c1..5105583 100644 --- a/templates/src/app/app.ts +++ b/templates/src/app/app.ts @@ -1,6 +1,7 @@ import App from '@sapper/App.html'; import { stores } from '@sapper/internal'; -import Root, * as RootStatic from '__ROOT__'; +import Root from '__ROOT__'; +import { preload as root_preload } from '__ROOT_PRELOAD__'; import ErrorComponent from '__ERROR__'; import { Target, @@ -24,8 +25,7 @@ export const routes: Route[] = __PAGES__; let ready = false; let root_component: Component; let current_token: {}; -let root_preload: Promise; -let root_data: any; +let root_preloaded: Promise; let current_branch = []; export let prefetching: { @@ -171,8 +171,6 @@ async function render(branch: any[], props: any, page: Page, scroll: ScrollPosit detach(end); } - Object.assign(props, root_data); // TODO what is root_data, do we still need it? - root_component = new App({ target, props: { @@ -230,15 +228,12 @@ export async function hydrate_target(target: Target): Promise<{ } }; - if (!root_preload) { - const preload_fn = RootStatic['pre' + 'load']; // Rollup makes us jump through these hoops :( - root_preload = preload_fn - ? initial_data.preloaded[0] || preload_fn.call(preload_context, { - path, - query, - params: {} - }) - : {}; + if (!root_preloaded) { + root_preloaded = initial_data.preloaded[0] || root_preload.call(preload_context, { + path, + query, + params: {} + }); } let branch; @@ -272,8 +267,6 @@ export async function hydrate_target(target: Target): Promise<{ branch = []; } - if (!root_data) root_data = await root_preload; - if (redirect) return { redirect }; const page_data = { @@ -298,7 +291,7 @@ export async function hydrate_target(target: Target): Promise<{ }; } - const props = { child: {} }; + const props = Object.assign({}, await root_preloaded, { child: {} }); let level = props.child; branch.forEach(node => {