Skip to content

Commit

Permalink
Fix missing hasHeader mock during revalidate (vercel#45681)
Browse files Browse the repository at this point in the history
Replaces usage of `hasHeader` of `getHeader` and also ensures we include
`hasHeader` in our `mockRes` we create during revalidate for good
measure.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

Closes: vercel#34929
Closes: vercel#37338
Closes: vercel#45481
  • Loading branch information
ijjk committed Feb 8, 2023
1 parent e36fb42 commit 434a8cb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/next/src/server/lib/mock-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function mockRequest(

mockRes.writeHead = (_status: any, _headers: any) =>
Object.assign(mockHeaders, _headers)
mockRes.hasHeader = (name: string) => Boolean(mockHeaders[name.toLowerCase()])
mockRes.getHeader = (name: string) => mockHeaders[name.toLowerCase()]
mockRes.getHeaders = () => mockHeaders
mockRes.getHeaderNames = () => Object.keys(mockHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function setRevalidateHeaders(
options: PayloadOptions
) {
if (options.private || options.stateful) {
if (options.private || !res.hasHeader('Cache-Control')) {
if (options.private || !res.getHeader('Cache-Control')) {
res.setHeader(
'Cache-Control',
`private, no-cache, no-store, max-age=0, must-revalidate`
Expand Down
3 changes: 1 addition & 2 deletions run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,9 @@ async function main() {
}
}
}
await cleanUpAndExit(0)
}

main().catch((err) => {
console.error(err)
process.exit(1)
cleanUpAndExit(1)
})
17 changes: 17 additions & 0 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import stripAnsi from 'strip-ansi'

describe('Prerender', () => {
let next: NextInstance
Expand Down Expand Up @@ -1526,6 +1527,12 @@ describe('Prerender', () => {
),
page: '/something',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(next.buildId)}\\/ssr.json$`
),
page: '/ssr',
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
Expand Down Expand Up @@ -1772,6 +1779,16 @@ describe('Prerender', () => {
})
}

it('should not throw error for manual revalidate for SSR path', async () => {
const res = await fetchViaHTTP(next.url, '/api/manual-revalidate', {
pathname: '/ssr',
})

expect(res.status).toBe(200)
expect(await res.json()).toEqual({ revalidated: false })
expect(stripAnsi(next.cliOutput)).not.toContain('hasHeader')
})

it('should revalidate manual revalidate with preview cookie', async () => {
const initialRes = await fetchViaHTTP(next.url, '/preview')
expect(initialRes.status).toBe(200)
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/prerender/pages/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getServerSideProps() {
return { props: {} }
}

export default function Page() {
return <p>/ssr</p>
}

0 comments on commit 434a8cb

Please sign in to comment.