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

Add example on how to expose the instance globally #413

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
848 changes: 416 additions & 432 deletions examples/next-csr-app/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/next-csr-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@croct/plug-react": "latest",
"next": "^12.3.3",
"next": "^14.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
19 changes: 19 additions & 0 deletions examples/next-ssr-app/components/GlobalCroct.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {render} from '@testing-library/react';
import croct, {Plug} from '@croct/plug';
import {GlobalCroct} from '@/components/GlobalCroct';

declare global {
interface Window {
croct?: Plug;
}
}

describe('<Providers />', () => {
it('should expose the Croct instance on the global scope', () => {
expect(window.croct).toBeUndefined();

render(<GlobalCroct />);

expect(window.croct).toBe(croct);
});
});
31 changes: 31 additions & 0 deletions examples/next-ssr-app/components/GlobalCroct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import {FunctionComponent, useEffect} from 'react';
import croct from '@croct/plug';

/*
* NOTE
*
* This component makes the `croct` instance available on the `window`
* object so tools like Google Tag Manager can use it.
*
* Although functional, we do not recommend this approach as it can
* lead to problems like race conditions where Google Tag Manager may
* run before the `useEffect` hook.
*
* For more reliable and efficient event tracking, use the `useCroct`
* hook directly in your application.
*/

export const GlobalCroct: FunctionComponent = () => {
useEffect(
() => {
if (!('croct' in window)) {
Object.assign(window, {croct: croct});
}
},
[],
);

return null;
};
2 changes: 1 addition & 1 deletion examples/next-ssr-app/components/Providers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('<Providers />', () => {
{
appId: APP_ID,
clientId: clientId,
children: 'foo',
children: expect.arrayContaining(['foo']),
},
expect.anything(),
);
Expand Down
2 changes: 2 additions & 0 deletions examples/next-ssr-app/components/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {PropsWithChildren, ReactElement} from 'react';
import {CroctProvider} from '@croct/plug-react/CroctProvider';
import {headers} from 'next/headers';
import {Header} from '@/lib/constants';
import {GlobalCroct} from '@/components/GlobalCroct';

export default function Providers({children}: PropsWithChildren): ReactElement {
return (
<CroctProvider
appId={process.env.NEXT_PUBLIC_CROCT_APP_ID!}
clientId={headers().get(Header.CLIENT_ID) ?? undefined}
>
<GlobalCroct />
{children}
</CroctProvider>
);
Expand Down
Loading
Loading