Skip to main content

compress

默认情况下,Next.js 在使用 next start 或自定义服务器时使用 gzip 来压缩渲染内容和静态文件。这是针对未配置压缩的应用的优化。如果你的应用中已通过自定义服务器配置了压缩,则 Next.js 将不会添加压缩。

¥By default, Next.js uses gzip to compress rendered content and static files when using next start or a custom server. This is an optimization for applications that do not have compression configured. If compression is already configured in your application via a custom server, Next.js will not add compression.

很高兴知道:

¥Good to know:

  • Vercel 上托管应用时,压缩首先使用 brotli,然后使用 gzip

    ¥When hosting your application on Vercel, compression uses brotli first, then gzip.

  • 你可以通过查看响应中的 Accept-Encoding(浏览器接受的选项)和 Content-Encoding(当前使用的)标头来检查是否启用了压缩以及使用哪种算法。

    ¥You can check if compression is enabled and which algorithm is used by looking at the Accept-Encoding (browser accepted options) and Content-Encoding (currently used) headers in the response.

禁用压缩

¥Disabling compression

要禁用压缩,请将 compress 配置选项设置为 false

¥To disable compression, set the compress config option to false:

module.exports = {
compress: false,
}

我们不建议禁用压缩,除非你在服务器上配置了压缩,因为压缩可以减少带宽使用并提高应用的性能。

¥We do not recommend disabling compression unless you have compression configured on your server, as compression reduces bandwidth usage and improves the performance of your application.

更改压缩算法

¥Changing the compression algorithm

要更改压缩算法,你需要配置自定义服务器并将 next.config.js 文件中的 compress 选项设置为 false

¥To change your compression algorithm, you will need to configure your custom server and set the compress option to false in your next.config.js file.

例如,你正在使用 nginx 并且想要切换到 brotli,请将 compress 选项设置为 false 以允许 nginx 处理压缩。

¥For example, you're using nginx and want to switch to brotli, set the compress option to false to allow nginx to handle compression.

很高兴知道:

¥Good to know:

  • 对于 Vercel 上的 Next.js 应用,压缩由 Vercel 的边缘网络而不是 Next.js 处理。请参阅 Vercel 文档 了解更多信息。

    ¥For Next.js applications on Vercel, compression is handled by the Vercel's Edge Network and not Next.js. See the Vercel documentation for more information.