My goal is to enable the following types of static and dynamic paths with the best pattern possible when using Gatsby (v2) and @reach/router. The problem I've run into is that the Gatsby docs set you up to prefix the path /app
on every dynamic route, which does not fit my use case.
Desired paths:
/
: static route and build-time generated/about
: static route and build-time generated/:username
: dynamic route and run-time generated/:username/:postTitleSlug
: dynamic route and run-time generated
Following the Gatsby docs, I have this folder structure:
/pages
index.jsx
about.jsx
app.jsx
/templates
profile.jsx // for the /:username path
post.jsx // for the /:username/:postTitleSlug path
app.jsx
const Routes = () => (
<Router>
<Profile path="/:username" />
<Read path="/:username/:slug" />
</Router>
);
gatsby-node.js
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions;
if (page.path.match(/^\/app/)) {
await createPage({
path: 'app',
matchPath: '/app/*',
component: path.resolve('src/templates/Profile/index.js'),
});
}
};
Is it possible to unwind the "app" nomenclature and key some dynamic routes off of the base path, /
?
FWIW, my first attempted fix was to make pages/index.jsx
just a router, similar to this post. It seems like a potential solution, but I'm not clear on the required updates to gatsby-node.js
.