Next.js Discord

Discord Forum

Cache optimization using pnpm, next and nx

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Avatar
Giant pandaOP
I'm using a monorepo solution called nx to manage several next apps. I also use github actions to run my CIs and I'm struggling to cache properly my builds.
Here's the relevant workflow steps:
- name: Install pnpm
  uses: pnpm/action-setup@v2
  with:
      version: 8
      run_install: false

- name: Get pnpm store directory
  shell: bash
  run: |
      echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

- uses: actions/cache@v3
  name: Setup pnpm & next caches
  with:
      path: |
          ${{ env.STORE_PATH }}
          ${{ github.workspace }}/dist/

      # Generate a new cache whenever packages or source files change or prisma model changes.
      key: ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**/*.[jt]s', '**/*.[jt]sx', '**/*.prisma') }}
      # If source files changed but packages didn't, rebuild from a prior cache.
      restore-keys: |
          ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-


But I keep running into warning messages in builds about no caches and stuff.
EDIT:
Here's the warning message:
warn No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
Does anyone have any experience in that?
Thanks!

23 Replies

Avatar
risky
can you share the exact warning message?
and your full action file if possible
and btw, cache can't be overwritten, so you should add github.workspace to your cache key
that error is because your not caching the .next/cache folder (made by nextjs)
Avatar
Giant pandaOP
Thanks for your answers.
The dist/ directory consists of nextjs apps that each contain the .next directory. Should I explicitly list them?
Avatar
risky
so what is github.workspace in this context
and you should be not caching things that are in the code themself - waistful
Avatar
Giant pandaOP
it's the root of the nx monorepo project
Avatar
risky
and then where is your building of nextjs
Avatar
Giant pandaOP
I don't know if you're familiar with nx, but basically nx builds and outputs all the apps builds into the dist/ directory
Avatar
risky
as it would make the most sense to use matrix to build them all and create individual cache for each
Avatar
Giant pandaOP
and more specifically under dist/apps/app-a/...
Avatar
risky
yeah i don't fully know it
Avatar
Giant pandaOP
No worries 🙂
Avatar
risky
so, it copies your code to dist?
Avatar
Giant pandaOP
it outputs the built code to dist under a specific folder, relevant to the app yeah
Avatar
risky
as most systems would do apps/app-a/dist
and thats whats messing with me
Avatar
Giant pandaOP
well, nx is special lol
what would the matrix look like?
Avatar
risky
like it would be manually running each of the apps individually (but at the same time), so you prob would loose nx features
so, i use turbo repo and it can cache folders inside your app project and save it to a global cache that can be used, does nx have something like it?
Avatar
Giant pandaOP
unfortunately not, i'll do some research and come back with an answer hopefully