コンテンツへスキップ
アプリケーションの構築デプロイ継続的インテグレーション (CI) ビルドキャッシュ

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

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

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

CIが.next/cacheをビルド間で永続化するように設定されていない場合、No Cache Detectedエラーが表示されることがあります。

一般的な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`, `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

その後、パイプラインの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"
        }
    }
}