Defer posthog script loading to relieve js main thread and improve site vitals

Created on 27 February 2025, 2 months ago

Problem/Motivation

Currently, we synchronously load the posthog scripts. This takes its tall on the main thread, leading to about ~340 ms delay:

Steps to reproduce

Proposed resolution

Instead, we should add the "defer" attribute to asynchonously load the script (https://pagespeedchecklist.com/async-and-defer).

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Active

Version

1.0

Component

Code (posthog_js)

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Grevil
  • 🇩🇪Germany Grevil

    Unclear what exactly causes the bad web vitals, this should be investigated in the future. I tried to set the scripts async, but it didn't help. Interestingly enough, "array.js", which is initiated by the cdn script, is already set as async, meaning it shouldn't block anything at all!

  • 🇩🇪Germany Anybody Porta Westfalica

    I'm unsure if we can even fix this and if this is an issue on the posthog side or a misinterpretation on the Google side.
    Thanks for the report and let's keep an eye on this, but I think we should treat it as minor for now.

    Our implementation fits the best practices from posthog, I think.

Production build 0.71.5 2024