#Hydration errors with Google Analytics
1 messages · Page 1 of 1 (latest)
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 🤕
that would bread, gaTrackingId is a string, needs to be quoted
and I don't think the problem is there
ah right,
https://www.googletagmanager.com/gtag/js?id=UA-1111111-1 loads the analytics.js script
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.
Are you on React 17 by any chance ?
yep
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
It's been a year plus and React18 is still buggy.
I think for this particular problem, the remix team is thinking of not hydrating the root element anymore
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),
}}
/>