Briefly summarize the issues you have been experiencing.
I’m trying to deploy a blog site built with Gatsby to Netlify. The thing is, I want to serve the site from /blog. Following the docs, I changed the gatsby-config.js to include pathPrefix like so:
Next, I changed my build command to include --prefix-paths:
gatsby build --prefix-paths
When I run the site locally using gatsby serve --prefix-paths everything works fine. However, after I deployed to netlify the site is still being served from root / and not from /blog. My netlify.toml:
For anyone encountering the same issue. I was able to solve this by copying all the files inside the public folder into the “blog” folder like so:
"build": "run-p build:**",
"build:app": "npm run clean && gatsby build --prefix-paths && npm run move",
"build:lambda": "netlify-lambda build src/lambda",
"move": "cd public && mkdir blog | mv * blog"
I added the move command to package.json and I execute it after the build. I’m not sure if that’s how I’m suppose to do it but after doing some research (I also found a starter that was doing this) I think it’s ok.
Now that the files are served from “/blog” everything works fine.
Thanks for your solution @ovidiu1207! And you’re right, --prefix-path doesn’t change where gatsby store built files in the build directory. This creates a difference between your blog links, which are prefixed with /blog and the path to your files.
I found a workaround with these two netlify.toml configuration files, the first on my main site and the second on my gatsby blog.
[[redirects]]
from = "/blog/*"
to = "https://my-gatsby-blog.netlify.app/:splat"
status = 200
[[redirects]]
from = "/blog/*"
to = "/:splat"
The last one tells your blog that each url prefixed with /blog should point to the files that are in your build directory.