Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] fully initialize router before rendering #2089

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-laws-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

[fix] fully initialize router before rendering
29 changes: 14 additions & 15 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ function find_anchor(node) {
}

export class Router {
/** @param {{
/**
* @param {{
* base: string;
* routes: import('types/internal').CSRRoute[];
* trailing_slash: import('types/internal').TrailingSlash;
* }} opts */
constructor({ base, routes, trailing_slash }) {
* renderer: import('./renderer').Renderer
* }} opts
*/
constructor({ base, routes, trailing_slash, renderer }) {
this.base = base;
this.routes = routes;
this.trailing_slash = trailing_slash;
}

/** @param {import('./renderer').Renderer} renderer */
init(renderer) {
/** @type {import('./renderer').Renderer} */
this.renderer = renderer;
renderer.router = this;

this.enabled = true;

// make it possible to reset focus
document.body.setAttribute('tabindex', '-1');

// create initial history entry, so we can return here
history.replaceState(history.state || {}, '', location.href);
}

init_listeners() {
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
Expand Down Expand Up @@ -147,12 +155,6 @@ export class Router {
this._navigate(url, event.state['sveltekit:scroll'], false, []);
}
});

// make it possible to reset focus
document.body.setAttribute('tabindex', '-1');

// create initial history entry, so we can return here
history.replaceState(history.state || {}, '', location.href);
}

/** @param {URL} url */
Expand Down Expand Up @@ -221,7 +223,6 @@ export class Router {
throw new Error('Attempted to prefetch a URL that does not belong to this app');
}

// @ts-expect-error
return this.renderer.load(info);
}

Expand Down Expand Up @@ -255,13 +256,11 @@ export class Router {
}
}

// @ts-expect-error
this.renderer.notify({
path: info.path,
query: info.query
});

// @ts-expect-error
await this.renderer.update(info, chain, false);

if (!keepfocus) {
Expand Down
19 changes: 10 additions & 9 deletions packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ export async function start({ paths, target, session, host, route, spa, trailing
throw new Error('Missing target element. See https://kit.svelte.dev/docs#configuration-target');
}

const router = route
? new Router({
base: paths.base,
routes,
trailing_slash
})
: null;

const renderer = new Renderer({
Root,
fallback,
Expand All @@ -46,13 +38,22 @@ export async function start({ paths, target, session, host, route, spa, trailing
host
});

const router = route
? new Router({
base: paths.base,
routes,
trailing_slash,
renderer
})
: null;

init(router);
set_paths(paths);

if (hydrate) await renderer.start(hydrate);
if (router) {
router.init(renderer);
if (spa) router.goto(location.href, { replaceState: true }, []);
router.init_listeners();
}

dispatchEvent(new CustomEvent('sveltekit:start'));
Expand Down