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

Wait until navigation ends before updating history #3241

Closed
wants to merge 4 commits into from
Closed

Conversation

Rich-Harris
Copy link
Member

Still not sure if this is desirable or not, but I'm opening this in order to close #2917. Fixes #2870.

It would be fairly straightforward to make this configurable, but I think something this fundamental should probably be decided by the framework.

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 pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jan 8, 2022

🦋 Changeset detected

Latest commit: 4136461

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 Patch

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

@netlify
Copy link

netlify bot commented Jan 8, 2022

❌ Deploy Preview for kit-demo failed.

🔨 Explore the source changes: 958211f

🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61d8fd3f6b034900089fdf9a

event.preventDefault();
});

addEventListener('popstate', (event) => {
if (event.state && this.enabled) {
const url = new URL(location.href);
this._navigate(url, event.state['sveltekit:scroll'], false, []);
this._navigate(url, event.state['sveltekit:scroll'], false, [], url.hash, null, null);
Copy link
Member

Choose a reason for hiding this comment

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

should we just allow these lass two parameters to be undefined so that you don't have to pass anything here?

@benmccann
Copy link
Member

I would say that I lean in favor of this change. It's taken me a couple days to comment on this as I really wanted to have time to read through and understand the various arguments and think about it.

However, I think there may be a slightly different implementation that I prefer even more, which would be to update the URL in between network fetching and DOM rendering (#2870 (comment)). This is most analogous to the browser's behavior. It also satisfies quite a lot of the people who wanted the URL to update before navigation (#2917 (comment)) because a lot of those arguments seemed to be that the UI should update after the URL. It sort of seems like it could get us the best of both worlds.

It's harder to figure out how to structure the code if we do that. The separation between router and renderer gets even messier. But I still think it's the best way to do it and we can figure out a refactoring later.

@Rich-Harris
Copy link
Member Author

However, I think there may be a slightly different implementation that I prefer even more, which would be to update the URL in between network fetching and DOM rendering

That feels like the worst of all worlds to me. It would be completely imperceptible to users (the difference is basically a microtask), but would mean that we couldn't rely on page state following navigation (for example during tests). What would be the advantage?

@Rich-Harris
Copy link
Member Author

I realised there's another compelling reason to update history at the start of the navigation rather than the end — unless the app implements some sort of loading bar UI, there's no visual indication that a navigation has been accepted other than the URL changing. That can make things feel sticky. When not using client-side navigation, the browser does provide immediate UI (the reload button starts spinning), which makes this much less of a concern. (The future appHistory API will align client-side navigation with the default behaviour, and I can imagine SvelteKit 2 or 3 using it.)

@Rich-Harris Rich-Harris mentioned this pull request Jan 12, 2022
5 tasks
@benmccann
Copy link
Member

That's probably the best argument I've heard for doing it before. And it does look like the AppHistory API will be helpful if/when it comes around.

Doing it after certainly would make testing easier as you pointed out. I'd prioritize users over ourselves though, so I guess I'm convinced on doing it before. Hopefully we can figure out the test flakiness one of these days

@netlify
Copy link

netlify bot commented Jan 13, 2022

✔️ Deploy Preview for kit-demo canceled.

🔨 Explore the source changes: 4136461

🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61df7840227c01000754070c

@Rich-Harris
Copy link
Member Author

The nice thing is that because of #3303, this is basically just a one-line change if and when we do want to do it. So there's probably no sense in leaving this PR open

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.

Router changes the URL prematurely
2 participants