1 | initial version |
In Next.js, you can obtain the previous URL using the useRouter
hook from the next/router
module. You can then access the previous URL using the asPath
property of the router object. Here's an example:
import { useRouter } from 'next/router';
function MyComponent() {
const router = useRouter();
const { asPath } = router;
// use `asPath` as needed
return <div>...</div>;
}
The asPath
property contains the previous URL as a string, including any query parameters or hash fragments. Note that this value is only available when using the client-side router, so it may be undefined on the initial request.