fix(front-end): redirect
All checks were successful
Build and Deploy / test-backend (push) Successful in 40s
Build and Deploy / test-frontend (push) Successful in 1m5s
Build and Deploy / build-and-push (push) Successful in 1m19s
Build and Deploy / deploy (push) Successful in 22s

This commit is contained in:
2026-03-22 22:41:12 +01:00
parent cc343ee27c
commit b317196217
6 changed files with 117 additions and 13 deletions

View File

@@ -5,6 +5,10 @@ import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import bootstrap from './main.server';
import { resolveRequestOrigin } from './core/request-origin';
import {
parseAcceptLanguage,
resolveInitialLanguage,
} from './app/core/i18n/language-resolution';
import { resolvePublicRedirectTarget } from './server-routing';
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
@@ -37,6 +41,25 @@ app.get(
}),
);
app.get('/', (req, res) => {
const userAgent = req.get('user-agent');
const preferredLanguages = parseAcceptLanguage(req.get('accept-language'));
const lang = resolveInitialLanguage({
preferredLanguages,
});
const stableRedirect = shouldUseStableRootRedirect(
userAgent,
preferredLanguages,
);
res.setHeader('Vary', 'Accept-Language, User-Agent');
res.setHeader('Cache-Control', 'private, no-store');
res.redirect(
stableRedirect ? 308 : 302,
`/${stableRedirect ? 'it' : lang}${querySuffix(req.originalUrl)}`,
);
});
app.get('**', (req, res, next) => {
const targetPath = resolvePublicRedirectTarget(req.path);
if (!targetPath) {
@@ -83,3 +106,21 @@ function querySuffix(url: string): string {
const queryIndex = String(url ?? '').indexOf('?');
return queryIndex >= 0 ? String(url).slice(queryIndex) : '';
}
function shouldUseStableRootRedirect(
userAgent: string | undefined,
preferredLanguages: readonly string[],
): boolean {
return preferredLanguages.length === 0 || isLikelyCrawler(userAgent);
}
function isLikelyCrawler(userAgent: string | undefined): boolean {
const normalized = String(userAgent ?? '').toLowerCase();
if (!normalized) {
return false;
}
return /(bot|crawler|spider|slurp|bingpreview|google-read-aloud)/.test(
normalized,
);
}