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

Option to disable client-side routing #570

Closed
wants to merge 4 commits into from
Closed
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 documentation/docs/13-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
adapter: null,
amp: false,
appDir: '_app',
clientSideRouting: true,
files: {
assets: 'static',
lib: 'src/lib',
Expand Down Expand Up @@ -57,6 +58,10 @@ Enable [AMP](#amp) mode.

The directory relative to `paths.assets` where the built JS and CSS (and imported assets) are served from. (The filenames therein contain content-based hashes, meaning they can be cached indefinitely).

#### clientSideRouting

Disable client side routing so that navigation will result in a full page reload.

#### files

An object containing zero or more of the following `string` values:
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ async function build_server(
app_dir: ${s(config.kit.appDir)},
host: ${s(config.kit.host)},
host_header: ${s(config.kit.hostHeader)},
client_side_routing: ${s(config.kit.clientSideRouting)},
get_component_path: id => ${s(`${config.kit.paths.assets}/${config.kit.appDir}/`)} + client_component_lookup[id],
get_stack: error => error.stack,
get_static_file,
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Watcher extends EventEmitter {
only_prerender: false,
host: this.config.kit.host,
host_header: this.config.kit.hostHeader,
client_side_routing: this.config.kit.clientSideRouting,
get_component_path: (id) => `/${id}?import`,
get_stack: (error) => {
this.viteDevServer.ssrFixStacktrace(error);
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/load_config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test('fills in defaults', () => {
adapter: [null],
amp: false,
appDir: '_app',
clientSideRouting: true,
files: {
assets: 'static',
lib: 'src/lib',
Expand Down Expand Up @@ -84,6 +85,7 @@ test('fills in partial blanks', () => {
adapter: [null],
amp: false,
appDir: '_app',
clientSideRouting: true,
files: {
assets: 'public',
lib: 'src/lib',
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/load_config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const options = {

appDir: expect_string('_app', false),

clientSideRouting: expect_boolean(true),

files: {
type: 'branch',
children: {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/load_config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ suite('load default config', async () => {
adapter: [null],
amp: false,
appDir: '_app',
clientSideRouting: true,
files: {
assets: join(cwd, 'static'),
lib: join(cwd, 'src/lib'),
Expand Down
20 changes: 12 additions & 8 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export class Router {
* base: string;
* host: string;
* pages: import('../../../types.internal').CSRPage[];
* client_side_routing: boolean;
* ignore: RegExp[];
* }} opts */
constructor({ base, host, pages, ignore }) {
constructor({ base, host, pages, client_side_routing, ignore }) {
this.base = base;
this.host = host;
this.pages = pages;
this.client_side_routing = client_side_routing;
this.ignore = ignore;

this.history = window.history || {
Expand Down Expand Up @@ -177,15 +179,17 @@ export class Router {
* @param {string[]} chain
*/
async goto(href, { noscroll = false, replaceState = false } = {}, chain) {
const url = new URL(href, get_base_uri(document));
const selected = this.select(url);
if (this.client_side_routing) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
const url = new URL(href, get_base_uri(document));
const selected = this.select(url);

if (selected) {
this.renderer.notify(selected.page);
if (selected) {
this.renderer.notify(selected.page);

// TODO shouldn't need to pass the hash here
this.history[replaceState ? 'replaceState' : 'pushState']({}, '', href);
return this.navigate(selected, noscroll ? scroll_state() : null, chain, url.hash);
// TODO shouldn't need to pass the hash here
this.history[replaceState ? 'replaceState' : 'pushState']({}, '', href);
return this.navigate(selected, noscroll ? scroll_state() : null, chain, url.hash);
}
}

location.href = href;
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import { set_paths } from '../paths.js';
* session: any;
* error: Error;
* status: number;
* client_side_routing: boolean;
* nodes: import('./types').NavigationTarget["nodes"];
* page: import('./types').NavigationTarget["page"];
* }} opts */
export async function start({ paths, target, session, error, status, nodes, page }) {
export async function start({ paths, target, session, error, status, client_side_routing, nodes, page }) {
const router = new Router({
base: paths.base,
host: page.host,
pages,
client_side_routing,
ignore
});

Expand All @@ -37,7 +39,7 @@ export async function start({ paths, target, session, error, status, nodes, page
init({ router, renderer });
set_paths(paths);

router.init(renderer);
if (client_side_routing) router.init(renderer);
await renderer.start({ nodes, page }, status, error);

dispatchEvent(new CustomEvent('sveltekit:start'));
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ async function get_response({ request, options, $session, route, status = 200, e
status: ${status},
error: ${serialize_error(error)},
session: ${serialized_session},
client_side_routing: ${options.client_side_routing},
nodes: [
${(route ? route.parts : [])
.map((part) => `import(${s(options.get_component_path(part.id))})`)
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Config = {
adapter?: string | [string, any];
amp?: boolean;
appDir?: string;
clientSideRouting?: boolean;
files?: {
assets?: string;
lib?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/types.internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ValidatedConfig = {
adapter: [string, any];
amp: boolean;
appDir: string;
clientSideRouting: boolean;
files: {
assets: string;
lib: string;
Expand Down Expand Up @@ -186,6 +187,7 @@ export type SSRRenderOptions = {
app_dir?: string;
host?: string;
host_header?: string;
client_side_routing?: boolean;
get_component_path?: (id: string) => string;
get_stack?: (error: Error) => string;
get_static_file?: (file: string) => Buffer;
Expand Down