Nuxt SSR 時における static ディレクトリの Cache-Control
Published: 2022/4/9
問題
nuxt start
で Nuxt を SSR で利用する場合、デフォルト設定においては、 _nuxt
以下のビルドされた asset については Cache-Control: public, max-age=31536000
すなわち、できるかぎりキャッシュしろ、という意図の Cache-Control
ヘッダーが付与されているが、 static
直下に配置した静的ファイルについては、 Cache-Control: public, max-age=0
が付与される。
これは、 CDN を前段に入れている場合などにおいては、「キャッシュをするな」という指示になるので、あまりよろしくない。
対処方法
nuxt static cached headers · Issue #2334 · nuxt/nuxt.js
looking for some help debugging why Google pagespeed is marking all my assets in the static folder as being served without cache headers. https://ipfs.io/ipfs/QmeXHLfXwT5nhMrjrhzv97Cs7hLi78QPF24qFv...
github.com
上記にある通り、 nuxt.config.ts
において、 render.static
に適切なオプションを指定する。
例:
const config = {
// ...
render: {
static: { maxAge: '1y' }
},
// ...
}
export default config