Remove forced server side rendering

This commit is contained in:
MartinBraquet
2025-12-26 19:15:44 +02:00
parent c66d82a06d
commit 3232e783e3
3 changed files with 15 additions and 16 deletions

View File

@@ -3,14 +3,16 @@ import {defaultLocale, supportedLocales, Locale} from "common/constants"
let cachedLocale: string | null | undefined = null
export function getLocale(req?: IncomingMessage): string {
export function getLocale(
// req?: IncomingMessage
): string {
// if (cachedLocale) return cachedLocale
console.log('cachedLocale', cachedLocale)
let cookie = null
// Server
if (req?.headers?.cookie) {
cookie = req.headers.cookie
}
// if (req?.headers?.cookie) {
// cookie = req.headers.cookie
// }
// Client
if (typeof document !== 'undefined') {
@@ -24,6 +26,7 @@ export function getLocale(req?: IncomingMessage): string {
.find(c => c.startsWith('lang='))
?.split('=')[1]
?.split(' ')[0]
?.replace(";", "")
console.log('Locale cookie', cachedLocale)
}

6
web/middleware.ts Normal file
View File

@@ -0,0 +1,6 @@
import { NextResponse } from 'next/server';
export function middleware(req: any) {
const lang = req.cookies.get('lang') ?? 'en';
req.nextUrl.searchParams.set('lang', lang);
return NextResponse.next();
}

View File

@@ -87,13 +87,13 @@ function printBuildInfo() {
// specially treated props that may be present in the server/static props
type PageProps = { auth?: AuthUser }
function MyApp(props: AppProps<PageProps> & { locale: string }) {
function MyApp(props: AppProps<PageProps>) {
const {Component, pageProps} = props
useEffect(printBuildInfo, [])
useHasLoaded()
const router = useRouter()
const [locale, setLocaleState] = useState(props.locale);
const [locale, setLocaleState] = useState(getLocale());
const setLocale = (newLocale: string) => {
document.cookie = `lang=${newLocale}; path=/; max-age=31536000`;
setLocaleState(newLocale);
@@ -208,14 +208,4 @@ function MyApp(props: AppProps<PageProps> & { locale: string }) {
)
}
MyApp.getInitialProps = async (appContext: AppContext) => {
const appProps = await import('next/app').then(m => m.default.getInitialProps(appContext))
const locale = getLocale(appContext.ctx.req)
return {
...appProps,
locale
};
};
export default MyApp