-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to customize Cache-Control (#69802)
This continues #39707 bringing the changes up to date with canary and adds test cases to ensure it's working as expected. Closes: #22319 Closes: #39707 Closes: NDX-148
- Loading branch information
Showing
13 changed files
with
266 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
test/e2e/app-dir/custom-cache-control/app/app-ssg/[slug]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const revalidate = 120 | ||
|
||
export function generateStaticParams() { | ||
return [ | ||
{ | ||
slug: 'first', | ||
}, | ||
] | ||
} | ||
|
||
export default function Page({ params }) { | ||
return ( | ||
<> | ||
<p>/app-ssg/[slug]</p> | ||
<p>{JSON.stringify(params)}</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const dynamic = 'force-dynamic' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p>/app-ssr</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
84 changes: 84 additions & 0 deletions
84
test/e2e/app-dir/custom-cache-control/custom-cache-control.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('custom-cache-control', () => { | ||
const { next, isNextDev, isNextDeploy } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
if (isNextDeploy) { | ||
// customizing these headers won't apply on environments | ||
// where headers are applied outside of the Next.js server | ||
it('should skip for deploy', () => {}) | ||
return | ||
} | ||
|
||
it('should have custom cache-control for app-ssg prerendered', async () => { | ||
const res = await next.fetch('/app-ssg/first') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=30' | ||
) | ||
}) | ||
|
||
it('should have custom cache-control for app-ssg lazy', async () => { | ||
const res = await next.fetch('/app-ssg/lazy') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=31' | ||
) | ||
}) | ||
;(process.env.__NEXT_EXPERIMENTAL_PPR ? it.skip : it)( | ||
'should have default cache-control for app-ssg another', | ||
async () => { | ||
const res = await next.fetch('/app-ssg/another') | ||
// eslint-disable-next-line jest/no-standalone-expect | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev | ||
? 'no-store, must-revalidate' | ||
: 's-maxage=120, stale-while-revalidate' | ||
) | ||
} | ||
) | ||
|
||
it('should have custom cache-control for app-ssr', async () => { | ||
const res = await next.fetch('/app-ssr') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=32' | ||
) | ||
}) | ||
|
||
it('should have custom cache-control for auto static page', async () => { | ||
const res = await next.fetch('/pages-auto-static') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=33' | ||
) | ||
}) | ||
|
||
it('should have custom cache-control for pages-ssg prerendered', async () => { | ||
const res = await next.fetch('/pages-ssg/first') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=34' | ||
) | ||
}) | ||
|
||
it('should have custom cache-control for pages-ssg lazy', async () => { | ||
const res = await next.fetch('/pages-ssg/lazy') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=35' | ||
) | ||
}) | ||
|
||
it('should have default cache-control for pages-ssg another', async () => { | ||
const res = await next.fetch('/pages-ssg/another') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev | ||
? 'no-store, must-revalidate' | ||
: 's-maxage=120, stale-while-revalidate' | ||
) | ||
}) | ||
|
||
it('should have default cache-control for pages-ssr', async () => { | ||
const res = await next.fetch('/pages-ssr') | ||
expect(res.headers.get('cache-control')).toBe( | ||
isNextDev ? 'no-store, must-revalidate' : 's-maxage=36' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
headers() { | ||
return [ | ||
{ | ||
source: '/app-ssg/first', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=30', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/app-ssg/lazy', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=31', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/app-ssr', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=32', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/pages-auto-static', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=33', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/pages-ssg/first', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=34', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/pages-ssg/lazy', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=35', | ||
}, | ||
], | ||
}, | ||
{ | ||
source: '/pages-ssr', | ||
headers: [ | ||
{ | ||
key: 'Cache-Control', | ||
value: 's-maxage=36', | ||
}, | ||
], | ||
}, | ||
] | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/custom-cache-control/pages/pages-auto-static.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Page() { | ||
return ( | ||
<> | ||
<p>/pages-auto-static</p> | ||
</> | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
test/e2e/app-dir/custom-cache-control/pages/pages-ssg/[slug].tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export function getStaticProps({ params }) { | ||
return { | ||
props: { | ||
now: Date.now(), | ||
params, | ||
}, | ||
revalidate: 120, | ||
} | ||
} | ||
|
||
export function getStaticPaths() { | ||
return { | ||
paths: [ | ||
{ | ||
params: { slug: 'first' }, | ||
}, | ||
], | ||
fallback: 'blocking', | ||
} | ||
} | ||
|
||
export default function Page({ params }) { | ||
return ( | ||
<> | ||
<p>/pages-ssg/[slug]</p> | ||
<p>{JSON.stringify(params)}</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function getServerSideProps() { | ||
return { | ||
props: { | ||
now: Date.now(), | ||
}, | ||
} | ||
} | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p>/pages-ssr</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters