From a3e29c1be1462cce55caaccb5a1965bcf54c4022 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Sun, 4 Feb 2024 18:33:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96middleware?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/src/middleware/fetch.global.ts | 23 +++++++++++++++++------ site/src/server/middleware/index.ts | 3 +++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 site/src/server/middleware/index.ts diff --git a/site/src/middleware/fetch.global.ts b/site/src/middleware/fetch.global.ts index bfe4c5a87..983f3dd7b 100644 --- a/site/src/middleware/fetch.global.ts +++ b/site/src/middleware/fetch.global.ts @@ -1,8 +1,19 @@ export default defineNuxtRouteMiddleware(async () => { - const configStore = useConfigStore() - const userStore = useUserStore() - await Promise.all([ - configStore.fetchConfig(), - userStore.fetchCurrent(), - ]) + + const load = async () => { + const configStore = useConfigStore() + const userStore = useUserStore() + await Promise.all([ + configStore.fetchConfig(), + userStore.fetchCurrent(), + ]) + } + + const nuxtApp = useNuxtApp() + + // 服务端渲染,或者客服端渲染 + if (process.server || (process.client && !nuxtApp.isHydrating)) { + await load() + } + }) diff --git a/site/src/server/middleware/index.ts b/site/src/server/middleware/index.ts new file mode 100644 index 000000000..644292900 --- /dev/null +++ b/site/src/server/middleware/index.ts @@ -0,0 +1,3 @@ +export default defineEventHandler(async (event) => { + console.log('New request: ' + getRequestURL(event)) +})