Skip to content

Commit

Permalink
fix: reinstantiate state parameter for goto (#11342)
Browse files Browse the repository at this point in the history
* fix: reinstantiate state parameter for goto

* Update documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md

---------

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
  • Loading branch information
dummdidumm and Rich-Harris authored Dec 18, 2023
1 parent 975ec9c commit 9eae31f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-queens-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: reinstantiate state parameter for goto
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function load({ fetch }) {

## goto(...) changes

`goto(...)` no longer accepts external URLs. To navigate to an external URL, use `window.location = url`. The `state` option was removed in favor of [shallow routing](shallow-routing).
`goto(...)` no longer accepts external URLs. To navigate to an external URL, use `window.location = url`. The `state` object now determines `$page.state` and must adhere to the `App.PageState` interface, if declared. See [shallow routing](shallow-routing) for more details.

## paths are now relative by default

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const disableScrollHandling = /* @__PURE__ */ client_method('disable_scro
* @param {boolean} [opts.noScroll] If `true`, the browser will maintain its scroll position rather than scrolling to the top of the page after navigation
* @param {boolean} [opts.keepFocus] If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body
* @param {boolean} [opts.invalidateAll] If `true`, all `load` functions of the page will be rerun. See https://kit.svelte.dev/docs/load#rerunning-load-functions for more info on invalidation.
* @param {App.PageState} [opts.state] An optional object that will be available on the `$page.state` store
* @returns {Promise<void>}
*/
export const goto = /* @__PURE__ */ client_method('goto');
Expand Down
15 changes: 5 additions & 10 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function create_client(app, target) {

/**
* @param {string | URL} url
* @param {{ replaceState?: boolean; noScroll?: boolean; keepFocus?: boolean; invalidateAll?: boolean; }} options
* @param {{ replaceState?: boolean; noScroll?: boolean; keepFocus?: boolean; invalidateAll?: boolean; state?: Record<string, any> }} options
* @param {number} redirect_count
* @param {{}} [nav_token]
*/
Expand All @@ -314,6 +314,7 @@ export function create_client(app, target) {
noscroll: options.noScroll,
replace_state: options.replaceState,
redirect_count,
state: options.state,
nav_token,
accept: () => {
if (options.invalidateAll) {
Expand Down Expand Up @@ -1107,6 +1108,7 @@ export function create_client(app, target) {
* keepfocus?: boolean;
* noscroll?: boolean;
* replace_state?: boolean;
* state?: Record<string, any>;
* redirect_count?: number;
* nav_token?: {};
* accept?: () => void;
Expand All @@ -1120,6 +1122,7 @@ export function create_client(app, target) {
keepfocus,
noscroll,
replace_state,
state = {},
redirect_count = 0,
nav_token = {},
accept = noop,
Expand Down Expand Up @@ -1213,7 +1216,7 @@ export function create_client(app, target) {
url.pathname = navigation_result.props.page.url.pathname;
}

const state = popped ? popped.state : {};
state = popped ? popped.state : state;

if (!popped) {
// this is a new navigation, rather than a popstate
Expand Down Expand Up @@ -1533,14 +1536,6 @@ export function create_client(app, target) {
goto: (url, opts = {}) => {
url = resolve_url(url);

// @ts-expect-error
if (DEV && opts.state) {
// TOOD 3.0 remove
throw new Error(
'Passing `state` to `goto` is no longer supported. Use `pushState` and `replaceState` from `$app/navigation` instead.'
);
}

if (url.origin !== origin) {
return Promise.reject(
new Error(
Expand Down

0 comments on commit 9eae31f

Please sign in to comment.