#Shallow Routing with dynamic routes

13 messages · Page 1 of 1 (latest)

leaden ivy
#

Recently, due to some requirements of the project, we created some middleware to force all url paths within the app to lowercase, however, with dynamic paths, the links end up producing something along the lines of: /user/test?username=test, if we modify next/link to enable shallow routing, it strips the extra stuff from the url, but causes page flickers;

What would be the proper way to resolve it, rather than just doing <Link href={`/user/${username.toLowerCase()}`}>profile</Link>

middleware:

export function middleware(request: NextRequest) {
    const pathname = request.nextUrl.pathname
    // Check if the pathname is not lowercase
    if (pathname !== pathname.toLowerCase()) {
        // Create a new URL with the lowercase pathname
        const url = request.nextUrl.clone()
        url.pathname = pathname.toLowerCase()

        // Redirect to the lowercase URL
        return NextResponse.redirect(url)
    }
}
quiet fiberBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

barren marlin
#

in middleware you arent preserve search parameters (query params) when constructing the new URL. The issue occurs because request.nextUrl.clone() does not automatically retain the search parameters when modifying the pathname.
so something like this ??
const url = new URL(request.nextUrl.origin + pathname.toLowerCase() + request.nextUrl.search);

leaden ivy
barren marlin
leaden ivy
barren marlin
#

yeah, putting lowerCase() and producing extra search parameters seems not related at all, all i can say (pathname !== pathname.toLowerCase()) { this line is checking for not lower case and if you explicitly put lower case then it will bypass this condition.
I dont understand what you mean by next/link on a dynamic route, is it like when you add link inside of pages/user/[id]/page.tsx ?
I would recommend reproducing it on Sandbox and screen record of it .

leaden ivy
#

using a standard html link works fine though with the expected behavior of forcing everything lowercase, so not sure where the random query parameter comes from lol, if I remove that middleware stuff, links work fine again

barren marlin
leaden ivy
barren marlin
#

you see when i hover on the button Limbo_anj. and see the URL on left bottom, it has not added any search params, and user/[username]/page.tsx is a dynamic page right

leaden ivy