Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostHog Next.js causing memory leaks #1405

Open
mjad218 opened this issue Sep 9, 2024 · 3 comments
Open

PostHog Next.js causing memory leaks #1405

mjad218 opened this issue Sep 9, 2024 · 3 comments

Comments

@mjad218
Copy link

mjad218 commented Sep 9, 2024

Hi,
I have been using PostHog for a while and just discovered it is causing a serious memory leak.
I am using [email protected].

I solved the leak by lazily importing Posthog.

export const CSPostHogProvider = (props: IProps) => {
  const [client, setClient] = useState<PostHog | undefined>();

  useEffect(() => {
    const handler = async () => {
      const posthog = (await import("posthog-js")).posthog;

      const client = posthog.init(
        process.env["NEXT_PUBLIC_POSTHOG_KEY"] ?? "",
        {
          api_host: `${FRONT_END_URL}/ingest`,
          capture_pageview: false,
        },
      );
      setClient(client);
    };

    handler();
  }, []);

  if (!client) return props.children;
  return <PostHogProvider client={client}>{props.children}</PostHogProvider>;
};

The above code solved the leak on the server side, but the thing is I am worried the leak is happening on clients now.

Last 14 Days memory usage. cloud run instance
image

Last 1 Days memory usage. cloud run instance

image

You can observe from the above image that the memory usage is now constant at around 15% after I pushed the above code const posthog = (await import("posthog-js")).posthog;

How I was using PostHog before:

export const CSPostHogProvider = (props: IProps) => {
  const [client, setClient] = useState<PostHog | undefined>();

  useEffect(() => {
    const client = posthog.init(process.env["NEXT_PUBLIC_POSTHOG_KEY"] ?? "", {
      api_host: `${FRONT_END_URL}/ingest`,
      capture_pageview: false,
    });
    setClient(client);
  }, []);

  return <PostHogProvider client={client}>{props.children}</PostHogProvider>;
};

I tried this as well:

export const CSPostHogProvider = (props: IProps) => {
  const isClientSide = typeof window !== "undefined";
  if (isClientSide) {
    posthog.init(process.env["NEXT_PUBLIC_POSTHOG_KEY"] ?? "", {
      api_host: `${FRONT_END_URL}/ingest`,
      capture_pageview: false,
    });
  }

  return <PostHogProvider client={posthog}>{props.children}</PostHogProvider>;
};

I don't know if the leak is because of the way I configured PostHog or because of the library itself.

I am using [email protected]

Another concern is there are any plans about decreasing the size of the lib?
#65

@volfadar
Copy link

image
that's already states in their docs bro to do the dynamic import a.k.a lazy load the components.

@mjad218
Copy link
Author

mjad218 commented Sep 22, 2024

The photo or the screenshot is for loading the Page View component.

I clearly stated that the problem concerns the Provider component and initialization of the lib itself.

@mjad218
Copy link
Author

mjad218 commented Sep 22, 2024

And I don't think lazily loading the provider is good for performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants