You could create a custom Link
component, as a wrapper to next/link
, that would check for the query string and add it to the href
of the Next.js Link
.
// components/link.jsx
import NextLink from 'next/link';
import { useRouter } from 'next/router';
const Link = ({ children, href }) => {
const router = useRouter();
const [, queryString] = router.asPath.split('?');
const hrefWithQuery = `${href}${queryString ? `?${queryString}` : ''}`;
return <NextLink href={hrefWithQuery}>{children}</NextLink>;
};
export default Link;
Then replace all imports of next/link
with the path to the new component wherever they are used (a simple search & replace would do).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…