@@ -5,25 +5,36 @@ import SuperJSON from "superjson";
55
66import type { AppRouter } from "@homarr/api" ;
77import { createHeadersCallbackForSource , getTrpcUrl } from "@homarr/api/shared" ;
8+ import { localeCookieKey } from "@homarr/definitions" ;
9+ import type { SupportedLanguage } from "@homarr/translation" ;
10+ import { supportedLanguages } from "@homarr/translation" ;
811import { createI18nMiddleware } from "@homarr/translation/middleware" ;
912
10- export async function middleware ( request : NextRequest ) {
11- // fetch api does not work because window is not defined and we need to construct the url from the headers
12- // In next 15 we will be able to use node apis and such the db directly
13- const culture = await serverFetchApi . serverSettings . getCulture . query ( ) ;
13+ let isOnboardingFinished = false ;
1414
15+ export async function middleware ( request : NextRequest ) {
1516 // Redirect to onboarding if it's not finished yet
1617 const pathname = request . nextUrl . pathname ;
17- if ( ! pathname . endsWith ( "/init" ) ) {
18+
19+ if ( ! isOnboardingFinished && ! pathname . endsWith ( "/init" ) ) {
1820 const currentOnboardingStep = await serverFetchApi . onboard . currentStep . query ( ) ;
1921 if ( currentOnboardingStep . current !== "finish" ) {
2022 return NextResponse . redirect ( new URL ( "/init" , request . url ) ) ;
2123 }
24+
25+ isOnboardingFinished = true ;
26+ }
27+
28+ // Only run this if the user has not already configured their language
29+ const currentLocale = request . cookies . get ( localeCookieKey ) ?. value ;
30+ let defaultLocale : SupportedLanguage = "en" ;
31+ if ( ! currentLocale || ! supportedLanguages . includes ( currentLocale as SupportedLanguage ) ) {
32+ defaultLocale = await serverFetchApi . serverSettings . getCulture . query ( ) . then ( ( culture ) => culture . defaultLocale ) ;
2233 }
2334
2435 // We don't want to fallback to accept-language header so we clear it
2536 request . headers . set ( "accept-language" , "" ) ;
26- const next = createI18nMiddleware ( culture . defaultLocale ) ;
37+ const next = createI18nMiddleware ( defaultLocale ) ;
2738 return next ( request ) ;
2839}
2940
0 commit comments