~3 دقیقه مطالعه • بروزرسانی ۴ آبان ۱۴۰۴
مقدمه
Next.js برای افزایش سرعت build و کاهش هزینهها، فایلهای کش را در مسیر .next/cache ذخیره میکند. در محیطهای CI، اگر این مسیر بین buildها حفظ نشود، با پیام No Cache Detected مواجه خواهید شد.
پیکربندی کش در سرویسهای CI مختلف
Vercel
در Vercel کشینگ بهصورت خودکار پیکربندی شده است و نیازی به اقدام خاصی نیست.
CircleCI
در فایل .circleci/config.yml مرحله save_cache را بهصورت زیر تنظیم کنید:
steps:
- save_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- ./.next/cacheTravis CI
در فایل .travis.yml بخش زیر را اضافه یا ادغام کنید:
cache:
directories:
- $HOME/.cache/yarn
- node_modules
- .next/cacheGitLab CI
در فایل .gitlab-ci.yml بخش زیر را اضافه کنید:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/Netlify CI
از پلاگین @netlify/plugin-nextjs استفاده کنید تا کشینگ بهصورت خودکار انجام شود.
AWS CodeBuild
در فایل buildspec.yml بخش زیر را اضافه کنید:
cache:
paths:
- 'node_modules/**/*'
- '.next/cache/**/*'GitHub Actions
با استفاده از 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
در فایل bitbucket-pipelines.yml بخش زیر را اضافه کنید:
definitions:
caches:
nextcache: .next/cache
و در مرحلهٔ pipeline:
- step:
name: your_step_name
caches:
- node
- nextcacheHeroku
در فایل package.json بخش زیر را اضافه کنید:
"cacheDirectories": [".next/cache"]Azure Pipelines
از task Cache@2 استفاده کنید:
- task: Cache@2
displayName: 'Cache .next/cache'
inputs:
key: next | $(Agent.OS) | yarn.lock
path: '$(System.DefaultWorkingDirectory)/.next/cache'Jenkins (Pipeline)
با استفاده از پلاگین 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ها نقش کلیدی در این فرآیند دارد.
نوشته و پژوهش شده توسط دکتر شاهین صیامی