Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the size of web server (#34767)
By moving `setRevalidateHeaders` to a separate file we can shake off the dependency of `etag` and a polyfill for `Buffer` from the web server (which brings hundreds of kilobytes). ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
- Loading branch information
Showing
5 changed files
with
38 additions
and
32 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
33 changes: 4 additions & 29 deletions
33
packages/next/server/send-payload.ts → packages/next/server/send-payload/index.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
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,30 @@ | ||
import type { ServerResponse } from 'http' | ||
import type { BaseNextResponse } from '../base-http' | ||
import type { PayloadOptions } from './index' | ||
|
||
export function setRevalidateHeaders( | ||
res: ServerResponse | BaseNextResponse, | ||
options: PayloadOptions | ||
) { | ||
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`) | ||
} | ||
} |
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