0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nuxt2でSSGしたページの一覧をとりたかった

Posted at

なぜか

  • nuxt2が一部で使われているが他環境も混ざっていて、かつCDNで配信されている環境がある。(レア!)
  • nuxtでビルドしたファイルだけキャッシュを消したい

解決策

nuxt.config.js に hooks を指定してビルドしたファイルのリストを書き出す

export default {
  hooks: {
    generate: {
      done(generator) {
        const filePath = `${generator.distPath}/buildList.json`;
        fs.writeFileSync(filePath, JSON.stringify(generator.manifest.routes), {
          encoding: 'utf-8',
        });
      },
    },
  },
};

後処理

パスのリストを読み込んで必要な処理を行う。
今回はcloudflontのキャッシュを削除したかったので以下の様なコマンドをCIを回すサーバから叩いています

paths=$(grep -oE '/[^"]+' dist/buildInfo.json | paste -sd "," -); aws cloudfront create-invalidation --distribution-id "XXXXXXXXXXXXX" --paths "/_nuxt/,$paths"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?