Skip to content

Commit

Permalink
[fix] fully initialize router before rendering (#2089)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Aug 4, 2021
1 parent 71b7a6b commit aed1bd0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
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

0 comments on commit aed1bd0

Please sign in to comment.