From b503b8eb521a3ea5e774905c202e6c3edb6edaa2 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 19 Sep 2024 14:45:46 -0700 Subject: [PATCH] Fix missing cache-control on SSR app route (#70265) This removes an inaccurate check that doesn't set a revalidate value if revalidate is `undefined` which can be the case for SSR app route pages. Also adds a regression test to ensure this doesn't break again. Fixes: https://github.com/vercel/next.js/issues/70213 # Conflicts: # packages/next/src/server/base-server.ts --- packages/next/src/server/base-server.ts | 5 +---- test/e2e/app-dir/app/index.test.ts | 12 ++++++++++++ test/e2e/app-dir/app/pages/ssr.js | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/e2e/app-dir/app/pages/ssr.js diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index deca9b2c32e99..a127a96627007 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -2638,10 +2638,7 @@ export default abstract class Server { opts.experimental.ppr ) { revalidate = 0 - } else if ( - typeof cacheEntry.revalidate !== 'undefined' && - (!this.renderOpts.dev || (hasServerProps && !isNextDataRequest)) - ) { + } else if (!this.renderOpts.dev || (hasServerProps && !isNextDataRequest)) { // If this is a preview mode request, we shouldn't cache it. We also don't // cache 404 pages. if (isPreviewMode || (is404Page && !isNextDataRequest)) { diff --git a/test/e2e/app-dir/app/index.test.ts b/test/e2e/app-dir/app/index.test.ts index acd75f7f29aa9..e70e36758c4a0 100644 --- a/test/e2e/app-dir/app/index.test.ts +++ b/test/e2e/app-dir/app/index.test.ts @@ -36,6 +36,18 @@ describe('app dir - basic', () => { }) } + if (isNextStart) { + it('should have correct cache-control for SSR routes', async () => { + for (const path of ['/catch-all/first', '/ssr']) { + const res = await next.fetch(path) + expect(res.status).toBe(200) + expect(res.headers.get('Cache-Control')).toBe( + 'private, no-cache, no-store, max-age=0, must-revalidate' + ) + } + }) + } + if (process.env.NEXT_EXPERIMENTAL_COMPILE) { it('should provide query for getStaticProps page correctly', async () => { const res = await next.fetch('/ssg?hello=world') diff --git a/test/e2e/app-dir/app/pages/ssr.js b/test/e2e/app-dir/app/pages/ssr.js new file mode 100644 index 0000000000000..5f554b1f32a3d --- /dev/null +++ b/test/e2e/app-dir/app/pages/ssr.js @@ -0,0 +1,16 @@ +import { useRouter } from 'next/router' + +export default function Page(props) { + return ( + <> +

hello from ssr

+

{JSON.stringify(useRouter().query)}

+ + ) +} + +export function getServerSideProps() { + return { + props: {}, + } +}