From 540d2a4fdb8f81fa624a47bc1189423eaa5f7717 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Wed, 14 Aug 2024 21:24:12 +0800 Subject: [PATCH 1/3] fix: should not redirect when visited by bot --- .../theme-default/src/logic/useRedirect4FirstVisit.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts index b870f3b29..84d3f3f84 100644 --- a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts +++ b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts @@ -10,17 +10,28 @@ export function useRedirect4FirstVisit() { const localeLanguages = Object.values(siteData.themeConfig.locales || {}); const langs = localeLanguages.map(item => item.lang) || []; const currentLang = page.lang; + useEffect(() => { const localeRedirect = siteData.themeConfig.localeRedirect ?? 'auto'; if (localeRedirect !== 'auto') { return; } + if (!defaultLang || process.env.TEST === '1') { // Check the window.navigator.language to determine the default language // If the default language is not the same as the current language, redirect to the default language // The default language will not have a lang prefix in the URL return; } + + const botRegex = /bot|spider|crawl|lighthouse/i; + const userAgent = window.navigator.userAgent.toLowerCase(); + // If the request is coming from a bot or crawler, do not redirect. + // this ensures that Google's search crawler can work as expected. + if (botRegex.test(userAgent)) { + return; + } + // Normalize current url, to ensure that the home url is always with a trailing slash const { pathname, search } = window.location; const cleanPathname = removeBase(pathname); From 8495545adc19a98f360a53aaca27238e8956e0f1 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Wed, 14 Aug 2024 21:52:00 +0800 Subject: [PATCH 2/3] fix --- packages/theme-default/src/logic/useRedirect4FirstVisit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts index 84d3f3f84..e5d259698 100644 --- a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts +++ b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts @@ -25,7 +25,7 @@ export function useRedirect4FirstVisit() { } const botRegex = /bot|spider|crawl|lighthouse/i; - const userAgent = window.navigator.userAgent.toLowerCase(); + const userAgent = window.navigator.userAgent; // If the request is coming from a bot or crawler, do not redirect. // this ensures that Google's search crawler can work as expected. if (botRegex.test(userAgent)) { From 7d7de40bcaff6fdc27d70cbe3dd99d40eb1ee499 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Wed, 14 Aug 2024 21:52:56 +0800 Subject: [PATCH 3/3] fix --- packages/theme-default/src/logic/useRedirect4FirstVisit.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts index e5d259698..9ab3caeec 100644 --- a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts +++ b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts @@ -25,10 +25,9 @@ export function useRedirect4FirstVisit() { } const botRegex = /bot|spider|crawl|lighthouse/i; - const userAgent = window.navigator.userAgent; // If the request is coming from a bot or crawler, do not redirect. // this ensures that Google's search crawler can work as expected. - if (botRegex.test(userAgent)) { + if (botRegex.test(window.navigator.userAgent)) { return; }