Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cache-control is correct when returning redirect #37958

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,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