Skip to content

Commit

Permalink
[breaking] remove per-page router option
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jan 18, 2022
1 parent d6d492a commit 111fb9b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-eels-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] remove per-page `router` option
18 changes: 1 addition & 17 deletions documentation/docs/11-page-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@ title: Page options

By default, SvelteKit will render any component first on the server and send it to the client as HTML. It will then render the component again in the browser to make it interactive in a process called **hydration**. For this reason, you need to ensure that components can run in both places. SvelteKit will then initialise a [**router**](#routing) that takes over subsequent navigations.

You can control each of these on a per-app or per-page basis. Note that each of the per-page settings use [`context="module"`](https://svelte.dev/docs#script_context_module), and only apply to page components, _not_ [layout](#layouts) components.

If both are specified, per-page settings override per-app settings in case of conflicts.

### router

SvelteKit includes a [client-side router](#appendix-routing) that intercepts navigations (from the user clicking on links, or interacting with the back/forward buttons) and updates the page contents, rather than letting the browser handle the navigation by reloading.

In certain circumstances you might need to disable [client-side routing](#appendix-routing) with the app-wide [`router` config option](#configuration-router) or the page-level `router` export:

```html
<script context="module">
export const router = false;
</script>
```

Note that this will disable client-side routing for any navigation from this page, regardless of whether the router is already active.
Note that each of the per-page settings use [`context="module"`](https://svelte.dev/docs#script_context_module), and only apply to page components, _not_ [layout](#layouts) components. If both per-page and per-app settings are specified, the per-page settings override per-app settings in case of conflicts.

### hydrate

Expand Down
9 changes: 0 additions & 9 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,6 @@ export class Renderer {
this.loading.id = null;
this.autoscroll = true;
this.updating = false;

if (!this.router) return;

const leaf_node = navigation_result.state.branch[navigation_result.state.branch.length - 1];
if (leaf_node && leaf_node.module.router === false) {
this.router.disable();
} else {
this.router.enable();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function respond(incoming, options, state = {}) {
options,
state,
$session: await options.hooks.getSession(request),
page_config: { router: true, hydrate: true },
page_config: { hydrate: true },
stuff: {},
status: 200,
branch: [],
Expand Down
5 changes: 2 additions & 3 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { create_prerendering_url_proxy } from './utils.js';
* options: import('types/internal').SSRRenderOptions;
* state: import('types/internal').SSRRenderState;
* $session: any;
* page_config: { hydrate: boolean, router: boolean };
* page_config: { hydrate: boolean };
* status: number;
* error?: Error;
* url: URL;
Expand Down Expand Up @@ -151,7 +151,7 @@ export async function render_response({
.map((dep) => `\n\t<link${styles.has(dep) ? ' disabled' : ''} rel="stylesheet" href="${options.prefix + dep}">`)
.join('');

if (page_config.router || page_config.hydrate) {
if (page_config.hydrate) {
head += Array.from(js)
.map((dep) => `\n\t<link rel="modulepreload" href="${options.prefix + dep}">`)
.join('');
Expand All @@ -165,7 +165,6 @@ export async function render_response({
session: ${try_serialize($session, (error) => {
throw new Error(`Failed to serialize session data: ${error.message}`);
})},
route: ${!!page_config.router},
spa: ${!ssr},
trailing_slash: ${s(options.trailing_slash)},
hydrate: ${ssr && page_config.hydrate ? `{
Expand Down
12 changes: 8 additions & 4 deletions packages/kit/src/runtime/server/page/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export async function respond(opts) {
...opts,
branch: [],
page_config: {
hydrate: true,
router: true
hydrate: true
},
status: 200,
url: request.url,
Expand Down Expand Up @@ -247,12 +246,17 @@ function get_page_config(leaf, options) {
// TODO remove for 1.0
if ('ssr' in leaf) {
throw new Error(
'`export const ssr` has been removed — use the handle hook instead: https://kit.svelte.dev/docs#hooks-handle'
'`export const ssr` has been removed — use the `handle` hook instead: https://kit.svelte.dev/docs#hooks-handle'
);
}

if ('router' in leaf) {
throw new Error(
'`export const router` has been removed — use `beforeNavigate` instead: https://kit.svelte.dev/docs#modules-$app-navigation'
);
}

return {
router: 'router' in leaf ? !!leaf.router : options.router,
hydrate: 'hydrate' in leaf ? !!leaf.hydrate : options.hydrate
};
}
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/src/runtime/server/page/respond_with_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export async function respond_with_error({
state,
$session,
page_config: {
hydrate: options.hydrate,
router: options.router
hydrate: options.hydrate
},
stuff: error_loaded.stuff,
status,
Expand Down

0 comments on commit 111fb9b

Please sign in to comment.