Skip to content

fix: respect replace_state on navigation where applicable - #13303

Closed
konstantinov90 wants to merge 27 commits into
sveltejs:mainfrom
konstantinov90:respectReplaceStateOnExternalNavigate
Closed

konstantinov90 wants to merge 27 commits into
sveltejs:mainfrom
konstantinov90:respectReplaceStateOnExternalNavigate

Conversation

@konstantinov90

@konstantinov90 konstantinov90 commented Jan 13, 2025 •

Copy link
Copy Markdown
Contributor

fixes #13403

When navigating via goto with replaceState: true with full page reload (e.g. navigating outside of the app) the page is not being replaced, but pushed to history instead
This PR aims to fix that


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Jan 13, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cc42c80

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris

Copy link
Copy Markdown
Member

@teemingc

Copy link
Copy Markdown
Member

goto is not meant to be used to navigate outside the app. From the docs:

For external URLs, use window.location = url instead of calling goto(url).

https://svelte.dev/docs/kit/$app-navigation#goto

@teemingc teemingc closed this Jan 15, 2025
@konstantinov90

Copy link
Copy Markdown
Contributor Author

@eltigerchino thank you, I had read the docs

goto is not meant to be used to navigate outside the app, but still it is used for same domain navigation, so why not do it correctly?

@teemingc

teemingc commented Jan 15, 2025 •

Copy link
Copy Markdown
Member

I think even if it's the same domain but not a part of the app it's still considered "outside the app", so you should use window.location = url instead.

@teemingc teemingc reopened this Jan 15, 2025
@konstantinov90

konstantinov90 commented Jan 15, 2025 •

Copy link
Copy Markdown
Contributor Author

@eltigerchino in theory I agree, but in practice it is quite convenient to use svelte-kit's mechanism to define which route is inside or outside of my current app. With file based routing you need some kind of manifest to determine if you navigate outside and as far as I know we don't have anything like that for client-side

Comment thread .changeset/dirty-rats-divide.md Resolved
@konstantinov90

Copy link
Copy Markdown
Contributor Author

@eltigerchino @dummdidumm hey, guys!

on the latest sveltekit version, goto still successfully navigates outside the app and still disrespects the replace_state parameter. Maybe you could accept this PR, or maybe we should break this "outside the app" navigation, so that not to mislead the users of sveltekit?

@teemingc teemingc changed the title respect replace_state on external navigation where applicable fix: respect replace_state on external navigation where applicable Jul 14, 2025
@teemingc

Copy link
Copy Markdown
Member

Could you add a test for this?

@konstantinov90
konstantinov90 force-pushed the respectReplaceStateOnExternalNavigate branch from fcaa948 to 472c362 Compare July 14, 2025 07:29
@svelte-docs-bot

Copy link
Copy Markdown

@konstantinov90 konstantinov90 changed the title fix: respect replace_state on external navigation where applicable fix: respect replace_state on navigation where applicable Jul 14, 2025
@konstantinov90

Copy link
Copy Markdown
Contributor Author

Could you add a test for this?

Done!

For clarity I have created the PR with tests only #13995, so as to illustrate the problems, that I am trying to solve

expectPath(page).toBe('/navigation/teststart');
});

test.describe('navigating outside the app on sameorigin', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I compare browser history between plain web navigation and sveltekit's goto navigation. I claim that browser history should be the same no matter which type of navigation I perform

Comment thread .changeset/dirty-rats-divide.md Resolved
Comment thread .changeset/dirty-rats-divide.md Resolved
Comment on lines +1649 to +1673
[
[
'app.goto { replaceState: false }',
async ({ app }) => {
await app.goto('/navigation/nonexistent', { replaceState: false }).catch(() => {});
}
],
[
'location.assign',
async ({ page }) => {
await page.evaluate(() => {
location.assign('/navigation/nonexistent');
});
}
]
].forEach(([title, navigate]) => {
test(title, async ({ app, page }) => {
await navigate({ app, page });
await page.waitForLoadState('networkidle');
expectPath(page).toBe('/navigation/nonexistent');

await page.goBack();
expectPath(page).toBe('/navigation/teststart');
});
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need these tests since they already work currently and are the default behaviour.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but someone could break it? I am not sure if this behaviour is covered by tests somewhere else

Comment on lines +1682 to +1689
[
'location.replace',
async ({ page }) => {
await page.evaluate(() => {
location.replace('/navigation/nonexistent');
});
}
]

@teemingc teemingc Jul 16, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the location.replace tests since they only test the browser functionality and nothing on SvelteKit's side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with such tests I tried to emphasize, that sveltekit's navigation behaves exactly the same as default browser navigation. In some way, these tests were aimed at you personally, to persuade you in correctness of this changes :)

if you still consider them to be unnecessary - I shall delete them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please let me know, if you still would like these extra tests to be deleted

[
'app.goto { replaceState: false }',
async ({ app }) => {
await app.goto('/navigation/nonexistent', { replaceState: false }).catch(() => {});

@teemingc teemingc Jul 16, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler flow for these tests without the catch handlers could be:

replaceState for external navigation

  1. Start at /goto/a
  2. Click a link that navigates to /goto/b
  3. Click a button that uses goto with replaceState: true to a non-existent route.
  4. Press the back button
  5. Expect to see the page contents of /goto/a

replaceState for redirects

  1. Start at /goto/a
  2. Click a link that navigates to /goto/b
  3. Click a button that uses goto with replaceState: true to /goto/redirect
  4. The load function of /goto/redirect should redirect to /goto/c
  5. Press the back button
  6. Expect to see the page contents of /goto/a

Note that we're adding the test routes to /goto since there's already test routes related to goto there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, thank you, I will do just that

@teemingc teemingc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I'll wait for another maintainer to take a look as well

@konstantinov90

Copy link
Copy Markdown
Contributor Author

@dummdidumm could you please take a look?

@Rich-Harris

Copy link
Copy Markdown
Member

Thank you but we can't merge this as it's a breaking change (as evidenced by the changes needed to unrelated tests, namely actions/update-form and shallow-routing/push-state).

I'm also just not sure it's desirable. Feels like it would be better to more consistently enforce the rule, spelled out in the docs, that goto is to be used for internal URLs only — in other words, await goto('/non-existent-route') should error just like await goto('https://elsewhere.com') does. (If we need to expose a helper for determining whether a pathname resolves or not, so be it.) Unfortunately that would also be a breaking change, so can't happen until SvelteKit 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

goto with replaceState: true not respected in redirects

4 participants