Skip to content

Commit

Permalink
Fix missing cache-control on SSR app route (#70265)
Browse files Browse the repository at this point in the history
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: #70213
# Conflicts:
#	packages/next/src/server/base-server.ts
  • Loading branch information
ijjk committed Sep 19, 2024
1 parent 6d7ced4 commit b503b8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2638,10 +2638,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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)) {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/app-dir/app/pages/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useRouter } from 'next/router'

export default function Page(props) {
return (
<>
<p>hello from ssr</p>
<p id="query">{JSON.stringify(useRouter().query)}</p>
</>
)
}

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

0 comments on commit b503b8e

Please sign in to comment.