Continuous Integration (CI) Build Caching

To improve build performance, Next.js saves a cache to .next/cache that is shared between builds.

To take advantage of this cache in Continuous Integration (CI) environments, your CI workflow will need to be configured to correctly persist the cache between builds.

If your CI is not configured to persist .next/cache between builds, you may see a No Cache Detected error.

Here are some example cache configurations for common CI providers:

Vercel

Next.js caching is automatically configured for you. There’s no action required on your part.

CircleCI

Edit your save_cache step in .circleci/config.yml to include .next/cache:

  1. steps:
  2. - save_cache:
  3. key: dependency-cache-{{ checksum "yarn.lock" }}
  4. paths:
  5. - ./node_modules
  6. - ./.next/cache

If you do not have a save_cache key, please follow CircleCI’s documentation on setting up build caching.

Travis CI

Add or merge the following into your .travis.yml:

  1. cache:
  2. directories:
  3. - $HOME/.cache/yarn
  4. - node_modules
  5. - .next/cache

GitLab CI

Add or merge the following into your .gitlab-ci.yml:

  1. cache:
  2. key: ${CI_COMMIT_REF_SLUG}
  3. paths:
  4. - node_modules/
  5. - .next/cache/

Netlify CI

Use Netlify Plugins with @netlify/plugin-nextjs.

AWS CodeBuild

Add (or merge in) the following to your buildspec.yml:

  1. cache:
  2. paths:
  3. - 'node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
  4. - '.next/cache/**/*' # Cache Next.js for faster application rebuilds

GitHub Actions

Using GitHub’s actions/cache, add the following step in your workflow file:

  1. uses: actions/cache@v2
  2. with:
  3. # 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
  4. path: |
  5. ~/.npm
  6. ${{ github.workspace }}/.next/cache
  7. # Generate a new cache whenever packages or source files change.
  8. key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
  9. # If source files changed but packages didn't, rebuild from a prior cache.
  10. restore-keys: |
  11. ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-

Bitbucket Pipelines

Add or merge the following into your bitbucket-pipelines.yml at the top level (same level as pipelines):

  1. definitions:
  2. caches:
  3. nextcache: .next/cache

Then reference it in the caches section of your pipeline’s step:

  1. - step:
  2. name: your_step_name
  3. caches:
  4. - node
  5. - nextcache

Heroku

Using Heroku’s custom cache, add a cacheDirectories array in your top-level package.json:

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

Azure Pipelines

Using Azure Pipelines’ Cache task, add the following task to your pipeline yaml file somewhere prior to the task that executes next build:

  1. - task: Cache@2
  2. displayName: 'Cache .next/cache'
  3. inputs:
  4. key: next | $(Agent.OS) | yarn.lock
  5. path: '$(System.DefaultWorkingDirectory)/.next/cache'