Skip to content

Commit

Permalink
Ensure cache-control is correct when returning redirect (#37958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Jun 23, 2022
1 parent 95449e6 commit 7c484a8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return null
}
} else if (cachedData.kind === 'REDIRECT') {
if (revalidateOptions) {
setRevalidateHeaders(res, revalidateOptions)
}
if (isDataReq) {
return {
type: 'json',
Expand Down
100 changes: 100 additions & 0 deletions test/production/required-server-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,106 @@ describe('should set-up next', () => {
if (server) await killApp(server)
})

it.each([
{
case: 'redirect no revalidate',
path: '/optional-ssg/redirect-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
},
{
case: 'redirect with revalidate',
path: '/optional-ssg/redirect-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
},
])(
`should have correct cache-control for $case`,
async ({ path, dest, cacheControl }) => {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
dest
)
expect(res.headers.get('cache-control')).toBe(cacheControl)

const dataRes = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}${path}.json`,
undefined,
{
redirect: 'manual',
}
)
expect(dataRes.headers.get('cache-control')).toBe(cacheControl)
expect((await dataRes.json()).pageProps).toEqual({
__N_REDIRECT: dest,
__N_REDIRECT_STATUS: 307,
})
}
)

it.each([
{
case: 'notFound no revalidate',
path: '/optional-ssg/not-found-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
},
{
case: 'notFound with revalidate',
path: '/optional-ssg/not-found-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
},
])(
`should have correct cache-control for $case`,
async ({ path, dest, cacheControl }) => {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(404)
expect(res.headers.get('cache-control')).toBe(cacheControl)

const dataRes = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}${path}.json`,
undefined,
{
redirect: 'manual',
}
)
expect(dataRes.headers.get('cache-control')).toBe(cacheControl)
}
)

it('should have the correct cache-control for props with no revalidate', async () => {
const res = await fetchViaHTTP(appPort, '/optional-ssg/props-no-revalidate')
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#props').text()).params).toEqual({
rest: ['props-no-revalidate'],
})

const dataRes = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/optional-ssg/props-no-revalidate.json`,
undefined
)
expect(dataRes.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
expect((await dataRes.json()).pageProps.params).toEqual({
rest: ['props-no-revalidate'],
})
})

it('should warn when "next" is imported directly', async () => {
await renderViaHTTP(appPort, '/gssp')
await check(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
export const getStaticProps = ({ params }) => {
console.log('getStaticProps /optional-ssg/[[...rest]]', params)

switch (params.rest?.[0]) {
case 'redirect-1': {
return {
redirect: { destination: '/somewhere', permanent: false },
}
}
case 'redirect-2': {
return {
redirect: { destination: '/somewhere-else', permanent: false },
revalidate: 5,
}
}
case 'not-found-1': {
return {
notFound: true,
}
}
case 'not-found-2': {
return {
notFound: true,
revalidate: 5,
}
}
case 'props-no-revalidate': {
return {
props: {
random: Math.random(),
params: params || null,
},
}
}
default: {
break
}
}

return {
props: {
random: Math.random(),
Expand Down

0 comments on commit 7c484a8

Please sign in to comment.