Why is LogRocket recording a session every time I run a new Gatsby JS build

945 Views Asked by At

I'm trying to install LogRocket in my Gatsby JS app, that I am hosting on Netlify.

I only want to record sessions in production, and for the most part everything works great but I have noticed that when I push a new build to Netlify, LogRocket will always register a new session during the build process.

Is there a way to prevent LogRocket from registering a new session during the Gatsby / Netlify build process?

I'm currently initializing the session in the onClientEntry build hook in gatsby-browser.js like this:

exports.onClientEntry = () => {
  if (typeof window !== "undefined" && process.env.NODE_ENV === "production") {
    LogRocket.init('<project_id>');
  }
}
1

There are 1 best solutions below

1
Ferran Buireu On BEST ANSWER

Try onInitialClientRender API:

exports.onInitialClientRender = () => {
  if (typeof window !== "undefined" && process.env.NODE_ENV === "production") {
    LogRocket.init('<project_id>');
  }
}

From the docs:

onInitialClientRender { Function }

Called when the initial (but not subsequent) render of Gatsby App is done on the client.