Gatsby not working paths deployed on the github pages (adding repo name in the url)

1.1k views Asked by At

I deployed a gatsby website with the Github pages and I'm having errors like that: enter image description here Locally everything works perfectly, errors occur only on the server. Seems like the server can not resolve paths correctly. I'm adding unnecessary repository name after domain. How to remove that? I tried changing some host options and deploying app again and once it worked properly, IDK why, then I made another deploy and it crushed again. My gatsby.config:

const path = require("path");
const { title, keywords, description, author, defaultLang, trackingId } = require("./config/site");

module.exports = {
  pathPrefix: "/lbearthworks",
  siteMetadata: {
    title,
    keywords,
    description,
    author,
  },
  plugins: [
    {
      resolve: "gatsby-plugin-google-analytics",
      options: {
        trackingId,
      },
    },
    {
      resolve: "gatsby-plugin-manifest",
      options: {
        name: title,
        short_name: "Lb",
        start_url: "/",
        background_color: "#212121",
        theme_color: "#fed136",
        display: "minimal-ui",
        icon: "content/assets/gatsby-icon.png",
      },
    },
    "gatsby-transformer-remark",
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "markdown",
        path: `${__dirname}/content`,
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "images",
        path: `${__dirname}/content/assets/images`,
      },
    },
    "gatsby-plugin-eslint",
    "gatsby-plugin-react-helmet",
    "gatsby-transformer-sharp",
    "gatsby-plugin-sharp",
    "gatsby-plugin-offline",
    {
      resolve: "gatsby-plugin-sass",
      options: {
        data: `@import "core.scss";`,
        includePaths: [path.resolve(__dirname, "src/style")],
      },
    },
    ...
  ],
};

Live version

Github Reository

1

There are 1 answers

0
Ferran Buireu On

When dealing with GitHub Pages you need to add an extra configuration to your build command, since you are adding a pathPrefix variable, you need to allow Netlify how to prefix those paths. Ideally, the build command should look like:

"deploy": "gatsby build --prefix-paths && gh-pages -d public"

In your case, because you are adding a file-based configuration (netlify.toml), your build command is:

[build]
  command = "yarn && yarn testbuild"
  publish = "public"

Note that testbuild is yarn test && yarn build, according to your repository.

So, one workaround is changing your package.json command to:

"testbuild": "yarn test && yarn build --prefix-paths && gh-pages -d public",

In addition, you should be in gh-pages branch as it shows the Gatsby's documentation:

When you run npm run deploy all contents of the public folder will be moved to your repository’s gh-pages branch. Make sure that your repository’s settings has the gh-pages branch set as the source to deploy from.