为 nextjs 默认服务器上的静态文件设置缓存控制标头 [英] Setting cache-control header for static file serving on nextjs default server
问题描述
我正在使用默认的 nextjs 服务器通过此命令运行我的 nextjs 程序 next start
.
I am using the default nextjs server to run my nextjs program by this command next start
.
但是,我无法更改公用文件夹下文件的缓存控制标头.
However, I am not able to change the cache-control header for the files under the public folder.
有什么方法可以在不设置自定义服务器的情况下设置缓存控制头吗?
Is there any method to set the cache-control header without setting the Custom Server?
推荐答案
存在未记录的功能或错误,但可以.更多信息可以在这里找到 https://nextjs.org/docs/api-reference/next.config.js/headers
There is undocumented feature or bug, but it works. More info can be found here https://nextjs.org/docs/api-reference/next.config.js/headers
将配置添加到您的 next.config.js
文件中,例如:
Add config to you next.config.js
file for example:
async headers() {
return [
{
source: '/:all*(svg|jpg|png)',
locale: false,
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=9999999999, must-revalidate',
}
],
},
]
},
这篇关于为 nextjs 默认服务器上的静态文件设置缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!