Unverified Commit 32a9c355 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix another case with app router revalidation (#51076)

This ensures we don't attempt streaming a response during revalidation
in deployed environments.

x-ref: https://github.com/vercel/next.js/pull/51062
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1686216024812579)
parent ccc64d96
...@@ -1326,7 +1326,13 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1326,7 +1326,13 @@ export default abstract class Server<ServerOptions extends Options = Options> {
hasStaticPaths = true hasStaticPaths = true
} }
if (hasFallback || staticPaths?.includes(resolvedUrlPathname)) { if (
hasFallback ||
staticPaths?.includes(resolvedUrlPathname) ||
// this signals revalidation in deploy environments
// TODO: make this more generic
req.headers['x-now-route-matches']
) {
isSSG = true isSSG = true
} else if (!this.renderOpts.dev) { } else if (!this.renderOpts.dev) {
const manifest = this.getPrerenderManifest() const manifest = this.getPrerenderManifest()
......
export const revalidate = 1 export const revalidate = 1
export default function Page() { export default function Page() {
console.log('rendering app-another')
return ( return (
<> <>
<p>/app-blog</p> <p>/app-another</p>
<p>Date: {Date.now()}</p> <p>Date: {Date.now()}</p>
</> </>
) )
......
export const revalidate = 1
export function generateStaticParams() {
return [
{
slug: ['compare'],
},
]
}
export default function Page({ params: { slug } }) {
console.log('rendering app-blog')
return (
<>
<p>/app-blog</p>
<p>slug {JSON.stringify(slug)}</p>
<p>Date: {Date.now()}</p>
</>
)
}
...@@ -38,13 +38,18 @@ describe('minimal-mode-response-cache', () => { ...@@ -38,13 +38,18 @@ describe('minimal-mode-response-cache', () => {
} }
} }
const files = glob.sync('**/*', { const files = glob.sync('**/*', {
cwd: join(next.testDir, 'standalone/.next/server/pages'), cwd: join(next.testDir, 'standalone/.next/server'),
nodir: true,
dot: true, dot: true,
}) })
for (const file of files) { for (const file of files) {
if (file.endsWith('.json') || file.endsWith('.html')) { if (file.match(/(pages|app)[/\\]/) && !file.endsWith('.js')) {
await fs.remove(join(next.testDir, '.next/server', file)) await fs.remove(join(next.testDir, 'standalone/.next/server', file))
console.log(
'removing',
join(next.testDir, 'standalone/.next/server', file)
)
} }
} }
...@@ -81,23 +86,29 @@ describe('minimal-mode-response-cache', () => { ...@@ -81,23 +86,29 @@ describe('minimal-mode-response-cache', () => {
if (server) await killApp(server) if (server) await killApp(server)
}) })
it('app router revalidate should work with previous response cache', async () => { it('app router revalidate should work with previous response cache dynamic', async () => {
const res1 = await fetchViaHTTP(appPort, '/app-blog.rsc', undefined, { const headers = {
headers: { vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
'x-matched-path': '/app-blog.rsc', 'x-now-route-matches': '1=compare&rsc=1',
RSC: '1', 'x-matched-path': '/app-blog/compare.rsc',
}, 'x-vercel-id': '1',
}) rsc: '1',
}
const res1 = await fetchViaHTTP(
appPort,
'/app-blog/compare.rsc',
undefined,
{
headers,
}
)
const content1 = await res1.text() const content1 = await res1.text()
expect(content1).not.toContain('<html') expect(content1).not.toContain('<html')
expect(content1).toContain('app-blog') expect(content1).toContain('app-blog')
expect(res1.headers.get('content-type')).toContain('text/x-component') expect(res1.headers.get('content-type')).toContain('text/x-component')
const res2 = await fetchViaHTTP(appPort, '/app-blog', undefined, { const res2 = await fetchViaHTTP(appPort, '/app-blog/compare', undefined, {
headers: { headers,
'x-matched-path': '/app-blog.rsc',
RSC: '1',
},
}) })
const content2 = await res2.text() const content2 = await res2.text()
expect(content2).toContain('<html') expect(content2).toContain('<html')
...@@ -105,6 +116,31 @@ describe('minimal-mode-response-cache', () => { ...@@ -105,6 +116,31 @@ describe('minimal-mode-response-cache', () => {
expect(res2.headers.get('content-type')).toContain('text/html') expect(res2.headers.get('content-type')).toContain('text/html')
}) })
it('app router revalidate should work with previous response cache', async () => {
const headers = {
vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
'x-now-route-matches': '1=app-another&rsc=1',
'x-matched-path': '/app-another.rsc',
'x-vercel-id': '1',
rsc: '1',
}
const res1 = await fetchViaHTTP(appPort, '/app-another.rsc', undefined, {
headers,
})
const content1 = await res1.text()
expect(content1).not.toContain('<html')
expect(content1).toContain('app-another')
expect(res1.headers.get('content-type')).toContain('text/x-component')
const res2 = await fetchViaHTTP(appPort, '/app-another', undefined, {
headers,
})
const content2 = await res2.text()
expect(content2).toContain('<html')
expect(content2).toContain('app-another')
expect(res2.headers.get('content-type')).toContain('text/html')
})
it('should have correct "Listening on" log', async () => { it('should have correct "Listening on" log', async () => {
expect(output).toContain(`Listening on port`) expect(output).toContain(`Listening on port`)
expect(output).toContain(`url: http://localhost:${appPort}`) expect(output).toContain(`url: http://localhost:${appPort}`)
......
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