Last active
July 28, 2025 10:42
-
-
Save miiiladiii244/3b10db541bf1020598e6c9e653d75d5d to your computer and use it in GitHub Desktop.
Logged Next Router to follow pushes to router in complex apps - suitable for debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useRouter as useNextRouter, NextRouter } from "next/router"; | |
| type PushSignature = (href: any, as?: any, options?: any, tag?: string) => Promise<boolean>; | |
| export function useLoggedRouter(): Omit<NextRouter, "push"> & { push: PushSignature } { | |
| const router = useNextRouter(); | |
| // our new push | |
| const push: PushSignature = async (href, as, options, tag) => { | |
| console.log(`%c[router.push]${tag ? ` [${tag}]` : ""}`, "color: teal; font-weight: bold;", { href, as, options }); | |
| return router.push(href, as, options); | |
| }; | |
| // spread everything except `push`, and override it | |
| const { push: _orig, ...rest } = router; | |
| return { ...rest, push }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment