diff --git a/.changeset/late-queens-build.md b/.changeset/late-queens-build.md new file mode 100644 index 000000000000..b38bd70f13ed --- /dev/null +++ b/.changeset/late-queens-build.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: reinstantiate state parameter for goto diff --git a/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md b/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md index a9b62e6d2bae..e859c7722649 100644 --- a/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md +++ b/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md @@ -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 diff --git a/packages/kit/src/runtime/app/navigation.js b/packages/kit/src/runtime/app/navigation.js index 4bbfae215f9c..f925ebe3ea12 100644 --- a/packages/kit/src/runtime/app/navigation.js +++ b/packages/kit/src/runtime/app/navigation.js @@ -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} */ export const goto = /* @__PURE__ */ client_method('goto'); diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 80fa5c07e116..95668d2579e2 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -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 }} options * @param {number} redirect_count * @param {{}} [nav_token] */ @@ -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) { @@ -1107,6 +1108,7 @@ export function create_client(app, target) { * keepfocus?: boolean; * noscroll?: boolean; * replace_state?: boolean; + * state?: Record; * redirect_count?: number; * nav_token?: {}; * accept?: () => void; @@ -1120,6 +1122,7 @@ export function create_client(app, target) { keepfocus, noscroll, replace_state, + state = {}, redirect_count = 0, nav_token = {}, accept = noop, @@ -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 @@ -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(