Files
Paul D'AmbraandGitHub 55f20248dc feat: allow adding lazy loaded scripts to head to avoid hydration errors (#2802)
avoid hydration errors https://github.com/PostHog/posthog-js/issues/2781

when someone is using SSR and we're lazy loading scripts we have a race between react hydrating and us altering the DOM

if we manage to alter the DOM before hydration complests then we'll cause a hydration error  
  
i don't believe we care where the script is added (since we're adding it async from code the location isn't going to affect whether it blocks) so we can add the script to the head and not have any risk of causing a hydration error

### Libraries affected

<!-- Please mark which libraries will require a version bump. -->

- [ ] All of them
- [x] posthog-js (web)
- [ ] posthog-js-lite (web lite)
- [ ] posthog-node
- [ ] posthog-react-native
- [ ] @posthog/react
- [ ] @posthog/ai
- [ ] @posthog/nextjs-config
- [ ] @posthog/nuxt
- [ ] @posthog/rollup-plugin
- [ ] @posthog/webpack-plugin
2026-01-22 07:51:27 +00:00
..