#Hydration errors with Google Analytics

1 messages · Page 1 of 1 (latest)

round tulip
#
react-dom.development.js:86 Warning: Prop `src` did not match. Server: "https://www.google-analytics.com/analytics.js" Client: "https://www.googletagmanager.com/gtag/js?id=UA-1111111-1"
    at script
    at head
    at html
    at App
    at RemixRoute (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:2655:3)
    at Routes2 (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:2639:7)
    at Router (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:727:15)
    at RemixCatchBoundary (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:1058:10)
    at RemixErrorBoundary (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:1099:9)
    at RemixEntry (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:2533:12)
    at RemixBrowser (https://www.automobile.lcl/build/_shared/chunk-ZTU4UPU3.js:3391:27)
#

The weird thing is that Server/Client seems to be inverted in the error message

#

Server replies with <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-1111111-1"></script>

#

and analytics gets called from the client as we can see in the request initiator chain

#

how do I get out of this one?

#

This is the last problem I need to tackle before swapping a first PHP/jQuery page with its Remix counterpart 🤕

eager dock
#

gtag('config', '${gaTrackingId}', {

#

try
gtag('config', gaTrackingId, {

round tulip
#

that would bread, gaTrackingId is a string, needs to be quoted

#

and I don't think the problem is there

eager dock
#

ah right,

round tulip
eager dock
#

I'm comparing it to our gtag implementation and can't spot anything obvious either.

#

Have you try just hardcoding gaTrackingId and see if the error goes away?

#

Might narrow down the issue.

round tulip
#

Are you on React 17 by any chance ?

eager dock
#

yep

round tulip
#

that's it then

#

I'm on React 18 ..

#

think I'm going to rollback to 17

#

have another app running it, and no problems with analytics

eager dock
#

It's been a year plus and React18 is still buggy.

round tulip
#

I think for this particular problem, the remix team is thinking of not hydrating the root element anymore

storm mist
#

Currently, I using trick below to prevent hydration error.
But I think this is a bug on React team.

type ClientOnlyScriptProps = ComponentProps<"script">;
function ClientOnlyScript(props: ClientOnlyScriptProps) {
  const html = props.dangerouslySetInnerHTML?.__html;

  if (!html) return null;
  return (
    <script
      ref={(ref) => {
        if (!ref) return;
        ref.innerHTML = html;
      }}
    />
  );
}

//...

        <ClientOnlyScript
          dangerouslySetInnerHTML={{
            __html: getGAScript(data.ENV.GA_ID),
          }}
        />