Next.js برای افزایش سرعت build و کاهش هزینهها، فایلهای کش را در مسیر .next/cache ذخیره میکند. در محیطهای CI، اگر این مسیر بین buildها حفظ نشود، با پیام No Cache Detected مواجه خواهید شد.
در Vercel کشینگ بهصورت خودکار پیکربندی شده است و نیازی به اقدام خاصی نیست.
در فایل .circleci/config.yml مرحله save_cache را بهصورت زیر تنظیم کنید:
steps:
- save_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- ./.next/cacheدر فایل .travis.yml بخش زیر را اضافه یا ادغام کنید:
cache:
directories:
- $HOME/.cache/yarn
- node_modules
- .next/cacheدر فایل .gitlab-ci.yml بخش زیر را اضافه کنید:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/از پلاگین @netlify/plugin-nextjs استفاده کنید تا کشینگ بهصورت خودکار انجام شود.
در فایل buildspec.yml بخش زیر را اضافه کنید:
cache:
paths:
- 'node_modules/**/*'
- '.next/cache/**/*'با استفاده از actions/cache مرحله زیر را در workflow اضافه کنید:
uses: actions/cache@v4
with:
path: |
~/.npm
${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.ts', '**/*.tsx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-در فایل bitbucket-pipelines.yml بخش زیر را اضافه کنید:
definitions:
caches:
nextcache: .next/cache
و در مرحلهٔ pipeline:
- step:
name: your_step_name
caches:
- node
- nextcacheدر فایل package.json بخش زیر را اضافه کنید:
"cacheDirectories": [".next/cache"]از task Cache@2 استفاده کنید:
- task: Cache@2
displayName: 'Cache .next/cache'
inputs:
key: next | $(Agent.OS) | yarn.lock
path: '$(System.DefaultWorkingDirectory)/.next/cache'با استفاده از پلاگین Job Cacher مراحل زیر را در Jenkinsfile اضافه کنید:
stage("Restore npm packages") {
steps {
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 {
writeFile file: "next-lock.cache", text: "$GIT_COMMIT"
cache(caches: [
arbitraryFileCache(
path: ".next/cache",
includes: "**/*",
cacheValidityDecidingFile: "next-lock.cache"
)
]) {
sh "npm run build"
}
}
}با پیکربندی صحیح کشینگ در محیطهای CI، میتوانید زمان build را کاهش داده و عملکرد پروژههای Next.js را بهینه کنید. حفظ مسیر .next/cache بین buildها نقش کلیدی در این فرآیند دارد.