The basic idea:
-
Create a custom App (
/pages/_app.tsx
) -
Return
null
iftypeof window === "undefined"
. This is required to prevent react-router from throwing errors during the SSR step!
// pages/_app.tsx
import { AppProps } from 'next/app';
function App({ Component, pageProps }: AppProps) {
return (
<div suppressHydrationWarning> // <- ADD THIS
{typeof window === 'undefined' ? null : <Component {...pageProps} />}
</div>
);
}
export default App;
-
Use optional catch all to catch all routes in the
/spa
path. You have to create apages/spa/[[...slug]].tsx
file inside the directory of your next app. -
Use the
StaticRouter
component to not bypass the history of the next router. -
You have to manually feed the
Switch
component fromreact-router
with a custom location object.
// pages/spa/[[...slug]].tsx
const router = useRouter();
<Switch
location={{
pathname: router.asPath,
search: "",
state: "",
hash: "",
}}
>
...
</Switch>;
- Replace all the
Link
andNavLink
components fromreact-router
byNextLink
. All theRedirect
components by a call to therouter.push
method of thenext-router
.
⚠️ As you can't use theLink
component fromreact-router
anymore nested routes will no longer work as expected. You need to provide the full path of the route to yourNextLink
component.