Cannot get external JS file to work with Gatsby site #27611
-
DescriptionI have a script file with some minimal javascript I need to implement. I've tried using react-helmet and it works, but I need the JS to reload on page routes, which it doesn't. I've tried using gatsby-browser.js and gatsby-ssr.js and have had no luck. Also, I tried putting JS in html.js and also not working. Steps to reproduceHere is how I would implement the script with gatsby-browser.js:
Here is how I would try to use gatsby-ssr.js:
I copied raw JS out of script file and put it in between `` in html.js in the recommended way and my JS doesn't seem to work as well. Maybe there is something wrong with my code (works fine with helmet), so here is my JS below for what its worth:
Expected resultFor JS to work on all pages through out site. Actual resultWhen using: react-helmet - my JS works but only on refresh or first render, need it to reload after page routes. gatsby-browser.js - I can see my app.js file in the body, but no JS works throughout the site. gatsby-ssr.js - I usually get an "unexpected token error '>" app.js line 1. html.js - my JS is unable to format saying theres errors but I'm unable to figure out why. EnvironmentSystem: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can use onRouteUpdate to trigger some javascript when a page is changed. You should be able to call whatever you need from there. |
Beta Was this translation helpful? Give feedback.
You can use onRouteUpdate to trigger some javascript when a page is changed. You should be able to call whatever you need from there.