From c901dbbdc5d914a548e6ceee99e4b1dbbfb0c841 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 12 Apr 2024 13:22:48 +0800 Subject: [PATCH] fix: request Signed-off-by: Innei --- .../(topic-detail)/topics/[slug]/layout.tsx | 2 +- src/app/(app)/(page-detail)/[slug]/layout.tsx | 2 +- src/app/(app)/api.tsx | 2 +- src/app/(app)/categories/[slug]/api.tsx | 2 +- src/app/(app)/notes/[id]/api.tsx | 8 +++++-- src/app/(app)/notes/page.tsx | 2 +- .../(post-detail)/[category]/[slug]/api.tsx | 2 +- src/app/(app)/timeline/layout.tsx | 2 +- src/lib/{attach-ua.ts => attach-fetch.ts} | 9 +++++++ src/lib/define-metadata.ts | 2 +- src/lib/query-client.server.ts | 2 ++ src/lib/request.server.ts | 2 +- src/lib/request.ts | 24 +++++++++++++++++++ 13 files changed, 50 insertions(+), 11 deletions(-) rename src/lib/{attach-ua.ts => attach-fetch.ts} (85%) diff --git a/src/app/(app)/(note-topic)/notes/(topic-detail)/topics/[slug]/layout.tsx b/src/app/(app)/(note-topic)/notes/(topic-detail)/topics/[slug]/layout.tsx index 32c146c2b6..a50dffe8b1 100644 --- a/src/app/(app)/(note-topic)/notes/(topic-detail)/topics/[slug]/layout.tsx +++ b/src/app/(app)/(note-topic)/notes/(topic-detail)/topics/[slug]/layout.tsx @@ -3,7 +3,7 @@ import type { Metadata } from 'next' import { QueryHydrate } from '~/components/common/QueryHydrate' import { NormalContainer } from '~/components/layout/container/Normal' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { isShallowEqualArray } from '~/lib/lodash' import { getQueryClient } from '~/lib/query-client.server' import { definePrerenderPage } from '~/lib/request.server' diff --git a/src/app/(app)/(page-detail)/[slug]/layout.tsx b/src/app/(app)/(page-detail)/[slug]/layout.tsx index 5f4cff941c..e6ad33ab82 100644 --- a/src/app/(app)/(page-detail)/[slug]/layout.tsx +++ b/src/app/(app)/(page-detail)/[slug]/layout.tsx @@ -14,7 +14,7 @@ import { BottomToUpTransitionView, } from '~/components/ui/transition' import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { getOgUrl } from '~/lib/helper.server' import { getSummaryFromMd } from '~/lib/markdown' import { apiClient } from '~/lib/request' diff --git a/src/app/(app)/api.tsx b/src/app/(app)/api.tsx index 9760566c3d..d45c2f352a 100644 --- a/src/app/(app)/api.tsx +++ b/src/app/(app)/api.tsx @@ -5,7 +5,7 @@ import type { AggregateRoot } from '@mx-space/api-client' import { simpleCamelcaseKeys } from '@mx-space/api-client' import { appStaticConfig } from '~/app.static.config' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { getQueryClient } from '~/lib/query-client.server' import { apiClient } from '~/lib/request' diff --git a/src/app/(app)/categories/[slug]/api.tsx b/src/app/(app)/categories/[slug]/api.tsx index 0ceb056da5..32046fc8cc 100644 --- a/src/app/(app)/categories/[slug]/api.tsx +++ b/src/app/(app)/categories/[slug]/api.tsx @@ -1,6 +1,6 @@ import { cache } from 'react' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { apiClient } from '~/lib/request' import { requestErrorHandler } from '~/lib/request.server' diff --git a/src/app/(app)/notes/[id]/api.tsx b/src/app/(app)/notes/[id]/api.tsx index a4e18807e6..0b2f19a22f 100644 --- a/src/app/(app)/notes/[id]/api.tsx +++ b/src/app/(app)/notes/[id]/api.tsx @@ -2,7 +2,7 @@ import { cache } from 'react' import { headers } from 'next/dist/client/components/headers' import { REQUEST_QUERY } from '~/constants/system' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch, getAuthFromCookie } from '~/lib/attach-fetch' import { getQueryClient } from '~/lib/query-client.server' import { requestErrorHandler } from '~/lib/request.server' import { queries } from '~/queries/definition' @@ -19,8 +19,12 @@ export const getData = cache(async (params: { id: string }) => { searchParams.get('password') || undefined, token ? `${token}` : undefined, ) + const data = await getQueryClient() - .fetchQuery(query) + .fetchQuery({ + ...query, + staleTime: getAuthFromCookie() ? 0 : undefined, + }) .catch(requestErrorHandler) return data }) diff --git a/src/app/(app)/notes/page.tsx b/src/app/(app)/notes/page.tsx index 52005409a5..991f00ca76 100644 --- a/src/app/(app)/notes/page.tsx +++ b/src/app/(app)/notes/page.tsx @@ -2,7 +2,7 @@ import { cookies } from 'next/headers' import { redirect } from 'next/navigation' import type { NoteWrappedWithLikedPayload } from '@mx-space/api-client' -import { getAuthFromCookie } from '~/lib/attach-ua' +import { getAuthFromCookie } from '~/lib/attach-fetch' import { AuthKeyNames } from '~/lib/cookie' import { apiClient } from '~/lib/request' import { definePrerenderPage } from '~/lib/request.server' diff --git a/src/app/(app)/posts/(post-detail)/[category]/[slug]/api.tsx b/src/app/(app)/posts/(post-detail)/[category]/[slug]/api.tsx index cdf0727d9a..b0ae40b5b6 100644 --- a/src/app/(app)/posts/(post-detail)/[category]/[slug]/api.tsx +++ b/src/app/(app)/posts/(post-detail)/[category]/[slug]/api.tsx @@ -1,6 +1,6 @@ import { cache } from 'react' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { getQueryClient } from '~/lib/query-client.server' import { requestErrorHandler } from '~/lib/request.server' import { queries } from '~/queries/definition' diff --git a/src/app/(app)/timeline/layout.tsx b/src/app/(app)/timeline/layout.tsx index 1c1eacc446..b2dbe85d7a 100644 --- a/src/app/(app)/timeline/layout.tsx +++ b/src/app/(app)/timeline/layout.tsx @@ -7,7 +7,7 @@ import { TimelineType } from '@mx-space/api-client' import { QueryHydrate } from '~/components/common/QueryHydrate' import { SearchFAB } from '~/components/modules/shared/SearchFAB' import { REQUEST_QUERY } from '~/constants/system' -import { attachServerFetch } from '~/lib/attach-ua' +import { attachServerFetch } from '~/lib/attach-fetch' import { getQueryClient } from '~/lib/query-client.server' import { apiClient } from '~/lib/request' diff --git a/src/lib/attach-ua.ts b/src/lib/attach-fetch.ts similarity index 85% rename from src/lib/attach-ua.ts rename to src/lib/attach-fetch.ts index dcccaea710..4bc2e86537 100644 --- a/src/lib/attach-ua.ts +++ b/src/lib/attach-fetch.ts @@ -24,6 +24,15 @@ export const attachServerFetch = () => { 'User-Agent', `${ua} NextJS/v${PKG.dependencies.next} ${PKG.name}/${PKG.version}`, ) + + // TODO 安全考虑还是去掉了 + // const jwt = getAuthFromCookie() + // clearGlobalSearchParams() + + // if (jwt) + // setGlobalSearchParams({ + // token: jwt, + // }) } export const attachServerFetchAuth = () => { diff --git a/src/lib/define-metadata.ts b/src/lib/define-metadata.ts index 48f81ad7ac..6ca6e492c6 100644 --- a/src/lib/define-metadata.ts +++ b/src/lib/define-metadata.ts @@ -6,7 +6,7 @@ import type { Metadata } from 'next' import { getQueryClient } from '~/lib/query-client.server' import { queries } from '~/queries/definition' -import { attachServerFetch } from './attach-ua' +import { attachServerFetch } from './attach-fetch' export const defineMetadata = >( fn: ( diff --git a/src/lib/query-client.server.ts b/src/lib/query-client.server.ts index 454921ff67..61efcef36e 100644 --- a/src/lib/query-client.server.ts +++ b/src/lib/query-client.server.ts @@ -1,3 +1,5 @@ +import 'server-only' + import { QueryClient } from '@tanstack/react-query' const sharedClient = new QueryClient({ diff --git a/src/lib/request.server.ts b/src/lib/request.server.ts index 5fc3b02c76..37ccbdda47 100644 --- a/src/lib/request.server.ts +++ b/src/lib/request.server.ts @@ -9,7 +9,7 @@ import { RequestError } from '@mx-space/api-client' import { BizErrorPage } from '~/components/common/BizErrorPage' import { NormalContainer } from '~/components/layout/container/Normal' -import { attachServerFetch } from './attach-ua' +import { attachServerFetch } from './attach-fetch' import { getErrorMessageFromRequestError } from './request.shared' export const requestErrorHandler = (error: Error | RequestError) => { diff --git a/src/lib/request.ts b/src/lib/request.ts index 525dc279da..fe0b89431e 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -15,6 +15,7 @@ const uuidStorageKey = 'x-uuid' const uuid = nanoid() const globalConfigureHeader = {} as any +const globalConfigureSearchParams = {} as any if (isServerSide) { globalConfigureHeader['User-Agent'] = @@ -43,11 +44,23 @@ const $fetch = createFetch({ } } + context.options.params ??= {} + Object.assign(context.options.params, globalConfigureSearchParams) + if (context.options.params.token) { + context.options.cache = 'no-store' + } if (isDev && isServerSide) { // eslint-disable-next-line no-console console.log(`[Request]: ${context.request}`) } }, + onResponse(context) { + // log response + if (isDev && isServerSide) { + // eslint-disable-next-line no-console + console.log(`[Response]: ${context.request}`, context.response.status) + } + }, }, }) @@ -117,3 +130,14 @@ export const attachFetchHeader = (key: string, value: string | null) => { } } } + +export const setGlobalSearchParams = (params: Record) => { + clearGlobalSearchParams() + Object.assign(globalConfigureSearchParams, params) +} + +export const clearGlobalSearchParams = () => { + Object.keys(globalConfigureSearchParams).forEach((key) => { + delete globalConfigureSearchParams[key] + }) +}