# svelte/no-navigation-without-resolve
disallow internal navigation (links,
goto(),pushState(),replaceState()) without aresolve()
- ⚙️ This rule is included in
"plugin:svelte/recommended".
This rule also runs on SvelteKit 3. Use resolve() from $app/paths for internal navigation. SvelteKit 3 removed the base export, so the deprecated no-navigation-without-base and no-goto-without-base rules do not run on Kit 3. See the official migration guide.
# 📖 Rule Details
This rule ensures internal navigation via HTML <a> tags, SvelteKit’s goto(), pushState() and replaceState() uses resolve(). <a> tags will skip this check when it has an absolute URL or rel="external". The external token must be present in the complete rel value. Adding data-sveltekit-reload does not exempt internal links from this rule. For programmatic external navigation, use window.location. Enforcing this rule ensures the base path is prefixed and internal links are type-checked.
A URL is considered resolved if it starts with a call to resolve() (or asset()) from $app/paths, or with a value of the type ResolvedPathname from $app/types. The resolved value may be followed by a query string or a fragment, as these don’t change the route. Appending anything else, such as another path segment, is reported, as it bypasses the route type-checking of resolve(). The URL may be built with concatenation, template literals, variables or ternaries.
The following URLs don’t need resolve():
- URLs that start with a query string (e.g.
?page=2) or a fragment (e.g.#top), as they keep the current path. - Empty URLs in
pushState()andreplaceState(), for shallow routing. - Absolute URLs and nullish values in
<a>tags.
<!-- ✓ GOOD -->
<script>
/* eslint svelte/no-navigation-without-resolve: "error" */
import { goto, pushState, replaceState } from '$app/navigation';
import { resolve } from '$app/paths';
goto(resolve('/foo/'));
pushState(resolve('/foo/'), {});
replaceState(resolve('/foo/'), {});
// queries and fragments
goto(resolve('/foo/') + '?page=2');
goto(`${resolve('/foo/')}#section`);
goto(resolve('/foo/') + (searchParams.size ? `?${searchParams}` : ''));
goto('?page=2');
replaceState('#top', {});
// shallow routing
pushState('', {});
replaceState('', {});
</script>
<a href={resolve('/foo/')}>Click me!</a>
<a href="{resolve('/foo/')}?page=2">Click me!</a>
<a href="?page=2">Click me!</a>
<a href="https://svelte.dev">Click me!</a>
<a href={someURL} rel="external">Click me!</a>
<a href={someURL} rel={`external nofollow`}>Click me!</a>
<a href="#top">Click me!</a>
<!-- ✗ BAD -->
<script>
/* eslint svelte/no-navigation-without-resolve: "error" */
import { goto, pushState, replaceState } from '$app/navigation';
import { resolve } from '$app/paths';
goto(Unexpected goto() call without resolve(). (svelte/no-navigation-without-resolve)'/foo');
goto(Unexpected goto() call without resolve(). (svelte/no-navigation-without-resolve)'/foo' + resolve('/bar'));
goto(Unexpected goto() call without resolve(). (svelte/no-navigation-without-resolve)resolve('/foo') + '/bar');
goto(Unexpected goto() call without resolve(). (svelte/no-navigation-without-resolve)resolve('/foo') + '/bar?page=2');
goto(Unexpected goto() call without resolve(). (svelte/no-navigation-without-resolve)`/foo?${searchParams}`);
pushState(Unexpected pushState() call without resolve(). (svelte/no-navigation-without-resolve)'/foo', {});
replaceState(Unexpected replaceState() call without resolve(). (svelte/no-navigation-without-resolve)'/foo', {});
</script>
<a Unexpected href link without resolve(). (svelte/no-navigation-without-resolve)href="/foo">Click me!</a>
<a Unexpected href link without resolve(). (svelte/no-navigation-without-resolve)href={'/foo'}>Click me!</a>
<a Unexpected href link without resolve(). (svelte/no-navigation-without-resolve)href="/foo" data-sveltekit-reload>Click me!</a>
<a Unexpected href link without resolve(). (svelte/no-navigation-without-resolve)href="/foo" rel="external-suffix">Click me!</a>
# 🔧 Options
{
"svelte/no-navigation-without-resolve": [
"error",
{
"ignoreGoto": false,
"ignoreLinks": false,
"ignorePushState": false,
"ignoreReplaceState": false
}
]
}
ignoreGoto… Whether to ignore allgoto()calls. Defaultfalse.ignoreLinks… Whether to ignore all<a>tags. Defaultfalse.ignorePushState… Whether to ignore allpushState()calls. Defaultfalse.ignoreReplaceState… Whether to ignore allreplaceState()calls. Defaultfalse.
# 📚 Further Reading
resolve()documentation- Shallow routing
goto()documentationpushState()documentationreplaceState()documentation
# 🚀 Version
This rule was introduced in eslint-plugin-svelte v3.12.0