Skip to content

Commit fae75f1

Browse files
authored
Add initial location state to router (sveltejs#1643)
1 parent 6cf6955 commit fae75f1

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

.changeset/happy-pumas-thank.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
add optional state parameter for goto function

documentation/docs/05-modules.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { amp, browser, dev, prerendering } from '$app/env';
2121
import { goto, invalidate, prefetch, prefetchRoutes } from '$app/navigation';
2222
```
2323

24-
- `goto(href, { replaceState, noscroll })` returns a `Promise` that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified `href`. The second argument is optional. If `replaceState` is true, a new history entry won't be created. If `noscroll` is true, the browser won't scroll to the top of the page after navigation.
24+
- `goto(href, { replaceState, noscroll, state })` returns a `Promise` that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified `href`. The second argument is optional. If `replaceState` is true, a new history entry won't be created. If `noscroll` is true, the browser won't scroll to the top of the page after navigation. If state is set, its value will be used to set the initital state of the new history entry, it defaults to `{}`.
2525
- `invalidate(href)` causes any `load` functions belonging to the currently active page to re-run if they `fetch` the resource in question. It returns a `Promise` that resolves when the page is subsequently updated.
2626
- `prefetch(href)` programmatically prefetches the given page, which means a) ensuring that the code for the page is loaded, and b) calling the page's `load` function with the appropriate options. This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with [sveltekit:prefetch](#anchor-options-sveltekit-prefetch). If the next navigation is to `href`, the values returned from `load` will be used, making navigation instantaneous. Returns a `Promise` that resolves when the prefetch is complete.
2727
- `prefetchRoutes(routes)` — programmatically prefetches the code for routes that haven't yet been fetched. Typically, you might call this to speed up subsequent navigation. If no argument is given, all routes will be fetched; otherwise, you can specify routes by any matching pathname such as `/about` (to match `src/routes/about.svelte`) or `/blog/*` (to match `src/routes/blog/[slug].svelte`). Unlike `prefetch`, this won't call `load` for individual pages. Returns a `Promise` that resolves when the routes have been prefetched.

packages/kit/src/runtime/client/router.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export class Router {
135135
if (!this.owns(url)) return;
136136

137137
const noscroll = a.hasAttribute('sveltekit:noscroll');
138+
138139
history.pushState({}, '', url.href);
139140
this._navigate(url, noscroll ? scroll_state() : null, [], url.hash);
140141
event.preventDefault();
@@ -178,14 +179,14 @@ export class Router {
178179

179180
/**
180181
* @param {string} href
181-
* @param {{ noscroll?: boolean, replaceState?: boolean }} opts
182+
* @param {{ noscroll?: boolean, replaceState?: boolean, state?: any }} opts
182183
* @param {string[]} chain
183184
*/
184-
async goto(href, { noscroll = false, replaceState = false } = {}, chain) {
185+
async goto(href, { noscroll = false, replaceState = false, state = {} } = {}, chain) {
185186
const url = new URL(href, get_base_uri(document));
186187

187188
if (this.enabled && this.owns(url)) {
188-
history[replaceState ? 'replaceState' : 'pushState']({}, '', href);
189+
history[replaceState ? 'replaceState' : 'pushState'](state, '', href);
189190
return this._navigate(url, noscroll ? scroll_state() : null, chain, url.hash);
190191
}
191192

0 commit comments

Comments
 (0)