question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pathPrefix not working after building and serving Gatsby site

See original GitHub issue

Description

Problem when using pathPrefix option in gatsby-config when serving the build.

The goal is to have a gatsby app, that builds, and the build is then served from an (express) app. You would have the main express app serving a static page, called page-2 (the gatsby page) at www.myapp.com/banana/page-2.

Due to banana there is a pathPrefix of “/banana” added to the config. The build then uses gatsby build --prefix-paths to utilize this.

Steps to reproduce

Demo Repo: https://github.com/jfarris587/gatsby-demo

A):

  • Clone the repo
  • run yarn build and then yarn serve
  • Go to localhost:9000
  • Attempt to navigate
  • Attempt to go to localhost:9000/page-2
  • Try and click the text to trigger alert

B):

  • Change build script to normal yarn gatsby build
  • Remove pathPrefix from gatsby-config file
  • Repeat steps above

Expected result

I should be able to go to the home page, navigate to the other page and click the text to see the javascript working.

Actual result

Trying (A): You will notice that going to the home page works, but clicking the link takes you to unknown page. However, if you go to /page-2 directory with no “banana” prefix, then it takes you to the page… But the javascript doesn’t work. When clicking the h3 an onClick fires to alert you…

Trying (B): You will get the expected behavior, but it doesn’t utilize a prefix, which is needed for serving page-2 from an express app

Notice that yarn Dev in Chrome does work. (IE seems to have seperate polyfill issue in development mode). However, serving the build gives above problems

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
yugandhar-pathicommented, Nov 5, 2019

If anyone coming here like me …after building your application with --prefix-paths serve your site also with --prefix-paths ! Modify your scripts in package.json as "serve": "gatsby serve --prefix-paths -o",

3reactions
ramrock93commented, Nov 6, 2019

If anyone coming here like me …after building your application with --prefix-paths serve your site also with --prefix-paths ! Modify your scripts in package.json as "serve": "gatsby serve --prefix-paths -o",

Thanks man! Solved my problem. It’s not specified in the docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding a Path Prefix - Gatsby
If this flag is not passed, Gatsby will ignore your pathPrefix and build the site as if hosted from the root domain. gatsby...
Read more >
`pathPrefix` ignored with `gatsby develop` - Stack Overflow
My GitLab deploy stage runs a bash script that uploads build results on my official OVH Web Hosting. Everything works splendid! GitLab pages ......
Read more >
Gatsby build : files not found / Failed to load resource
When you're in development, everything is great. But after running gatsby build, your js plugins and fonts are all broken or missing So...
Read more >
Why is —prefix-paths breaking my build during gh-pages ...
My local build runs fine when I Gatsby serve it, but I'm using gh-pages to deploy and narrowed it down to path prefix...
Read more >
Deploying to Github Pages? Don't Forget to Fix Your Links
Is your site not working after you've deployed to Github Pages? ... Gatsby will ignore the pathPrefix configuration and it will build your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found