File size: 1,204 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import type { DevToolsConfig } from '../shared'
import { devToolsConfigSchema } from '../../shared/devtools-config-schema'
import { deepMerge } from '../../shared/deepmerge'
let queuedConfigPatch: DevToolsConfig = {}
let timer: ReturnType<typeof setTimeout> | null = null
function flushPatch() {
if (Object.keys(queuedConfigPatch).length === 0) {
return
}
const body = JSON.stringify(queuedConfigPatch)
queuedConfigPatch = {}
fetch('/__nextjs_devtools_config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
// keepalive in case of fetch interrupted, e.g. navigation or reload
keepalive: true,
}).catch((error) => {
console.warn('[Next.js DevTools] Failed to save config:', {
data: body,
error,
})
})
}
export function saveDevToolsConfig(patch: DevToolsConfig) {
const validation = devToolsConfigSchema.safeParse(patch)
if (!validation.success) {
console.warn(
'[Next.js DevTools] Invalid config patch:',
validation.error.message
)
return
}
queuedConfigPatch = deepMerge(queuedConfigPatch, patch)
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(flushPatch, 120)
}
|