Skip to content

Commit

Permalink
fix: should not redirect when page is visited by bot (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Aug 15, 2024
1 parent d16b4b6 commit 964f4dd
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/theme-default/src/logic/useRedirect4FirstVisit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ 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;
// 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(window.navigator.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);
Expand Down

0 comments on commit 964f4dd

Please sign in to comment.