问题
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:
module.exports = {
pathPrefix: `/blog`,
siteMetadata: {...},
plugins: [...]
}
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:
[build]
Command = "npm run build"
Functions = "lambda"
Publish = "public"
The build command frum netlify.toml runs the command from package.json which is this:
"build": "run-p build:**",
"build:app": "gatsby build --prefix-paths",
"build:lambda": "netlify-lambda build src/lambda",
What am I missing here? Do I need to make some other configuration to netlify or something?
回答1:
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 started that was doing this) I think it's ok.
Now that the files are served from "/blog" everything works fine.
来源:https://stackoverflow.com/questions/56425441/deploy-gatsby-site-to-netlify-with-prefix-paths