-
Notifications
You must be signed in to change notification settings - Fork 483
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
Update Remix instructions to a working version #9830
base: master
Are you sure you want to change the base?
Conversation
@Hansenq is attempting to deploy a commit to the PostHog Team on Vercel. A member of the Team first needs to authorize it. |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, tested it out but came across some issues, let me know your thoughts
import { hydrateRoot } from "react-dom/client"; | ||
Next, create a new PostHog context in `app/contexts/posthog-context.tsx`. This is necessary because of a [missing export statement](https://github.com/PostHog/posthog-js/issues/908) in `posthog-js`'s `package.json`. | ||
|
||
```ts file=app/contexts/posthog-context.tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to cause a hydration error and de-opts the code into client side rendering for me. The follow code doesn't cause that problem. What do you think?
import posthog from "posthog-js";
import { useEffect, createContext, useContext, useRef } from "react";
type PosthogType = typeof posthog | undefined;
const PosthogContext = createContext<PosthogType>(undefined);
export function PosthogProvider({ children }: { children: React.ReactNode }) {
const posthogInstanceRef = useRef<PosthogType>(undefined);
useEffect(() => {
// Only initialize PostHog once during component mount
if (posthogInstanceRef.current) return;
const apiKey = window.ENV?.POSTHOG_API_KEY;
if (!apiKey) return;
posthogInstanceRef.current = posthog.init(apiKey, {
api_host: "https://us.i.posthog.com",
loaded: (posthog) => {
if (process.env.NODE_ENV === 'development') {
posthog.debug();
}
},
});
}, []);
return (
<PosthogContext.Provider value={posthogInstanceRef.current}>
{children}
</PosthogContext.Provider>
);
}
export const usePosthog = () => useContext(PosthogContext);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough; React's recommendations around variables that must be initialized only once is pretty bad.
One call out is that process.env.NODE_ENV
is not available in the client, so I'll replace it with window.ENV.IS_PROD
and add a note stating that this assumes the client ENV variables include IS_PROD
. Note that also if the user follows Remix's recommendations on how to pass env variables to the client, window.ENV
should always be defined, so we don't need the optional chaining.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actualy, if we change it to a useEffect
, we also need to change the ref to state. This is because setting the ref won't cause a re-render, so PosthogProvider
will still not have the posthog instance passed in as its value until the next render triggered by something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also can you check again and/or push the remix repo you're testing with? I checked again, and I'm not getting the hydration error with the original code (that uses a function and a ref). I'm happy to change it to an effect and state, but given that React's recommendation is to use a function and ref, I'd prefer only changing it to effect and state only if it's not working.
There's a bunch of different reasons why a hydration error is caused in Remix (including 3rd party browser extensions); I just want to make sure React's recommendation doesn't work before making the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works fine on the first load, but subsequent loads get the hydration error. Tested on guest Chrome without extensions.
Here's the test repo: https://github.com/ivanagas/remix-cool2
Here's a test site where you can see the error on refresh: https://vercel.com/ivanagas-projects/remix-cool2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing the repo! Sorry about the delay; I was traveling. Downloaded, built, and ran it both in dev and as production. Unfortunately I haven't been able to replicate the hydration error in either my regular Chrome or incognito; or on the first load, uncached load, or reload:

I'm not able to visit the vercel test site since I don't have access. Let me know if you're able to still reproduce the error, or if there are any other issues you notice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, here is the link to the Vercel site that is having the issue: remix-cool2.vercel.app
Co-authored-by: Ian Vanagas <[email protected]>
Is it really the plan of record to tell users to manually reimplement https://github.com/PostHog/posthog-js/blob/main/react/src/context/PostHogProvider.tsx, https://github.com/PostHog/posthog-js/blob/main/react/src/context/PostHogContext.ts and https://github.com/PostHog/posthog-js/blob/main/react/src/hooks/usePostHog.ts, rather than to fix PostHog/posthog-js#908? edit: coming back to this a month later, that sounded brusque and I apologise for that. I appreciate the work of the author here and I realise everyone is always busy and it's hard to get large projects in sync! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Figured out the one change that will make this work :)
|
||
Lastly, we need to add this context to your React tree. Go to `app/entry.client.tsx` and add the following: | ||
|
||
```ts file=app/entry.client.tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, checking again, the posthog-context
seems to work without hydration errors if you use it in the app.root.tsx
instead of entry.client
:
// app/root.tsx
import { PosthogProvider } from "./contexts/posthog-context";
export default function App() {
const { ENV } = useLoaderData<typeof loader>();
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<PosthogProvider>
<Outlet />
</PosthogProvider>
<ScrollRestoration />
<script
dangerouslySetInnerHTML={{
__html: `window.ENV = ${JSON.stringify(ENV)}`,
}}
/>
<Scripts />
</body>
</html>
);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, TBH I have no idea why you're experiencing these issues on Vercel and I'm not able to replicate the hydration errors running your repo locally (whether running npm run dev
or npm run build && npm run start
[prod]). Happy to take your lead here and update the PR to move it to root.tsx if you can confirm that it doesn't break on your vercel deployment!
(note that remix-cool.vercel.app right now is running the dev version of your app for some reason, and doesn't show the hydration errors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these seem to work now 😢, let me try some more options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't figure it out yet, still getting a variety of hydration errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you publish your Remix app that works so I could test it out?
Changes
Updates the Posthog Remix guide to a version that correctly sets up Posthog in Remix. These instructions were determined by me through setting up my own Remix app, referencing the existing guide, referencing the React guide, referencing this guide that's also incorrect, and referencing this issue that I ran into only when deploying to prod.
Checklist
vercel.json
Article checklist
Useful resources