diff --git a/.env.local b/.env.example similarity index 99% rename from .env.local rename to .env.example index 6952a616..eb29e18c 100644 --- a/.env.local +++ b/.env.example @@ -1,12 +1,11 @@ # 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables -NEXT_PUBLIC_VERSION=4.7.7 - # 可在此添加环境变量,去掉最左边的(# )注释即可 # Notion页面ID,必须 # NOTION_PAGE_ID=097e5f674880459d8e1b4407758dc4fb # 非必须 +# NEXT_PUBLIC_VERSION= # NEXT_PUBLIC_PSEUDO_STATIC= # NEXT_PUBLIC_REVALIDATE_SECOND= # NEXT_PUBLIC_THEME=matery diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 00000000..c9ec6df7 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,34 @@ +## 已知问题 + +1. (示例)版本号管理不规范 + - 版本号直接写在环境变量中,容易出错 + - 多处维护版本号,可能不一致 + +## 解决方案 + +1. (示例)将版本号管理从 `.env.local` 迁移到 `package.json` + - 统一从 `package.json` 读取版本号 + - 使用 IIFE 优雅处理版本号获取逻辑 + - 保持向后兼容,支持环境变量覆盖 + +## 改动收益 + +1. (示例)更规范的版本管理 + - 统一从 `package.json` 读取 + - 保持与 npm 生态一致 + - 减少人为错误 + +## 具体改动 + +1. (示例)`blog.config.js` + - 移除原有的静态版本号配置 + - 在文件末尾添加动态版本号获取逻辑 + - 保持向后兼容,优先使用环境变量 + - 添加错误处理和默认值 + +## 测试确认 + +- [x] 本地开发环境测试通过 +- [x] 生产环境构建测试通过 +- [x] 版本号正确显示 +- [x] 环境变量配置正常工作 diff --git a/.gitignore b/.gitignore index b400f485..a0811bd9 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ yarn-debug.log* yarn-error.log* # local env files -# .env.local # 版本号放在此环境变量中 +.env.local .env.development.local .env.test.local .env.production.local diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5fffb37a..9fde0549 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,6 +3,7 @@ - [Setup](#setup) - [Creating new themes](#creating-new-themes) - [Adding localizations](#adding-localizations) +- [Environment Variables](#environment-variables) Thanks for considering to contribute! @@ -42,6 +43,19 @@ localization! Follow these steps to add a new localization: 3. Add your language config to [lang.js][lang.js]. 4. [Create a PR][pr] with your localization updates. +## Environment Variables + +NotionNext uses environment variables for configuration. To set up your development environment: + +1. Copy `.env.example` to `.env.local` +2. Fill in the required values in `.env.local` +3. Never commit `.env.local` to version control + +The configuration priority is: +1. Notion Config Table (highest) +2. Environment Variables +3. blog.config.js (lowest) + [fork]: https://github.com/tangly1024/NotionNext/fork [pr]: https://github.com/tangly1024/NotionNext/compare [next.js]: https://github.com/vercel/next.js diff --git a/blog.config.js b/blog.config.js index 772cae9b..ed0721cc 100644 --- a/blog.config.js +++ b/blog.config.js @@ -548,7 +548,15 @@ const BLOG = { process.env.npm_lifecycle_event === 'export', // 在打包过程中默认开启缓存,开发或运行时开启此功能意义不大。 isProd: process.env.VERCEL_ENV === 'production' || process.env.EXPORT, // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) BUNDLE_ANALYZER: process.env.ANALYZE === 'true' || false, // 是否展示编译依赖内容与大小 - VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号 + VERSION: (() => { + try { + // 优先使用环境变量,否则从package.json中获取版本号 + return process.env.NEXT_PUBLIC_VERSION || require('./package.json').version + } catch (error) { + console.warn('Failed to load package.json version:', error) + return '1.0.0' // 缺省版本号 + } + })() } module.exports = BLOG diff --git a/package.json b/package.json index dff2f46a..6a87045e 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "post-build": "next-sitemap --config next-sitemap.config.js", "export": "cross-env EXPORT=true next build && next-sitemap --config next-sitemap.config.js", "bundle-report": "cross-env ANALYZE=true next build", - "build-all-in-dev": "cross-env VERCEL_ENV=production next build" + "build-all-in-dev": "cross-env VERCEL_ENV=production next build", + "version": "echo $npm_package_version" }, "dependencies": { "@clerk/localizations": "^3.0.4",