create-young-proj 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,24 @@
1
1
  # Changelog
2
2
 
3
3
 
4
+ ## v1.2.1
5
+
6
+ [compare changes](https://github.com/BluesYoung-web/create-young-proj/compare/v1.2.0...v1.2.1)
7
+
8
+ ### 🚀 Enhancements
9
+
10
+ - 🚀 日志 & 自适应跳转优化 ([1325277](https://github.com/BluesYoung-web/create-young-proj/commit/1325277))
11
+ - 🚀 打印请求者 ua, ip ([59fc41d](https://github.com/BluesYoung-web/create-young-proj/commit/59fc41d))
12
+ - 🚀 缓存配置 ([3573531](https://github.com/BluesYoung-web/create-young-proj/commit/3573531))
13
+
14
+ ### 🔥 Performance
15
+
16
+ - 🔥 缓存机制优化 ([cbe8bb5](https://github.com/BluesYoung-web/create-young-proj/commit/cbe8bb5))
17
+
18
+ ### ❤️ Contributors
19
+
20
+ - 张扬 <bluesyoung_web@163.com>
21
+
4
22
  ## v1.2.0
5
23
 
6
24
  [compare changes](https://github.com/BluesYoung-web/create-young-proj/compare/v1.1.0...v1.2.0)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "create-young-proj",
3
3
  "type": "module",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "description": "create project from template",
6
6
  "author": "BluesYoung-web",
7
7
  "license": "MIT",
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2022-12-30 17:19:42
4
- * @LastEditTime: 2023-11-07 14:38:26
4
+ * @LastEditTime: 2023-11-10 16:09:24
5
5
  * @Description:
6
6
  */
7
7
  import { resolve } from 'node:path'
@@ -48,13 +48,19 @@ export default defineNitroPlugin(async (nitroApp) => {
48
48
  delete config[key]
49
49
  }
50
50
 
51
+ // 仅打包之后格式化日志
52
+ if (process.env.NODE_ENV !== 'development')
53
+ useYoungLogger()
54
+
51
55
  console.log('------------------------读取配置文件------------------------')
52
56
  console.log(config)
53
57
  console.log('-------------------------------------------------------------')
54
58
 
55
- // 仅打包之后格式化日志
56
- if (process.env.NODE_ENV !== 'development')
57
- useYoungLogger()
59
+ nitroApp.hooks.hook('request', (event) => {
60
+ const headers = event.node.req.headers
61
+ console.log('ua', headers['user-agent'], 'x-forwarded-for', headers['x-forwarded-for'], 'x-real-ip', headers['x-real-ip'])
62
+ })
63
+
58
64
 
59
65
  nitroApp.hooks.hook('render:html', (html, { event }) => {
60
66
  // 直接注入环境变量到前端
@@ -27,6 +27,14 @@ navigateTo({
27
27
 
28
28
  **`asstes` 内的图片不得大于 4k**,超过大小的图片需要转换为 `webp` 后上传至 `cdn`
29
29
 
30
+ ### 接口缓存基准时间
31
+
32
+ 开发时可以修改 `nuxt.config.ts -> runtimeConfig -> public -> cacheTime` 的值
33
+
34
+ 部署时调整 `boot.mjs` 中 `NUXT_PUBLIC_CACHE_TIME` 的默认值
35
+
36
+ **系统环境变量 `NUXT_PUBLIC_CACHE_TIME` 优先级最高!!!**
37
+
30
38
  ## 特性
31
39
 
32
40
  - 基于 [@nuxtjs/device](https://github.com/nuxt-modules/device) 的设备判断
@@ -1,12 +1,21 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2022-09-07 09:31:12
4
- * @LastEditTime: 2023-05-26 11:21:19
4
+ * @LastEditTime: 2023-11-10 09:41:43
5
5
  * @Description:
6
6
  */
7
7
  (async () => {
8
8
  const env = process.env.DEPLOY_ENV
9
9
  const listenPort = process.env.LISTEN_PORT || 3003
10
+
11
+ /**
12
+ * !!! 接口缓存时间基准配置,默认 60s
13
+ */
14
+ const NUXT_PUBLIC_CACHE_TIME = 60
15
+
16
+ if (!(+process.env.NUXT_PUBLIC_CACHE_TIME > 0))
17
+ process.env.NUXT_PUBLIC_CACHE_TIME = NUXT_PUBLIC_CACHE_TIME
18
+
10
19
  console.log('当前环境:', env)
11
20
  console.log('服务监听端口:', listenPort)
12
21
 
@@ -1,11 +1,17 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-07-26 11:48:46
4
- * @LastEditTime: 2023-09-27 12:06:20
4
+ * @LastEditTime: 2023-11-10 10:27:21
5
5
  * @Description:
6
6
  */
7
7
  /**
8
- * 接口缓存时间,单位为 s
9
- * todo: 正式部署前按需修改
8
+ * 接口缓存基准时间,单位为 s
10
9
  */
11
- export const NUXT_PUBLIC_CACHE_TIME = 1
10
+ export const NUXT_PUBLIC_CACHE_TIME = +useRuntimeConfig().public.cacheTime
11
+
12
+ if (!NUXT_PUBLIC_CACHE_TIME) {
13
+ console.error('配置异常:缓存基准时长 NUXT_PUBLIC_CACHE_TIME', NUXT_PUBLIC_CACHE_TIME)
14
+ process.exit(1)
15
+ }
16
+
17
+ console.log('缓存基准时长 NUXT_PUBLIC_CACHE_TIME:', NUXT_PUBLIC_CACHE_TIME, '秒')
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-09-21 15:57:55
4
- * @LastEditTime: 2023-11-08 10:56:04
4
+ * @LastEditTime: 2023-11-10 14:55:01
5
5
  * @Description:
6
6
  */
7
7
 
@@ -88,6 +88,14 @@ export default defineNuxtConfig({
88
88
  defaultImage: '/svg/image_placeholder.svg',
89
89
  },
90
90
 
91
+ 'routeRules': {
92
+ /**
93
+ * 页面缓存,配置具体数值会被作为 max-age 的值,不过貌似 true 就足够了
94
+ * 根据具体情况自行调整各个路径的对应配置
95
+ */
96
+ '*': { swr: true },
97
+ },
98
+
91
99
  'vite': {
92
100
  build: {
93
101
  // 兼容钉钉浏览器
@@ -95,4 +103,12 @@ export default defineNuxtConfig({
95
103
  sourcemap: false,
96
104
  },
97
105
  },
106
+ 'runtimeConfig': {
107
+ public: {
108
+ /**
109
+ * 接口缓存基准时间,会被 NUXT_PUBLIC_CACHE_TIME 环境变量覆盖
110
+ */
111
+ cacheTime: 10,
112
+ },
113
+ },
98
114
  })
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-10-07 16:02:03
4
- * @LastEditTime: 2023-11-08 10:32:38
4
+ * @LastEditTime: 2023-11-10 15:02:38
5
5
  * @Description:
6
6
  */
7
7
  import { NUXT_PUBLIC_CACHE_TIME } from '~~/composables/config'
@@ -47,6 +47,6 @@ export default cachedEventHandler(async () => {
47
47
  return false
48
48
  }
49
49
  }, {
50
- maxAge: NUXT_PUBLIC_CACHE_TIME,
50
+ maxAge: NUXT_PUBLIC_CACHE_TIME * 10,
51
51
  swr: true,
52
52
  })
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-06-07 12:06:48
4
- * @LastEditTime: 2023-11-08 10:30:59
4
+ * @LastEditTime: 2023-11-10 15:02:45
5
5
  * @Description:
6
6
  */
7
7
  import { NUXT_PUBLIC_CACHE_TIME } from '~~/composables/config'
@@ -33,6 +33,6 @@ export default cachedEventHandler(async (event) => {
33
33
  }
34
34
  }
35
35
  }, {
36
- maxAge: NUXT_PUBLIC_CACHE_TIME * 3,
36
+ maxAge: NUXT_PUBLIC_CACHE_TIME * 5,
37
37
  swr: true,
38
38
  })
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-06-07 14:43:38
4
- * @LastEditTime: 2023-11-08 09:53:14
4
+ * @LastEditTime: 2023-11-10 15:02:52
5
5
  * @Description:
6
6
  */
7
7
  import { NUXT_PUBLIC_CACHE_TIME } from '~~/composables/config'
@@ -33,6 +33,6 @@ export default cachedEventHandler(async () => {
33
33
  return false
34
34
  }
35
35
  }, {
36
- maxAge: NUXT_PUBLIC_CACHE_TIME,
36
+ maxAge: NUXT_PUBLIC_CACHE_TIME * 5,
37
37
  swr: true,
38
38
  })
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-10-07 17:43:38
4
- * @LastEditTime: 2023-10-09 12:08:35
4
+ * @LastEditTime: 2023-11-10 15:03:01
5
5
  * @Description:
6
6
  */
7
7
  import { NUXT_PUBLIC_CACHE_TIME } from '~~/composables/config'
@@ -23,6 +23,6 @@ export default cachedEventHandler(async (event) => {
23
23
  return false
24
24
  }
25
25
  }, {
26
- maxAge: NUXT_PUBLIC_CACHE_TIME,
26
+ maxAge: NUXT_PUBLIC_CACHE_TIME * 3,
27
27
  swr: true,
28
28
  })
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2023-06-07 14:29:59
4
- * @LastEditTime: 2023-11-08 09:51:06
4
+ * @LastEditTime: 2023-11-10 15:03:09
5
5
  * @Description:
6
6
  */
7
7
  import { NUXT_PUBLIC_CACHE_TIME } from '~~/composables/config'
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author: zhangyang
3
3
  * @Date: 2022-12-30 17:19:42
4
- * @LastEditTime: 2023-11-08 10:52:46
4
+ * @LastEditTime: 2023-11-10 14:59:37
5
5
  * @Description:
6
6
  */
7
7
  import { resolve } from 'node:path'
@@ -9,6 +9,9 @@ import { loadConfig } from 'c12'
9
9
  import { useYoungLogger } from '@bluesyoung/logger'
10
10
 
11
11
  export default defineNitroPlugin(async (nitroApp) => {
12
+ const IS_PROD = process.env.NODE_ENV !== 'development'
13
+ console.log('🚀 ~ file: init.ts:13 ~ IS_PROD:', IS_PROD)
14
+
12
15
  const env = (process.env.DEPLOY_ENV as 'dev' | 'test' | 'online') || 'dev'
13
16
  const { config } = await loadConfig<Record<string, any>>({
14
17
  name: env,
@@ -17,7 +20,7 @@ export default defineNitroPlugin(async (nitroApp) => {
17
20
  // 优先读取环境变量中的版本信息(自己打的 Tag)
18
21
  NUXT_PUBLIC_CURRENT_VERSION: process.env.PROJECT_VERSION || 'v0.0.1',
19
22
  // 日志等级
20
- CONSOLA_LEVEL: 1,
23
+ CONSOLA_LEVEL: 3,
21
24
  // 此处可以放置通用的环境变量
22
25
  VITE_WECHAT_APPID: 'todo: 微信公众号appid',
23
26
  VITE_OSS_URL: `https://master-web-cdn.laiyouxi.com/platform_index/${env}/`,
@@ -45,25 +48,19 @@ export default defineNitroPlugin(async (nitroApp) => {
45
48
  delete config[key]
46
49
  }
47
50
 
48
- console.log('------------------------读取配置文件------------------------')
49
- console.log(config)
50
- console.log('-------------------------------------------------------------')
51
-
52
- const IS_PROD = process.env.NODE_ENV !== 'development'
53
- console.log('🚀 ~ file: init.ts:53 ~ IS_PROD:', IS_PROD)
54
-
55
51
  if (IS_PROD) {
56
52
  // 格式化日志
57
- const { consola } = useYoungLogger()
58
- const logger = consola.create({
59
- level: +process.env.CONSOLA_LEVEL!,
60
- })
61
- logger.wrapConsole()
53
+ useYoungLogger()
62
54
  }
63
55
 
64
- nitroApp.hooks.hook('render:html', async (html, { event }) => {
56
+ console.log('------------------------读取配置文件------------------------')
57
+ console.log(config)
58
+ console.log('-------------------------------------------------------------')
59
+
60
+ nitroApp.hooks.hook('request', (event) => {
65
61
  const ua = event.node.req.headers['user-agent']
66
62
 
63
+ console.log('ua', ua, 'x-forwarded-for', event.node.req.headers['x-forwarded-for'], 'x-real-ip', event.node.req.headers['x-real-ip'])
67
64
  console.log('jump status: ', !config!.DISABLE_AUTO_LOCATION, typeof !config!.DISABLE_AUTO_LOCATION)
68
65
 
69
66
  if (ua && (IS_PROD && !config!.DISABLE_AUTO_LOCATION)) {
@@ -76,18 +73,19 @@ export default defineNitroPlugin(async (nitroApp) => {
76
73
  console.log('🚀 ~ file: init.ts:64 ~ nitroApp.hooks.hook ~ 手机访问 PC 网址:')
77
74
  event.node.res.setHeader('Location', process.env.VITE_WEBSITE_MOBILE! + event.node.req.url)
78
75
  event.node.res.statusCode = 301
79
- return
76
+ event.node.res.end()
80
77
  }
81
78
  else if (!isMobile && fromMobile) {
82
79
  // PC 访问手机网址
83
80
  console.log('🚀 ~ file: init.ts:72 ~ nitroApp.hooks.hook ~ PC 访问手机网址:')
84
81
  event.node.res.setHeader('Location', process.env.VITE_WEBSITE_PC! + event.node.req.url)
85
82
  event.node.res.statusCode = 301
86
- return
83
+ event.node.res.end()
87
84
  }
88
85
  }
86
+ })
89
87
 
90
- // 网站置灰
88
+ nitroApp.hooks.hook('render:html', async (html, { event }) => {
91
89
  try {
92
90
  const {
93
91
  is_gray,