Skip to content

feat(server): implement brotli compression for server #17766

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion docs/api-reference/next.config.js/compression.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Next.js provides gzip compression to compress rendered content and

# Compression

Next.js provides [**gzip**](https://tools.ietf.org/html/rfc6713#section-3) compression to compress rendered content and static files. Compression only works with the [`server` target](/docs/api-reference/next.config.js/build-target.md#server-target). In general you will want to enable compression on a HTTP proxy like [nginx](https://www.nginx.com/), to offload load from the `Node.js` process.
Next.js provides [**brotli**](https://tools.ietf.org/html/rfc7932) and [**gzip**](https://tools.ietf.org/html/rfc6713#section-3) compression to compress rendered content and static files. Compression only works with the [`server` target](/docs/api-reference/next.config.js/build-target.md#server-target). In general you will want to enable compression on a HTTP proxy like [nginx](https://www.nginx.com/), to offload load from the `Node.js` process.

To disable **compression**, open `next.config.js` and disable the `compress` config:

Expand Down
1 change: 1 addition & 0 deletions packages/next/compiled/compression/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>
Copyright (c) 2020 Nick Randall <nick@nickrandall.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion packages/next/compiled/compression/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/compiled/compression/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"compression","main":"index.js","license":"MIT"}
{"name":"@nicksrandall/compression","main":"index.js","license":"MIT"}
14 changes: 9 additions & 5 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compression from 'next/dist/compiled/compression'
import Compression from 'next/dist/compiled/compression'
import fs from 'fs'
import chalk from 'next/dist/compiled/chalk'
import { IncomingMessage, ServerResponse } from 'http'
Expand Down Expand Up @@ -85,6 +85,7 @@ import { detectDomainLocale } from '../lib/i18n/detect-domain-locale'
import cookie from 'next/dist/compiled/cookie'

const getCustomRouteMatcher = pathMatch(true)
const noop = (): void => {}

type NextConfig = any

Expand Down Expand Up @@ -147,7 +148,7 @@ export default class Server {
locales?: string[]
defaultLocale?: string
}
private compression?: Middleware
private compression?: ReturnType<typeof Compression>
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
private incrementalCache: IncrementalCache
router: Router
Expand Down Expand Up @@ -209,7 +210,7 @@ export default class Server {
}

if (compress && this.nextConfig.target === 'server') {
this.compression = compression() as Middleware
this.compression = Compression()
}

// Initialize next/config with the environment configuration
Expand Down Expand Up @@ -1016,9 +1017,12 @@ export default class Server {
}))
}

