Skip to content

Commit

Permalink
Reduce the size of web server (#34767)
Browse files Browse the repository at this point in the history
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
shuding committed Feb 24, 2022
1 parent 1a0d149 commit c9863c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/next/server/base-server.ts
Expand Up @@ -16,6 +16,7 @@ import type { CacheFs } from '../shared/lib/utils'
import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { PayloadOptions } from './send-payload'

import { join, resolve } from 'path'
import { parse as parseQs } from 'querystring'
Expand All @@ -42,7 +43,7 @@ import * as envConfig from '../shared/lib/runtime-config'
import { DecodeError, normalizeRepeatedSlashes } from '../shared/lib/utils'
import { isTargetLikeServerless } from './utils'
import Router, { replaceBasePath, route } from './router'
import { PayloadOptions, setRevalidateHeaders } from './send-payload'
import { setRevalidateHeaders } from './send-payload/revalidate-headers'
import { IncrementalCache } from './incremental-cache'
import { execOnce } from '../shared/lib/utils'
import { isBlockedPage, isBot } from './utils'
Expand Down
@@ -1,42 +1,17 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { BaseNextResponse } from './base-http'

import { isResSent } from '../shared/lib/utils'
import { isResSent } from '../../shared/lib/utils'
import generateETag from 'next/dist/compiled/etag'
import fresh from 'next/dist/compiled/fresh'
import RenderResult from './render-result'
import RenderResult from '../render-result'
import { setRevalidateHeaders } from './revalidate-headers'

export type PayloadOptions =
| { private: true }
| { private: boolean; stateful: true }
| { private: boolean; stateful: false; revalidate: number | false }

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`)
}
}
export { setRevalidateHeaders }

export async function sendRenderResult({
req,
Expand Down
30 changes: 30 additions & 0 deletions packages/next/server/send-payload/revalidate-headers.ts
@@ -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`)
}
}
2 changes: 1 addition & 1 deletion test/integration/production-swcminify/test/index.test.js
Expand Up @@ -75,7 +75,7 @@ describe.skip('Production Usage with swcMinify', () => {
expect(serverTrace.version).toBe(1)
expect(
serverTrace.files.some((file) =>
file.includes('next/dist/server/send-payload.js')
file.includes('next/dist/server/send-payload/index.js')
)
).toBe(true)
expect(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/production/test/index.test.js
Expand Up @@ -95,7 +95,7 @@ describe('Production Usage', () => {
expect(serverTrace.version).toBe(1)
expect(
serverTrace.files.some((file) =>
file.includes('next/dist/server/send-payload.js')
file.includes('next/dist/server/send-payload/index.js')
)
).toBe(true)
expect(
Expand Down

0 comments on commit c9863c8

Please sign in to comment.