Gatsby: Allow pathPrefix to be specified with "gatsby build" command

Created on 5 Sep 2018  路  2Comments  路  Source: gatsbyjs/gatsby

Summary

pathPrefix (documented here) is a helpful feature.

In our case:

  • A production deployment is published to /
  • A staging deployment is published to /staging (because of reasons)

I don't want to add pathPrefix to gatsby-config.js because production doesn't require it. But every time I want to stage, I have to temporarily modify gatsby-config.js to set pathPrefix: "/staging". This upsets source control.

It would be nice if I could just pass the parameter directly (see example, below). That way I don't need to modify gatsby-config.js.

Basic example

Allow pathPrefix to be passed directly, if not present in gatsby-config.js:

gatsby build --pathPrefix /staging --prefix-paths

Motivation

Putting pathPrefix in gatsby-config.js is great if it is a permanent setting. But if you only need it sometimes, then it is painful.

This enhancement request is to support the case where you only _sometimes_ want to specify pathPrefix. It will let you easily specify it at runtime as a command line argument.

question or discussion

Most helpful comment

Hi @robinzimmermann, you can use environment variables to pass th e pathPrefix value.

Your staging environment build script:
PATH_PREFIX='/staging' gatsby build --prefix-paths

Your production environment build script:
gatsby build

In gatsby-config.js:

module.exports = {
    pathPrefix: process.env.PATH_PREFIX || ""
    // rest of the configuration
}

This way you can specify the pathPrefix at runtime as a command line argument and won't need to modify gatsby-config.js to set it.

All 2 comments

Hi @robinzimmermann, you can use environment variables to pass th e pathPrefix value.

Your staging environment build script:
PATH_PREFIX='/staging' gatsby build --prefix-paths

Your production environment build script:
gatsby build

In gatsby-config.js:

module.exports = {
    pathPrefix: process.env.PATH_PREFIX || ""
    // rest of the configuration
}

This way you can specify the pathPrefix at runtime as a command line argument and won't need to modify gatsby-config.js to set it.

Sadly, i got this error

ValidationError: child "pathPrefix" fails because ["pathPrefix" is not allowed to be empty]

So, I had to change my config to this

module.exports = {
    pathPrefix: process.env.PATH_PREFIX || "/"
    // rest of the configuration
}

;D

Was this page helpful?
0 / 5 - 0 ratings