EDIT:
I forgot to mention that my site contains around 25k pages built from this template. The answer from Derek Nguyen will work for smaller sites with a small amount of pages, but when this is scaled up the matchPath
data is stored in the JS, resulting in massive bundle sizes. (Around 3.1mb for me!)
The Challenge
I need to create a page using createPage
which contains a sub-routing system in which the default route is built to static HTML, but any other routes are client-only.
The Problem
I have a template component used for static HTML generation in the createPage
method, but also a component in the pages/
directory used to assign a matchPath
to for the client-only routes.
On the latest version of Gatsby, the template component renders for the default route, but then the "page" component renders for the client-only ones, leading to full page re-renders when I only want the sub-route to render, as there is data in the parent route which needs to persist.
It seems wrong in my head to have two components with two routers in, but I can't think of any other way to achieve what I am trying to achieve.
Here is an example of my routing set up:
Template used for createPage
<Router>
<ContainerComponent
path={urlFromGraphQL}
>
<DefaultRoute
default
/>
</ContainerComponent>
</Router>
Client only component in /pages
<Router>
<ContainerComponent
path="/some-route/:slug/:id"
>
<DefaultRoute
default
/>
<SecondTabRoute
path="second-tab-route"
/>
<ThirdTabRoute
path="third-tab-route"
/>
</ContainerComponent>
</Router>
I have also replicated the behaviour in this codesandbox. Make sure to go to https://hkhbb.sse.codesandbox.io/test/foo/bar to see the correct output.
I have created an issue in github about this but have yet to have a reply.