継続的インテグレーション(CI)ビルドキャッシュの設定方法
ビルドパフォーマンスを向上させるために、Next.js はビルド間で共有されるキャッシュを .next/cache に保存します。
継続的インテグレーション(CI)環境でこのキャッシュを活用するには、CI ワークフローがビルド間でキャッシュを正しく永続化するように設定する必要があります。
CI がビルド間で
.next/cacheを永続化するように設定されていない場合、キャッシュが見つかりませんというエラーが発生する可能性があります。
一般的な CI プロバイダー向けのキャッシュ設定例を以下に示します。
Vercel
Next.js のキャッシュは自動的に設定されます。お客様による操作は不要です。Vercel で Turborepo を使用している場合は、こちらで詳細を確認してください。
CircleCI
.circleci/config.yml の save_cache ステップを編集して、.next/cache を含めてください。
steps:
- save_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- ./.next/cachesave_cache キーがない場合は、CircleCI の ビルドキャッシュの設定に関するドキュメント を参照してください。
Travis CI
.travis.yml に以下を追加またはマージしてください。
cache:
directories:
- $HOME/.cache/yarn
- node_modules
- .next/cacheGitLab CI
.gitlab-ci.yml に以下を追加またはマージしてください。
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/Netlify CI
@netlify/plugin-nextjs で Netlify Plugins を使用します。@netlify/plugin-nextjs。
AWS CodeBuild
buildspec.yml に以下を追加(またはマージ)してください。
cache:
paths:
- 'node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
- '.next/cache/**/*' # Cache Next.js for faster application rebuildsGitHub Actions
GitHub の actions/cache を使用して、ワークフローファイルに以下のステップを追加してください。
uses: actions/cache@v4
with:
# See here for caching with `yarn`, `bun` or other package managers https://github.com/actions/cache/blob/main/examples.md or you can leverage caching with actions/setup-node https://github.com/actions/setup-node
path: |
~/.npm
${{ github.workspace }}/.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-Bitbucket Pipelines
bitbucket-pipelines.yml のトップレベル(pipelines と同じレベル)に以下を追加またはマージしてください。
definitions:
caches:
nextcache: .next/cacheその後、パイプラインの step の caches セクションで参照してください。
- step:
name: your_step_name
caches:
- node
- nextcacheHeroku
Heroku の カスタムキャッシュ を使用して、トップレベルの package.json に cacheDirectories 配列を追加してください。
"cacheDirectories": [".next/cache"]Azure Pipelines
Azure Pipelines の Cache task を使用して、next build を実行するタスクよりも前のパイプライン YAML ファイルに以下のタスクを追加してください。
- task: Cache@2
displayName: 'Cache .next/cache'
inputs:
key: next | $(Agent.OS) | yarn.lock
path: '$(System.DefaultWorkingDirectory)/.next/cache'Jenkins (Pipeline)
Jenkins の Job Cacher プラグインを使用して、next build または npm install を通常実行する場所の Jenkinsfile に以下のビルドステップを追加してください。
stage("Restore npm packages") {
steps {
// Writes lock-file to cache based on the GIT_COMMIT hash
writeFile file: "next-lock.cache", text: "$GIT_COMMIT"
cache(caches: [
arbitraryFileCache(
path: "node_modules",
includes: "**/*",
cacheValidityDecidingFile: "package-lock.json"
)
]) {
sh "npm install"
}
}
}
stage("Build") {
steps {
// Writes lock-file to cache based on the GIT_COMMIT hash
writeFile file: "next-lock.cache", text: "$GIT_COMMIT"
cache(caches: [
arbitraryFileCache(
path: ".next/cache",
includes: "**/*",
cacheValidityDecidingFile: "next-lock.cache"
)
]) {
// aka `next build`
sh "npm run build"
}
}
}役に立ちましたか?