in NextJS 13 with app folder, I need to get data based on url (language from e.g. /en/asdf/
or /de/asdf/
) and pass it to components.
I tried using middleware and it seemed to work relatively well using cookies, but there is one big problem - the cookie is only available in the components after the next reload, not immediately.
Is there any way to pass data from middleware to components?
Or something else, like some Global variables?
Shortcode of my middleware:
import { NextResponse } from 'next/server'
export const middleware = (req) => {
const pathname = req.nextUrl.pathname
const language = pathname.split('/')[1]
const allowedLanguages = ['en', 'de'];
if(allowedLanguages.includes(language)) {
const response = NextResponse.rewrite(new URL(pathname.replace(`/${language}`, ''), req.url))
// This works only after browser reload
response.cookies.set('language', language)
return response
}
}
export const config = {
matcher: ['/((?!assets|api|_next/static|favicon.ico).*)'],
}
Thanks