コンテンツにスキップ

継続的インテグレーション (CI) ビルドキャッシュ

ビルドのパフォーマンスを向上させるために、Next.js はビルド間で共有されるキャッシュを .next/cache に保存します。

継続的インテグレーション (CI) 環境でこのキャッシュを活用するには、ビルド間でキャッシュが正しく永続化されるように CI ワークフローを設定する必要があります。

CI がビルド間で .next/cache を永続化するように設定されていない場合、キャッシュが検出されません エラーが表示されることがあります。

一般的な CI プロバイダーのキャッシュ設定例を以下に示します

Vercel

Next.js のキャッシュは自動的に設定されます。何もする必要はありません。Vercel で Turborepo を使用している場合は、こちらをご覧ください

CircleCI

.circleci/config.ymlsave_cache ステップを編集して、.next/cache を含めます

steps:
  - save_cache:
      key: dependency-cache-{{ checksum "yarn.lock" }}
      paths:
        - ./node_modules
        - ./.next/cache

save_cache キーがない場合は、CircleCI の ビルドキャッシュの設定に関するドキュメントに従ってください。

Travis CI

以下を .travis.yml に追加またはマージします

cache:
  directories:
    - $HOME/.cache/yarn
    - node_modules
    - .next/cache

GitLab CI

以下を .gitlab-ci.yml に追加またはマージします

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .next/cache/

Netlify CI

Netlifyプラグイン@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 rebuilds

GitHub Actions

GitHubの actions/cache を使用して、ワークフローファイルに以下のステップを追加します。

uses: actions/cache@v4
with:
  # See here for caching with `yarn` https://github.com/actions/cache/blob/main/examples.md#node---yarn 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

その後、パイプラインの stepcaches セクションで参照します。

- step:
    name: your_step_name
    caches:
      - node
      - nextcache

Heroku

Herokuの カスタムキャッシュ を使用して、トップレベルのpackage.jsonに cacheDirectories 配列を追加します。

"cacheDirectories": [".next/cache"]

Azure Pipelines

Azure Pipelinesの キャッシュタスク を使用して、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"
        }
    }
}