I deployed my first Gatsby project to github pages:
repo: https://github.com/michal-kurz/stfuandclick
gh-pages: https://michal-kurz.github.io/stfuandclick/app/ (generated from gh-pages
branch)
It has one page, src/pages/app.tsx
, which uses use Reach Router for dynamic routing.
// app.tsx
const App = () => (
<>
<GlobalStyles />
<Provider store={store}>
<ThemeProvider theme={theme}>
<Router>
<Homepage path={`${BASE_URL}/app/`} />
<Team path={`${BASE_URL}/app/team/:teamName/`} />
</Router>
</ThemeProvider>
</Provider>
</>
)
// gatsby-node.js
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
if (page.path.match(/^\/app/)) {
page.matchPath = '/app/*'
createPage(page)
}
}
note: BASE_URL
equals to /stfuandclick
in production env version and ''
in other envs
Everything works fine in development mode (gatsby develop
), but on the deployed version, it is impossible to visit /team/:teamName
without using /app/
as the app's entry point (going to /app/
and click the blue button works fine).
Opening the /team/:teamName link directly results in a 404 (unless previously cached by visiting it through /app/
, but then ctrl + f5 results in 404 again).
At seems as if gatsby.node.js
isn't doing it's job properly once app is deployed. I initialy suspected that it does't work at all, but apparently that's not the case, as commenting all the code out breaks the app even further (the team pages suddenly break even upon visited via link from /app
).
I tried prefixing the paths in gatsby-node.js
with BASE_URL
in the production build like so:
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
if (page.path.match(/^\/stfuandclick\/app/)) {
page.matchPath = '/stfuandclick/app/*'
createPage(page)
}
}
and also prefixing each of the two paths in gatsby-node.js
individually, but with no luck.
I do have pathPrefix
in my gatsby-config.json
: (full config here)
module.exports = {
pathPrefix: '/stfuandclick',
// ...
}
and I deploy with the following yarn script: (full package.json here)
"deploy": "gatsby build --prefix-paths && gh-pages -d public"
What can I do to make my routing work?