private handleCompression(req: IncomingMessage, res: ServerResponse): void {
private async handleCompression(
req: IncomingMessage,
res: ServerResponse
): Promise<void> {
if (this.compression) {
this.compression(req, res, () => {})
this.compression(noop)(req, res)
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"@babel/preset-typescript": "7.12.1",
"@babel/types": "7.12.6",
"@next/polyfill-nomodule": "10.0.2-canary.1",
"@nicksrandall/compression": "1.0.3",
"@taskr/clear": "1.1.0",
"@taskr/esnext": "1.1.0",
"@taskr/watch": "1.1.0",
Expand All @@ -136,7 +137,6 @@
"@types/babel__traverse": "7.0.10",
"@types/ci-info": "2.0.0",
"@types/comment-json": "1.1.1",
"@types/compression": "0.0.36",
"@types/content-type": "1.1.3",
"@types/cookie": "0.3.3",
"@types/cross-spawn": "6.0.0",
Expand Down Expand Up @@ -173,7 +173,6 @@
"chalk": "2.4.2",
"ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540",
"comment-json": "3.0.3",
"compression": "1.7.4",
"conf": "5.0.0",
"content-type": "1.0.4",
"cookie": "0.4.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ export async function ncc_ci_info(task, opts) {
externals['compression'] = 'next/dist/compiled/compression'
export async function ncc_compression(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('compression')))
.ncc({ packageName: 'compression', externals })
.source(
opts.src ||
relative(__dirname, require.resolve('@nicksrandall/compression'))
)
.ncc({ packageName: '@nicksrandall/compression', externals })
.target('compiled/compression')
}
// eslint-disable-next-line camelcase
Expand Down
2 changes: 1 addition & 1 deletion packages/next/types/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ declare module 'next/dist/compiled/ci-info' {
export = m
}
declare module 'next/dist/compiled/compression' {
import m from 'compression'
import m from '@nicksrandall/compression'
export = m
}
declare module 'next/dist/compiled/conf' {
Expand Down
73 changes: 15 additions & 58 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@
lodash.camelcase "^4.3.0"
protobufjs "^6.8.6"

"@hapi/accept@5.0.1":
"@hapi/accept@5.0.1", "@hapi/accept@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@hapi/accept/-/accept-5.0.1.tgz#068553e867f0f63225a506ed74e899441af53e10"
integrity sha512-fMr4d7zLzsAXo28PRRQPXR1o2Wmu+6z+VY1UzDp0iFo13Twj8WePakwXBiqn3E1aAlTpSNzCXdnnQXFhst8h8Q==
Expand Down Expand Up @@ -2248,6 +2248,17 @@
call-me-maybe "^1.0.1"
glob-to-regexp "^0.3.0"

"@nicksrandall/compression@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@nicksrandall/compression/-/compression-1.0.3.tgz#00a286310ab672aec7e409c955e5217f69295a86"
integrity sha512-GxHsCL3sXDO9NODFOO6rSSh3X+hW49Pv5Q1AXB2RL2YZ1kxtNUsak3WvqgVTWzG43aQgIWR8eSQHVC3K7CY8Xg==
dependencies:
"@bitauth/libauth" "^1.17.2"
"@hapi/accept" "^5.0.1"
compressible "^2.0.18"
on-headers "^1.0.2"
vary "^1.1.2"

"@nodelib/fs.scandir@2.1.3":
version "2.1.3"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
Expand Down Expand Up @@ -2561,13 +2572,6 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/body-parser@*":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.17.1.tgz#18fcf61768fb5c30ccc508c21d6fd2e8b3bf7897"
dependencies:
"@types/connect" "*"
"@types/node" "*"

"@types/cacheable-request@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.1.tgz#5d22f3dded1fd3a84c0bbeb5039a7419c2c91976"
Expand Down Expand Up @@ -2596,18 +2600,6 @@
resolved "https://registry.yarnpkg.com/@types/comment-json/-/comment-json-1.1.1.tgz#b4ae889912a93e64619f97989aecaff8ce889dca"
integrity sha512-U70oEqvnkeSSp8BIJwJclERtT13rd9ejK7XkIzMCQQePZe3VW1b7iQggXyW4ZvfGtGeXD0pZw24q5iWNe++HqQ==

"@types/compression@0.0.36":
version "0.0.36"
resolved "https://registry.yarnpkg.com/@types/compression/-/compression-0.0.36.tgz#7646602ffbfc43ea48a8bf0b2f1d5e5f9d75c0d0"
dependencies:
"@types/express" "*"

"@types/connect@*":
version "3.4.33"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.33.tgz#31610c901eca573b8713c3330abc6e6b9f588546"
dependencies:
"@types/node" "*"

"@types/content-type@1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@types/content-type/-/content-type-1.1.3.tgz#3688bd77fc12f935548eef102a4e34c512b03a07"
Expand Down Expand Up @@ -2650,21 +2642,6 @@
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"

"@types/express-serve-static-core@*":
version "4.17.1"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.1.tgz#82be64a77211b205641e0209096fd3afb62481d3"
dependencies:
"@types/node" "*"
"@types/range-parser" "*"

"@types/express@*":
version "4.17.2"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.2.tgz#a0fb7a23d8855bac31bc01d5a58cadd9b2173e6c"
dependencies:
"@types/body-parser" "*"
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/fresh@0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@types/fresh/-/fresh-0.5.0.tgz#4d09231027d69c4369cfb01a9af5ef083d0d285f"
Expand Down Expand Up @@ -2845,10 +2822,6 @@
version "1.5.2"
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"

"@types/range-parser@*":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c"

"@types/react-dom@16.9.4":
version "16.9.4"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.9.4.tgz#0b58df09a60961dcb77f62d4f1832427513420df"
Expand Down Expand Up @@ -3246,7 +3219,7 @@ abort-controller@3.0.0, abort-controller@^3.0.0:
dependencies:
event-target-shim "^5.0.0"

accepts@~1.3.5, accepts@~1.3.7:
accepts@~1.3.7:
version "1.3.7"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd"
dependencies:
Expand Down Expand Up @@ -4288,10 +4261,6 @@ byte-size@^4.0.3:
version "4.0.4"
resolved "https://registry.yarnpkg.com/byte-size/-/byte-size-4.0.4.tgz#29d381709f41aae0d89c631f1c81aec88cd40b23"

bytes@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"

bytes@3.1.0, bytes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
Expand Down Expand Up @@ -5087,18 +5056,6 @@ compressible@~2.0.16:
dependencies:
mime-db ">= 1.43.0 < 2"

compression@1.7.4:
version "1.7.4"
resolved "https://registry.yarnpkg.com/compression/-/compression-1.7.4.tgz#95523eff170ca57c29a0ca41e6fe131f41e5bb8f"
dependencies:
accepts "~1.3.5"
bytes "3.0.0"
compressible "~2.0.16"
debug "2.6.9"
on-headers "~1.0.2"
safe-buffer "5.1.2"
vary "~1.1.2"

concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -11742,7 +11699,7 @@ on-finished@~2.3.0:
dependencies:
ee-first "1.1.1"

on-headers@~1.0.2:
on-headers@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f"

Expand Down Expand Up @@ -16698,7 +16655,7 @@ validate-npm-package-name@3.0.0, validate-npm-package-name@^3.0.0:
dependencies:
builtins "^1.0.3"

vary@^1, vary@~1.1.2:
vary@^1, vary@^1.1.2, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

Expand Down