Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should not redirect when page is visited by bot #1330

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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