WORKAROUND #449 by intercepting buggy response in service worker

This commit is contained in:
keyan 2023-10-15 13:10:39 -05:00
parent 94cbc902d6
commit 61e7030b0b
1 changed files with 27 additions and 1 deletions

View File

@ -33,10 +33,36 @@ self.addEventListener('install', () => {
// The browser may use own caching (HTTP cache). // The browser may use own caching (HTTP cache).
// Also, the offline fallback only works if request matched a route // Also, the offline fallback only works if request matched a route
setDefaultHandler(new NetworkOnly({ setDefaultHandler(new NetworkOnly({
// tell us why a request failed in dev
plugins: [{ plugins: [{
fetchDidFail: async (args) => { fetchDidFail: async (args) => {
// tell us why a request failed in dev
process.env.NODE_ENV !== 'production' && console.log('fetch did fail', ...args) process.env.NODE_ENV !== 'production' && console.log('fetch did fail', ...args)
},
fetchDidSucceed: async ({ request, response, event, state }) => {
if (
response.ok &&
request.headers.get('x-nextjs-data') &&
response.headers.get('x-nextjs-matched-path') &&
response.headers.get('content-type') === 'application/json' &&
response.headers.get('content-length') === '2' &&
response.status === 200) {
console.log('service worker detected a successful yet empty nextjs SSR data response')
console.log('nextjs has a bug where it returns a 200 with empty data when it should return a 404')
console.log('see https://github.com/vercel/next.js/issues/56852')
console.log('HACK ... intercepting response and returning 404')
const headers = new Headers(response.headers)
headers.delete('x-nextjs-matched-path')
headers.delete('content-type')
headers.delete('content-length')
return new Response(null, {
status: 404,
statusText: 'Not Found',
headers,
ok: false
})
}
return response
} }
}] }]
})) }))