Unverified Commit f88cee96 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Backport: Fix(pages-router): restore Content-Length and ETag for /_next/data/…

Backport: Fix(pages-router): restore Content-Length and ETag for /_next/data/ JSON responses (#90304) (#91615)

Backports #90304
Co-authored-by: 's avatarHenry <lllomh@qq.com>
parent cfd5f533
...@@ -502,16 +502,13 @@ export const getHandler = ({ ...@@ -502,16 +502,13 @@ export const getHandler = ({
return { return {
value: { value: {
kind: CachedRouteKind.PAGES, kind: CachedRouteKind.PAGES,
html: new RenderResult( html: new RenderResult(previousCacheEntry.value.html, {
Buffer.from(previousCacheEntry.value.html), contentType: HTML_CONTENT_TYPE_HEADER,
{ metadata: {
contentType: HTML_CONTENT_TYPE_HEADER, statusCode: previousCacheEntry.value.status,
metadata: { headers: previousCacheEntry.value.headers,
statusCode: previousCacheEntry.value.status, },
headers: previousCacheEntry.value.headers, }),
},
}
),
pageData: {}, pageData: {},
status: previousCacheEntry.value.status, status: previousCacheEntry.value.status,
headers: previousCacheEntry.value.headers, headers: previousCacheEntry.value.headers,
...@@ -710,13 +707,10 @@ export const getHandler = ({ ...@@ -710,13 +707,10 @@ export const getHandler = ({
// anymore // anymore
result: result:
isNextDataRequest && !isErrorPage && !is500Page isNextDataRequest && !isErrorPage && !is500Page
? new RenderResult( ? new RenderResult(JSON.stringify(result.value.pageData), {
Buffer.from(JSON.stringify(result.value.pageData)), contentType: JSON_CONTENT_TYPE_HEADER,
{ metadata: result.value.html.metadata,
contentType: JSON_CONTENT_TYPE_HEADER, })
metadata: result.value.html.metadata,
}
)
: result.value.html, : result.value.html,
generateEtags: nextConfig.generateEtags, generateEtags: nextConfig.generateEtags,
poweredByHeader: nextConfig.poweredByHeader, poweredByHeader: nextConfig.poweredByHeader,
......
...@@ -2071,7 +2071,10 @@ describe('Prerender', () => { ...@@ -2071,7 +2071,10 @@ describe('Prerender', () => {
it('should handle revalidating JSON correctly', async () => { it('should handle revalidating JSON correctly', async () => {
const route = `/_next/data/${next.buildId}/blog/post-2/comment-3.json` const route = `/_next/data/${next.buildId}/blog/post-2/comment-3.json`
const initialJson = await renderViaHTTP(next.url, route) const initialRes = await fetchViaHTTP(next.url, route)
const initialJson = await initialRes.text()
expect(initialRes.headers.get('Content-Length')).toBeDefined()
expect(initialRes.headers.get('ETag')).toBeDefined()
expect(initialJson).toMatch(/post-2/) expect(initialJson).toMatch(/post-2/)
expect(initialJson).toMatch(/comment-3/) expect(initialJson).toMatch(/comment-3/)
...@@ -2086,7 +2089,10 @@ describe('Prerender', () => { ...@@ -2086,7 +2089,10 @@ describe('Prerender', () => {
await renderViaHTTP(next.url, route) await renderViaHTTP(next.url, route)
await check(async () => { await check(async () => {
newJson = await renderViaHTTP(next.url, route) const newRes = await fetchViaHTTP(next.url, route)
expect(newRes.headers.get('Content-Length')).toBeDefined()
expect(newRes.headers.get('ETag')).toBeDefined()
newJson = await newRes.text()
return newJson !== initialJson ? 'success' : newJson return newJson !== initialJson ? 'success' : newJson
}, 'success') }, 'success')
...@@ -2143,7 +2149,10 @@ describe('Prerender', () => { ...@@ -2143,7 +2149,10 @@ describe('Prerender', () => {
it('should handle revalidating HTML correctly with blocking and seed', async () => { it('should handle revalidating HTML correctly with blocking and seed', async () => {
const route = '/blocking-fallback/a' const route = '/blocking-fallback/a'
const initialHtml = await renderViaHTTP(next.url, route) const initialRes = await fetchViaHTTP(next.url, route)
const initialHtml = await initialRes.text()
expect(initialRes.headers.get('Content-Length')).toBeDefined()
expect(initialRes.headers.get('ETag')).toBeDefined()
const $initial = cheerio.load(initialHtml) const $initial = cheerio.load(initialHtml)
expect($initial('p').text()).toBe('Post: a') expect($initial('p').text()).toBe('Post: a')
...@@ -2158,7 +2167,10 @@ describe('Prerender', () => { ...@@ -2158,7 +2167,10 @@ describe('Prerender', () => {
await renderViaHTTP(next.url, route) await renderViaHTTP(next.url, route)
await check(async () => { await check(async () => {
newHtml = await renderViaHTTP(next.url, route) const newRes = await fetchViaHTTP(next.url, route)
expect(newRes.headers.get('Content-Length')).toBeDefined()
expect(newRes.headers.get('ETag')).toBeDefined()
newHtml = await newRes.text()
return newHtml !== initialHtml ? 'success' : newHtml return newHtml !== initialHtml ? 'success' : newHtml
}, 'success') }, 'success')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment