Skip to content

Commit 8e587e1

Browse files
committed
docs(no-navigation-without-base): documented the new rule
1 parent d14664a commit 8e587e1

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

docs/rules/no-navigation-without-base.md

+45-11
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,77 @@ since: 'v2.36.0-next.9'
1212
1313
## :book: Rule Details
1414

15-
This rule reports navigation using SvelteKit's `goto()` function without prefixing a relative URL with the base path. If a non-prefixed relative URL is used for navigation, the `goto` function navigates away from the base path, which is usually not what you wanted to do (for external URLs, `window.location = url` should be used instead).
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.
1618

1719
<!--eslint-skip-->
1820

1921
```svelte
2022
<script>
2123
/* eslint svelte/no-navigation-without-base: "error" */
2224
23-
import { goto } from '$app/navigation';
25+
import { goto, pushState, replaceState } from '$app/navigation';
2426
import { base } from '$app/paths';
25-
import { base as baseAlias } from '$app/paths';
2627
2728
// ✓ GOOD
2829
goto(base + '/foo/');
2930
goto(`${base}/foo/`);
3031
31-
goto(baseAlias + '/foo/');
32-
goto(`${baseAlias}/foo/`);
32+
pushState(base + '/foo/', {});
33+
pushState(`${base}/foo/`, {});
34+
pushState('', {});
3335
34-
goto('https://localhost/foo/');
36+
replaceState(base + '/foo/', {});
37+
replaceState(`${base}/foo/`, {});
38+
replaceState('', {});
3539
3640
// ✗ BAD
3741
goto('/foo');
38-
3942
goto('/foo/' + base);
40-
goto(`/foo/${base}`);
43+
44+
pushState('/foo', {});
45+
replaceState('/foo', {});
4146
</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>
4256
```
4357

4458
## :wrench: Options
4559

46-
Nothing.
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`.
4778

4879
## :books: Further Reading
4980

50-
- [`goto()` documentation](https://kit.svelte.dev/docs/modules#$app-navigation-goto)
51-
- [`base` documentation](https://kit.svelte.dev/docs/modules#$app-paths-base)
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)
5286

5387
## :rocket: Version
5488

0 commit comments

Comments
 (0)