Vercel
For anyone looking trying to solve this on Vercel platform, the answer is rewrites: https://vercel.com/docs/configuration#project/rewrites
If you have a gatsby app with client-only paths, as per something like https://www.gatsbyjs.com/plugins/gatsby-plugin-create-client-paths/, it's likely you will encounter this issue on deployment.
Example: your application has a path like mything.com/app/user-profile.
In this case, app/* are client-only routes that don't get generated on the server, so you may get a flash of a 404 when you reload this route or land on it directly from an external link, for example.
To solve, add a rewrite on the /app/* server side route in Vercel to point back to /app where, it is assumed, your routing is controlled by Reach router or similar in your pages/app.js file.
To set this up on Vercel, create a vercel.json in the root of your gatsby/react project and add the following configuration:
{
"rewrites": [{ "source": "/app/:match*", "destination": "/app" }]
}
This issue will generally only manifest itself when deployed to production/staging. It won't occur in development (via gatsby develop anyway).