Script not being executed while navigating, it works only if I refresh the page

Viewed 111

I'm trying to insert a script on a gatsby project, but the script doesn't load, it loads only if I refresh the page, not while navigating.

The html.js file is same only that I inserted this line of code in the head section :

<script src={'../src/general/sendData'}/>

The script doesn't show any messages while navigating, only if I refresh the page:

window.onload = function(){
 console.log('worked');
 }
1 Answers

Try using onRouteUpdate API (from Gatsby browser, gatsby-browser.js):

exports.onRouteUpdate = () => {
 console.log('worked');
}

It's a helper triggered in each route change so it should fit your specs.

Related