Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change page.url when calling pushState(...) with a new url #13221

Closed
wants to merge 1 commit into from

Conversation

gyzerok
Copy link

@gyzerok gyzerok commented Dec 22, 2024

Recently @Rich-Harris fixed the first part of the bug, that I reported in #13196. However the second bug related to pushState is still a problem.

Basically when doing pushState('/new-url', { foo: 'bar' }) you end up with real browser url and page.url being out of sync. The weird part is that some of the tests are explicitly ensuring current behavior, which makes me unsure if that's an accident or I am missing something.

Some tests are failing because their existence made me unsure about the change and I am opening this PR to validate it.

That's my first contribution. I've tried to match the code and logic written around, but am not sure how well I've done that. Happy to incorporate any feedback.


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

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

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.

Copy link

changeset-bot bot commented Dec 22, 2024

⚠️ No Changeset found

Latest commit: 0341b8f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

@Leonidaz Leonidaz left a comment

Choose a reason for hiding this comment

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

Some observations but better to wait for the team to respond.

@@ -1958,17 +1958,22 @@ export function pushState(url, state) {
}

update_scroll_positions(current_history_index);
url = resolve_url(url);

Choose a reason for hiding this comment

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

to make sure that a new instance of URL is created as resolve_url will simply return the same instance if url was already a URL vs a string. And if the instance is the same replacing url on the page will not trigger a change.

Suggested change
url = resolve_url(url);
url = new URL(resolve_url(url));

has_navigated = true;

if (change) {
page.url = url;

Choose a reason for hiding this comment

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

Not 100% sure but maybe update_url(url) would be the call here to make sure that stores are also updated.

Suggested change
page.url = url;
update_url(url);

Choose a reason for hiding this comment

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

looks like the same changes as for pushState should be applied to export function replaceState(url, state). They seem to be very much the same and probably should share a common function with an arg like method with values either being pushState or replaceState. The error messages would be the same with method replacements and then there is only other thing when calling history and should be taken care of with history[method](opts, '', url).

But I don't know if the team still wants to keep them separate.

@@ -1958,17 +1958,22 @@ export function pushState(url, state) {
}

update_scroll_positions(current_history_index);
url = resolve_url(url);
const change = page.url.href !== url.href ? 1 : 0;

Choose a reason for hiding this comment

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

it's a boolean so not needed this here.

Suggested change
const change = page.url.href !== url.href ? 1 : 0;

has_navigated = true;

if (change) {

Choose a reason for hiding this comment

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

since it's a boolean

Suggested change
if (change) {
if (page.url.href !== url.href) {


const opts = {
[HISTORY_INDEX]: (current_history_index += 1),
[NAVIGATION_INDEX]: current_navigation_index,
[PAGE_URL_KEY]: page.url.href,
[NAVIGATION_INDEX]: (current_navigation_index += change),

Choose a reason for hiding this comment

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

I don't think this should be updated since no navigation actually takes place, only the url changes.

Suggested change
[NAVIGATION_INDEX]: (current_navigation_index += change),
[NAVIGATION_INDEX]: current_navigation_index,

type="button"
class="after"
onclick={() => {
pushState('/state/url/after', { s: 'test' });

Choose a reason for hiding this comment

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

also need a test to pushState with new URL(...) to check if it's reactive with a URL instance.

@gyzerok
Copy link
Author

gyzerok commented Dec 22, 2024

@Leonidaz really good points, thank you! Will try to work my way through them with tests first approach tomorrow. Even if the team will reject the PR, my goal is first of all to learn a bit more here with the hopes to contribute something meaningful to svelte one day :)

@eltigerchino
Copy link
Member

closing in favour of #13205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants