Gatsby: Button onClick not working in Gatsby build

Created on 5 Jun 2020  路  11Comments  路  Source: gatsbyjs/gatsby

Hi;
I am planning to migrate my website from Wordpress to Gatsby. My plan is that I will use my existing Bluehost account to avoid any complications with transferring domains, especially that I will still use my Wordpress API for the back end. Accordingly, I will build my website locally using, then I will upload the "public" folder to my host folders.
Now the case is that I need to add a contact form, I am planning to use ajax call from the front end to a PHP file on my server to send the email.

The issue I am facing now is that when I add the onClick prop to the button, the event fires on the development phase. However when I run gatsby build and upload the HTML files on the server, no event is fired, I inspected the

awaiting author response question or discussion

All 11 comments

Can you put together a reproduction using the guidance here please: https://www.gatsbyjs.org/contributing/how-to-make-a-reproducible-test-case/

It's ok. Here is the repository:
https://github.com/mkhaledche/bug-repro
Here is also a link of the public folder on a host:
http://tagaruby.online/gatsby/public/

The event will be triggered on localhost (will give an error but it will be triggered which means the function is called), but on the link to the "public" folder, it will not be triggered.

at first glimpse i see there is in console a bug:

GET http://tagaruby.online/page-data/index/page-data.json net::ERR_ABORTED 404 (Not Found)

but the page data is found here:

http://tagaruby.online/gatsby/public/page-data/index/page-data.json

did you build it with pathPrefix ( https://www.gatsbyjs.org/docs/path-prefix/ )?

in your example repo i miss the form ...

Tested:

  • i build your site with gatsby build
  • then i started with gatsby serve
  • clicks are writing a log entry in the console
  • to simulate your subdirectory i moved all from public to a new directory: public/more
  • then i started with gatsby serve
  • i see the same error about page-data.json and no click event is triggered if i open the site at http://localhost:9000/more/
  • no log entry in the console when i click

following the steps in https://www.gatsbyjs.org/docs/path-prefix/

added:

module.exports = {
  pathPrefix: `/more`,
  siteMetadata: {
gatsby build --prefix-paths
gatsby serve --prefix-paths

opened browser at http://localhost:9000/more/
clicks making a log entry in console

in your case you need the following pathPrefix because your link on server is http://tagaruby.online/gatsby/public/

module.exports = {
  pathPrefix: `/gatsby/public`,
  siteMetadata: {

hint: here is a tutorial how to do form submissions back to the wordpress backend: https://www.youtube.com/watch?v=ZRQ94PMNEcg

Thanks @muescha for your reply. I'll check the path prefix and the solution in the video which seems to be interesting.
However, in my case, I don't think I need the form tag as all the functionality I need is a button that collects some input values and sends them to the server through ajax requests. I have already tested it on a non-gatsby static file with no form tag and it worked well.

It worked now. The issue was with pathPrefix. Thanks @muescha

Glad to hear that you solved the issue.

Thx for the feedback

Happy coding!

Was this page helpful?
0 / 5 - 0 ratings