-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct Cache-Control Behavior for GS(S)P (#11022)
* Correct Cache-Control Behavior for GS(S)P * remove old line * fix test
- Loading branch information
Showing
6 changed files
with
136 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ServerResponse } from 'http' | ||
import { isResSent } from '../lib/utils' | ||
|
||
export function sendPayload( | ||
res: ServerResponse, | ||
payload: any, | ||
type: 'html' | 'json', | ||
options?: | ||
| { private: true } | ||
| { private: boolean; stateful: true } | ||
| { private: boolean; stateful: false; revalidate: number | false } | ||
): void { | ||
if (isResSent(res)) { | ||
return | ||
} | ||
|
||
// TODO: ETag headers? | ||
res.setHeader( | ||
'Content-Type', | ||
type === 'json' ? 'application/json' : 'text/html; charset=utf-8' | ||
) | ||
res.setHeader('Content-Length', Buffer.byteLength(payload)) | ||
if (options != null) { | ||
if (options.private || options.stateful) { | ||
if (options.private || !res.hasHeader('Cache-Control')) { | ||
res.setHeader( | ||
'Cache-Control', | ||
`private, no-cache, no-store, max-age=0, must-revalidate` | ||
) | ||
} | ||
} else if (typeof options.revalidate === 'number') { | ||
if (options.revalidate < 1) { | ||
throw new Error( | ||
`invariant: invalid Cache-Control duration provided: ${options.revalidate} < 1` | ||
) | ||
} | ||
|
||
res.setHeader( | ||
'Cache-Control', | ||
`s-maxage=${options.revalidate}, stale-while-revalidate` | ||
) | ||
} else if (options.revalidate === false) { | ||
res.setHeader( | ||
'Cache-Control', | ||
`s-maxage=31536000, stale-while-revalidate` | ||
) | ||
} | ||
} | ||
res.end(payload) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react' | ||
|
||
export async function getServerSideProps({ res }) { | ||
res.setHeader('Cache-Control', 'public, max-age=3600') | ||
return { | ||
props: { world: 'world' }, | ||
} | ||
} | ||
|
||
export default ({ world }) => { | ||
return <p>hello: {world}</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