Access dynamic path in middlware
Answered
Forest bachac posted this in #help-forum
Forest bachacOP
I can't find in the docs a way in middleware to access the dynamic path. If I do
request.nextURL it just is / instead of /en-US or whatever the locale is. How do I access it?Answered by Forest bachac
I figured it out:
if you have locales setup in next.config.js, it disables them from being apart of the url. idrk why but it does that
if you have locales setup in next.config.js, it disables them from being apart of the url. idrk why but it does that
16 Replies
Forest bachacOP
I console.logged Request and nothing in it has the "en-US" dynamic path
European sprat
Try req.nextUrl.pathname
Forest bachacOP
nope its just "/"
@European sprat Try req.nextUrl.pathname
Forest bachacOP
nextURL.searchParams doesnt work either
European sprat
And the locale is set how?
Like
app/[lang]/page.js ?Does req.nextUrl.locale give you anything?
@European sprat Does req.nextUrl.locale give you anything?
Forest bachacOP
yes, it shows the locale (default locale or the locale page i go to). the only issue is that it shows the default locale "en-US" if no locale is specified, which means that if i use it to redirect that will be an issue.
European sprat
I don't know anything about locale, if it's handled in some special way or what so I can't really help
But req.nextUrl is where you'll find the various things that come with a request, there's no other parameter or import or anything to get other values
European sprat
I think you can set defaultLocale to "default" in next config
@European sprat But req.nextUrl is where you'll find the various things that come with a request, there's no other parameter or import or anything to get other values
Forest bachacOP
i dont think i could access any sort of dynamic url in req.nextURL not just locale
ill try that tho
ill try that tho
European sprat
Just making sure you know it's nextUrl not nextURL
Forest bachacOP
ik
Forest bachacOP
I figured it out:
if you have locales setup in next.config.js, it disables them from being apart of the url. idrk why but it does that
if you have locales setup in next.config.js, it disables them from being apart of the url. idrk why but it does that
Answer
Forest bachacOP
so i disabled that and i think its working fine