last fade attempt
This commit is contained in:
@@ -7,22 +7,31 @@
|
||||
* history entry or re-runs a load - the back button still means "the page
|
||||
* before", not "the previous day I clicked".
|
||||
*
|
||||
* Callers must pass the URL *untracked* (`get(page).url`, not `$page.url`).
|
||||
* Reading it reactively inside the same effect that writes it creates a loop:
|
||||
* replaceState publishes a new URL, the effect re-runs, writes again - which
|
||||
* Svelte eventually kills with `effect_update_depth_exceeded`, hanging the page.
|
||||
* The base is `location`, deliberately not `page.url`. Shallow routing does not
|
||||
* republish the URL: `replaceState` writes the history entry (and files the
|
||||
* *previous* `page.url` in it, so a popstate can restore it) but leaves
|
||||
* `page.url` on the last navigated URL. Diffing against that stale value is
|
||||
* wrong in exactly one direction - clearing a parameter. Opening a day writes
|
||||
* `?day=`, `page.url` still has none, so asking to remove it produces a URL
|
||||
* identical to the stale one, the write is skipped as a no-op, and the
|
||||
* parameter stays in the address bar for good.
|
||||
*
|
||||
* Reading `location` rather than a passed-in URL also removes the old trap that
|
||||
* callers had to pass it untracked: an effect that both read `$page.url` and
|
||||
* wrote to it looped until `effect_update_depth_exceeded` hung the page.
|
||||
*/
|
||||
import { browser } from '$app/environment';
|
||||
import { replaceState } from '$app/navigation';
|
||||
|
||||
export function syncSearchParams(url: URL, updates: Record<string, string | null>): void {
|
||||
export function syncSearchParams(updates: Record<string, string | null>): void {
|
||||
if (!browser) return;
|
||||
const next = new URL(url);
|
||||
const current = new URL(window.location.href);
|
||||
const next = new URL(current);
|
||||
for (const [key, value] of Object.entries(updates)) {
|
||||
if (value == null || value === '') next.searchParams.delete(key);
|
||||
else next.searchParams.set(key, value);
|
||||
}
|
||||
if (next.href === url.href) return;
|
||||
if (next.href === current.href) return;
|
||||
try {
|
||||
replaceState(next, {});
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user