Skip to content

Commit

Permalink
fix: Detect multiple patching with different versions
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Nov 7, 2023
1 parent 4928040 commit cef5808
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions packages/next-usequerystate/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,27 @@ export function subscribeToQueryUpdates(
return () => emitter.off(NOTIFY_EVENT_KEY, callback)
}

if (typeof history === 'object' && !history.__nextUseQueryState_patched) {
debug('Patching history')
if (typeof history === 'object') {
patchHistory()
}

function patchHistory() {
// This is replaced with the package.json version by scripts/prepack.sh
// after semantic-release has done updating the version number.
const version = '0.0.0-inject-version-here'
const patched = history.__nextUseQueryState_patched
if (patched) {
if (patched !== version) {
// todo: Make this a URL error with more details and resolution instructions
console.warn(
'[next-usequerystate] Multiple versions of the library are loaded. This may lead to unexpected behavior. Currently using %s, but %s was about to load on top.',
patched,
version
)
}
return
}
debug('[nuqs] Patching history with %s', version)
for (const method of ['pushState', 'replaceState'] as const) {
const original = history[method].bind(history)
history[method] = function nextUseQueryState_patchedHistory(
Expand Down Expand Up @@ -80,10 +99,8 @@ if (typeof history === 'object' && !history.__nextUseQueryState_patched) {
return original(state, title === NOSYNC_MARKER ? '' : title, url)
}
}
// This is replaced by the package.json version in the prepack script
// after semantic-release has done updating the version number.
Object.defineProperty(history, '__nextUseQueryState_patched', {
value: '0.0.0-inject-version-here',
value: version,
writable: false,
enumerable: false,
configurable: false
Expand Down

0 comments on commit cef5808

Please sign in to comment.