Skip to content

Commit

Permalink
feat: Reject URL updates with unapplied search params
Browse files Browse the repository at this point in the history
So actions can be taken on the .catch branch.
  • Loading branch information
franky47 committed Nov 13, 2023
1 parent 5eaf822 commit 18c2ae7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/next-usequerystate/src/update-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export function scheduleFlushToURL(router: Router) {
}
function flushNow() {
lastFlushTimestamp = performance.now()
const search = flushUpdateQueue(router)
if (!search) {
reject()
} else {
const [search, error] = flushUpdateQueue(router)
if (error === null) {
resolve(search)
} else {
reject(search)
}
flushPromiseCache = null
}
Expand Down Expand Up @@ -108,10 +108,10 @@ export function scheduleFlushToURL(router: Router) {
return flushPromiseCache
}

function flushUpdateQueue(router: Router) {
function flushUpdateQueue(router: Router): [URLSearchParams, null | unknown] {
const search = new URLSearchParams(location.search)
if (updateQueue.size === 0) {
return search
return [search, null]
}
// Work on a copy and clear the queue immediately
const items = Array.from(updateQueue.entries())
Expand Down Expand Up @@ -160,14 +160,14 @@ function flushUpdateQueue(router: Router) {
shallow: false
})
}
return search
return [search, null]
} catch (error) {
console.error(
// This may fail due to rate-limiting of history methods,
// for example Safari only allows 100 updates in a 30s window.
`useQueryState error updating URL: ${error}`
)
return null
return [search, error]
}
}

Expand Down

0 comments on commit 18c2ae7

Please sign in to comment.