ssa
ssa

Reputation: 130

I want to "Gatsby Build" and deploy to a subdirectory

I read https://www.gatsbyjs.org/docs/path-prefix/ and I added the following to gatsby-configs.js.

{
  pathPrefix: "/modules/custom/gatsby/gatsby-shopify-starter/public",
},

And, I execute sudo gatsby build --prefix-paths on terminal. But, I got this error message.

ERROR

The "path" argument must be of type string. Received an instance of Object

Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type s  tring. Received an instance of Object

What am I doing wrong?

Upvotes: 1

Views: 3227

Answers (1)

apena
apena

Reputation: 2321

Gatsby's pathPrefix only prepends a string to your projects routes (url). Your deployment script(s) should handle where the public folder ends up on your server. If you really want to publish your gatsby project to somewhere other than /public (locally) You will propbably need to do something like use fs and path in onPostBuild in gatsby-node.js to move everything over to another directory. At the time or writing this, Gatsby doesn't directly support alternative build directories so you are on your own when using gatsby develop or gatsby serve to view this locally.

Try something like this in gatsby-node.js but change the paths to serve your needs. This example will publish your project to /public/blog rather than the default /public and is just a proof of concept but I tested it and it works:

const path = require('path');
const fs = require('fs');

exports.onPostBuild = function() {
  fs.renameSync(path.join(__dirname, 'public'), path.join(__dirname, 'public-blog'));

  fs.mkdirSync(path.join(__dirname, 'public'));

  fs.renameSync(path.join(__dirname, 'public-blog'), path.join(__dirname, 'public', 'blog'));
};

Upvotes: 3

Related Questions