|
| 1 | +--- |
| 2 | +pageClass: 'rule-details' |
| 3 | +sidebarDepth: 0 |
| 4 | +title: 'svelte/no-navigation-without-base' |
| 5 | +description: 'disallow using navigation (links, goto, pushState, replaceState) without the base path' |
| 6 | +since: 'v2.36.0-next.9' |
| 7 | +--- |
| 8 | + |
| 9 | +# svelte/no-navigation-without-base |
| 10 | + |
| 11 | +> disallow using navigation (links, goto, pushState, replaceState) without the base path |
| 12 | +
|
| 13 | +## :book: Rule Details |
| 14 | + |
| 15 | +This rule reports navigation using HTML `<a>` tags, SvelteKit's `goto()`, `pushState()` and `replaceState()` functions without prefixing a relative URL with the base path. All four of these may be used for navigation, with `goto()`, `pushState()` and `replaceState()` being intended solely for iternal navigation (i.e. not leaving the site), while `<a>` tags may be used for both internal and external navigation. When using any way of internal navigation, the base path must be prepended, otherwise the site may break. For programmatic navigation to external URLs, using `window.location` is advised. |
| 16 | + |
| 17 | +This rule checks all 4 navigation options for the presence of the base path, with an exception for `<a>` links to absolute URLs, which are assumed to be used for external navigation and so do not require the base path, and for shallow outing functions with an empty string as the path, which keeps the current URL. |
| 18 | + |
| 19 | +<!--eslint-skip--> |
| 20 | + |
| 21 | +```svelte |
| 22 | +<script> |
| 23 | + /* eslint svelte/no-navigation-without-base: "error" */ |
| 24 | +
|
| 25 | + import { goto, pushState, replaceState } from '$app/navigation'; |
| 26 | + import { base } from '$app/paths'; |
| 27 | +
|
| 28 | + // ✓ GOOD |
| 29 | + goto(base + '/foo/'); |
| 30 | + goto(`${base}/foo/`); |
| 31 | +
|
| 32 | + pushState(base + '/foo/', {}); |
| 33 | + pushState(`${base}/foo/`, {}); |
| 34 | + pushState('', {}); |
| 35 | +
|
| 36 | + replaceState(base + '/foo/', {}); |
| 37 | + replaceState(`${base}/foo/`, {}); |
| 38 | + replaceState('', {}); |
| 39 | +
|
| 40 | + // ✗ BAD |
| 41 | + goto('/foo'); |
| 42 | + goto('/foo/' + base); |
| 43 | +
|
| 44 | + pushState('/foo', {}); |
| 45 | + replaceState('/foo', {}); |
| 46 | +</script> |
| 47 | +
|
| 48 | +<!-- ✓ GOOD --> |
| 49 | +<a href={base + '/foo/'}>Click me!</a> |
| 50 | +<a href={`${base}/foo/`}>Click me!</a> |
| 51 | +<a href="https://svelte.dev">Click me!</a> |
| 52 | +
|
| 53 | +<!-- ✗ BAD --> |
| 54 | +<a href="/foo">Click me!</a> |
| 55 | +<a href={'/foo'}>Click me!</a> |
| 56 | +``` |
| 57 | + |
| 58 | +## :wrench: Options |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "svelte/no-navigation-without-base": [ |
| 63 | + "error", |
| 64 | + { |
| 65 | + "ignoreGoto": false, |
| 66 | + "ignoreLinks": false, |
| 67 | + "ignorePushState": false, |
| 68 | + "ignoreReplaceState": false |
| 69 | + } |
| 70 | + ] |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +- `ignoreGoto` ... Whether to ignore all `goto()` calls. Default `false`. |
| 75 | +- `ignoreLinks` ... Whether to ignore all `<a>` tags. Default `false`. |
| 76 | +- `ignorePushState` ... Whether to ignore all `pushState()` calls. Default `false`. |
| 77 | +- `ignoreReplaceState` ... Whether to ignore all `replaceState()` calls. Default `false`. |
| 78 | + |
| 79 | +## :books: Further Reading |
| 80 | + |
| 81 | +- [`base` documentation](https://svelte.dev/docs/kit/$app-paths#base) |
| 82 | +- [Shallow routing](https://svelte.dev/docs/kit/shallow-routing) |
| 83 | +- [`goto()` documentation](https://svelte.dev/docs/kit/$app-navigation#goto) |
| 84 | +- [`pushState()` documentation](https://svelte.dev/docs/kit/$app-navigation#pushState) |
| 85 | +- [`replaceState()` documentation](https://svelte.dev/docs/kit/$app-navigation#replaceState) |
| 86 | + |
| 87 | +## :rocket: Version |
| 88 | + |
| 89 | +This rule was introduced in eslint-plugin-svelte v2.36.0-next.9 |
| 90 | + |
| 91 | +## :mag: Implementation |
| 92 | + |
| 93 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/no-navigation-without-base.ts) |
| 94 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/no-navigation-without-base.ts) |
0 commit comments