-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: change page.url when calling pushState(...) with a new url #13221
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1958,17 +1958,22 @@ export function pushState(url, state) { | |||||
} | ||||||
|
||||||
update_scroll_positions(current_history_index); | ||||||
url = resolve_url(url); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make sure that a new instance of URL is created as
Suggested change
|
||||||
const change = page.url.href !== url.href ? 1 : 0; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a boolean so not needed this here.
Suggested change
|
||||||
|
||||||
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), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
[PAGE_URL_KEY]: url.href, | ||||||
[STATES_KEY]: state | ||||||
}; | ||||||
|
||||||
history.pushState(opts, '', resolve_url(url)); | ||||||
history.pushState(opts, '', url); | ||||||
has_navigated = true; | ||||||
|
||||||
if (change) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since it's a boolean
Suggested change
|
||||||
page.url = url; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.state = state; | ||||||
root.$set({ page }); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<script> | ||
import { pushState } from '$app/navigation'; | ||
import { page } from '$app/state'; | ||
import { untrack } from 'svelte'; | ||
|
||
let count = $state(0); | ||
|
||
$effect(() => { | ||
page.url; | ||
untrack(() => { | ||
count += 1; | ||
}); | ||
}); | ||
</script> | ||
|
||
<button | ||
type="button" | ||
class="before" | ||
onclick={() => { | ||
pushState('', { s: 'test' }); | ||
}}>slug = before</button | ||
> | ||
<button | ||
type="button" | ||
class="after" | ||
onclick={() => { | ||
pushState('/state/url/after', { s: 'test' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also need a test to pushState with |
||
}}>slug = after</button | ||
> | ||
|
||
<h1>{page.url.pathname}</h1> | ||
<h2>page.url was updated {count} time(s)</h2> |
There was a problem hiding this comment.
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 toexport function replaceState(url, state)
. They seem to be very much the same and probably should share a common function with an arg likemethod
with values either beingpushState
orreplaceState
. The error messages would be the same withmethod
replacements and then there is only other thing when callinghistory
and should be taken care of withhistory[method](opts, '', url)
.But I don't know if the team still wants to keep them separate.