Unverified Commit fde065b9 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: add vary header to app routes (#47946)

Next.js adds a `Vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch`
to app routes for both rendering and flight requests. Without this, the
browser can mistakenly serve the flight request to the app render, and
vice versa.

This only adds `Vary: RSC` for the moment, I'm not sure what the others
are used for. We also don't add a `Cache-Control: max-age=0` header, but
I'm not sure if that's necessary either.

Fixes NEXT-657
fix #45595
parent e3e22f5b
...@@ -27,10 +27,11 @@ import "next/dist/server/node-polyfill-fetch"; ...@@ -27,10 +27,11 @@ import "next/dist/server/node-polyfill-fetch";
import "next/dist/server/node-polyfill-web-streams"; import "next/dist/server/node-polyfill-web-streams";
import "@vercel/turbopack-next/polyfill/async-local-storage"; import "@vercel/turbopack-next/polyfill/async-local-storage";
import { renderToHTMLOrFlight } from "next/dist/server/app-render/app-render"; import { renderToHTMLOrFlight } from "next/dist/server/app-render/app-render";
import { PassThrough } from "stream"; import { RSC_VARY_HEADER } from "next/dist/client/components/app-router-headers"
import { ServerResponseShim } from "@vercel/turbopack-next/internal/http"; import { ServerResponseShim } from "@vercel/turbopack-next/internal/http";
import { headersFromEntries } from "@vercel/turbopack-next/internal/headers"; import { headersFromEntries } from "@vercel/turbopack-next/internal/headers";
import { parse, ParsedUrlQuery } from "node:querystring"; import { parse, ParsedUrlQuery } from "node:querystring";
import { PassThrough } from "node:stream";
("TURBOPACK { transition: next-layout-entry; chunking-type: isolatedParallel }"); ("TURBOPACK { transition: next-layout-entry; chunking-type: isolatedParallel }");
// @ts-ignore // @ts-ignore
...@@ -290,6 +291,7 @@ async function runOperation(renderData: RenderData) { ...@@ -290,6 +291,7 @@ async function runOperation(renderData: RenderData) {
return { return {
headers: [ headers: [
["Content-Type", result.contentType() ?? MIME_TEXT_HTML_UTF8], ["Content-Type", result.contentType() ?? MIME_TEXT_HTML_UTF8],
["Vary", RSC_VARY_HEADER],
] as [string, string][], ] as [string, string][],
body, body,
}; };
......
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
},
}
module.exports = nextConfig
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>{children}</body>
</html>
)
}
'use client'
import { useEffect } from 'react'
import { RSC_VARY_HEADER } from 'next/dist/client/components/app-router-headers'
export default function Home() {
useEffect(() => {
import('@turbo/pack-test-harness').then(runTests)
}, [])
return 'index'
}
function runTests() {
it('should include RSC vary for app route', async () => {
const res = await fetch('/app')
expect(res.headers.get('vary')).toBe(RSC_VARY_HEADER)
const text = await res.text()
expect(text).toContain('<html')
expect(text).toContain('app')
})
it('should include RSC vary for flight route', async () => {
const res = await fetch('/app', {
headers: {
RSC: 1,
},
})
expect(res.headers.get('vary')).toBe(RSC_VARY_HEADER)
const text = await res.text()
expect(text).not.toContain('<html')
expect(text).toContain('app')
})
it('should not include RSC vary for page route', async () => {
const res = await fetch('/page')
const vary = res.headers.get('vary') || ''
expect(vary.split(/\s*,\s*/g)).not.toContain('RSC')
const text = await res.text()
expect(text).toContain('<html')
expect(text).toContain('page')
})
}
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