Skip to content

Instantly share code, notes, and snippets.

@miiiladiii244
Last active July 28, 2025 10:42
Show Gist options
  • Select an option

  • Save miiiladiii244/3b10db541bf1020598e6c9e653d75d5d to your computer and use it in GitHub Desktop.

Select an option

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
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