Next.js Discord

Discord Forum

Need help with route middleware

Unanswered
Pip gall wasp posted this in #help-forum
Open in Discord
Pip gall waspOP
How do you use route middleware?
I need to execute an async function before going to another route to check something.
How do I do that?
The function must execute every time the route is changed.

33 Replies

@"use php" https://nextjs.org/docs/app/building-your-application/routing/middleware
Pip gall waspOP
This will only work for SSR
And will not work for client side navigation
It will work for every request
@Arinji It will work for every request
Pip gall waspOP
It will only work for every request made to server
@Pip gall wasp It will only work for every request made to server
When you change route, middleware is executed
^^
Its basically sending a server side request to render that route
Have you tried it out first?
Pip gall waspOP
@"use php" Doesn't work
Kindly share code, ss
Pip gall waspOP
middleware.js
export function middleware(request) {
    console.log('From middleware')
}


home page
import Link from 'next/link'

export default function Home() {
    return (
        <>
            <p>This is the home page</p>
            <Link href={'/about'}>Go about</Link>
        </>
    )
}


about page
import Link from 'next/link'

export default function Home() {
    return (
        <>
            <p>This is the about page</p>
            <Link href={'/'}>Go home</Link>
        </>
    )
}
It should log From moddleware in the console. And it logs it for requests made to the server
But after loading the home page in the browser, if you navigate to the about page, not request is make to the server.
So, that middleware function isn't called
@"use php" I think its a xyproblem https://xyproblem.info
Pip gall waspOP
nope
Suppose I have something like that
Where I want to call a function before changing route
If the function is client side, Its worth seeing this: https://medium.com/@codingbeautytutorials/nextjs-detect-route-change-bb8e6c316d1
Pip gall waspOP
Okay. So, in the actual application I want to check some tokens before going to another route. If the token is valid, I want to proceed, otherwise I want to redirect to another route.
@"use php" How would you do something like that?
@"use php" Wait you are not exporting a matched
Pip gall waspOP
It's just an example
I’ll try it in my personal playground in some time and let you know
Pip gall waspOP
If the request is made to the server, then server can check the token and proceed or redirect to another route.
But if no request is made to the server (client-side routing), then that middleware won't execute. So, the token isn't checked.
@Pip gall wasp So I've confirmed that unless the route is cached on the client side, It'll send a request to server side
So if someone goes from / to /about, middleware will execute, but not vice versa because / is already cached
Pip gall waspOP
Yeah. I've also noticed that. But is there any way to achieve that?
@"use php" So if someone goes from / to /about, middleware will execute, but not vice versa because / is already cached
Pip gall waspOP
Is there any way to execute a function every time before route change?
You can check everytime in page.tsx.

I wouldn't recommend actually, because token will most likely be active. And incase it gets inactive, user can just refresh the page