Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RSC etag generation handling #63336

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion packages/next/src/server/send-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,48 @@ export async function sendRenderResult({
const payload = result.isDynamic ? null : result.toUnchunkedString()

if (payload !== null) {
const etag = generateEtags ? generateETag(payload) : undefined
let etagPayload = payload
if (type === 'rsc') {
// ensure etag generation is deterministic as
// ordering can differ even if underlying content
// does not differ
etagPayload = payload.split('\n').sort().join('\n')
} else if (type === 'html' && payload.includes('__next_f')) {
const { parse } =
require('next/dist/compiled/node-html-parser') as typeof import('next/dist/compiled/node-html-parser')

try {
// Parse the HTML
let root = parse(payload)

// Get script tags in the body element
let scriptTags = root
.querySelector('body')
?.querySelectorAll('script')
.filter(
(node) =>
!node.hasAttribute('src') && node.innerHTML?.includes('__next_f')
)

// Sort the script tags by their inner text
scriptTags?.sort((a, b) => a.innerHTML.localeCompare(b.innerHTML))

// Remove the original script tags
scriptTags?.forEach((script: any) => script.remove())

// Append the sorted script tags to the body
scriptTags?.forEach((script: any) =>
root.querySelector('body')?.appendChild(script)
)

// Stringify back to HTML
etagPayload = root.toString()
} catch (err) {
console.error(`Error parsing HTML payload`, err)
}
}

const etag = generateEtags ? generateETag(etagPayload) : undefined
if (sendEtagResponse(req, res, etag)) {
return
}
Expand Down
50 changes: 50 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,36 @@ createNextDescribe(
})

if (isNextStart) {
if (!process.env.__NEXT_EXPERIMENTAL_PPR) {
it('should have deterministic etag across revalidates', async () => {
const initialRes = await next.fetch(
'/variable-revalidate-stable/revalidate-3'
)
expect(initialRes.status).toBe(200)

// check 2 revalidate passes to ensure it's consistent
for (let i = 0; i < 2; i++) {
let startIdx = next.cliOutput.length

await retry(
async () => {
const res = await next.fetch(
'/variable-revalidate-stable/revalidate-3'
)
expect(next.cliOutput.substring(startIdx)).toContain(
'rendering /variable-revalidate-stable'
)
expect(initialRes.headers.get('etag')).toBe(
res.headers.get('etag')
)
},
12_000,
3_000
)
}
})
}

it('should output HTML/RSC files for static paths', async () => {
const files = (
await glob('**/*', {
Expand Down Expand Up @@ -733,6 +763,10 @@ createNextDescribe(
"variable-revalidate-edge/post-method/page_client-reference-manifest.js",
"variable-revalidate-edge/revalidate-3/page.js",
"variable-revalidate-edge/revalidate-3/page_client-reference-manifest.js",
"variable-revalidate-stable/revalidate-3.html",
"variable-revalidate-stable/revalidate-3.rsc",
"variable-revalidate-stable/revalidate-3/page.js",
"variable-revalidate-stable/revalidate-3/page_client-reference-manifest.js",
"variable-revalidate/authorization.html",
"variable-revalidate/authorization.rsc",
"variable-revalidate/authorization/page.js",
Expand Down Expand Up @@ -1445,6 +1479,22 @@ createNextDescribe(
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-config-revalidate/revalidate-3",
},
"/variable-revalidate-stable/revalidate-3": {
"dataRoute": "/variable-revalidate-stable/revalidate-3.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data",
},
],
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-revalidate-stable/revalidate-3",
},
"/variable-revalidate/authorization": {
"dataRoute": "/variable-revalidate/authorization.rsc",
"experimentalBypassFor": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Suspense } from 'react'

export const revalidate = 3

async function RandomSuspenseResolve() {
await new Promise((resolve) => {
setTimeout(resolve, Math.random() * 5_000)
})
return <p>hello world</p>
}

export default function Page() {
console.log('rendering /variable-revalidate-stable/revalidate-3')
return (
<>
<p>Testing etag...</p>
<Suspense fallback={'Loading...'}>
<RandomSuspenseResolve />
</Suspense>
<Suspense fallback={'Loading...'}>
<RandomSuspenseResolve />
</Suspense>
<Suspense fallback={'Loading...'}>
<RandomSuspenseResolve />
</Suspense>
</>
)
}
Loading