mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-06 15:59:47 -05:00
Remove forced server side rendering
This commit is contained in:
@@ -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
6
web/middleware.ts
Normal 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();
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user