Skip to content

Commit

Permalink
Fix navigation when base path is set and validate that option's val…
Browse files Browse the repository at this point in the history
…ue (#1666)

Co-authored-by: Sidharth Vinod <sidharthv96@gmail.com>
  • Loading branch information
ignatiusmb and sidharthv96 authored Jun 11, 2021
1 parent b6763cc commit dc56d3c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-bikes-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix navigation when `base` path is set and validate that option's value
2 changes: 1 addition & 1 deletion documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Whether to [hydrate](#ssr-and-javascript-hydrate) the server-rendered HTML with
An object containing zero or more of the following `string` values:

- `assets` — an absolute path, or a path relative to `base`, where your app's files are served from. This is useful if your files are served from a storage bucket of some kind
- `base` — a root-relative (i.e. starts with `/`) path that specifies where your app is served from. This allows the app to live on a non-root path
- `base` — a root-relative path that must start, but not end with `/` (e.g. `/base-path`). This specifies where your app is served from and allows the app to live on a non-root path

### prerender

Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/load_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export function validate_config(config) {
// resolve paths
const { paths } = validated.kit;

if (paths.base !== '' && !paths.base.startsWith('/')) {
throw new Error('config.kit.paths.base must be a root-relative path');
if (paths.base !== '' && (paths.base.endsWith('/') || !paths.base.startsWith('/'))) {
throw new Error(
"kit.paths.base option must be a root-relative path that starts but doesn't end with '/'. See https://kit.svelte.dev/docs#configuration-paths"
);
}

paths.assets = resolve(paths.base, paths.assets);
Expand Down
14 changes: 13 additions & 1 deletion packages/kit/src/core/load_config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,19 @@ test('fails if paths.base is not root-relative', () => {
}
}
});
}, /^config\.kit\.paths\.base must be a root-relative path$/);
}, /^kit\.paths\.base option must be a root-relative path that starts but doesn't end with '\/'. See https:\/\/kit\.svelte\.dev\/docs#configuration-paths$/);
});

test("fails if paths.base ends with '/'", () => {
assert.throws(() => {
validate_config({
kit: {
paths: {
base: '/github-pages/'
}
}
});
}, /^kit\.paths\.base option must be a root-relative path that starts but doesn't end with '\/'. See https:\/\/kit\.svelte\.dev\/docs#configuration-paths$/);
});

test('fails if prerender.pages are invalid', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class Router {

if (incorrect) {
info.path = has_trailing_slash ? info.path.slice(0, -1) : info.path + '/';
history.replaceState({}, '', `${info.path}${location.search}`);
history.replaceState({}, '', `${this.base}${info.path}${location.search}`);
}
}

Expand Down

0 comments on commit dc56d3c

Please sign in to comment.