-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
"react-facebook-pixel" doesn't work with Next.js. "ReferenceError: window is not defined" #65
Comments
same here problem with ssr |
bump |
There are many solutions in issue #53. Instead of doing a direct import like
|
@superapplejuice While it may be true that there are workarounds, it would be extremely easy for this module to make itself SSR friendly (as it already was before v1.0). There can be build system side effects or other situations where dynamic imports won't necessarily work (or at least are far more complicated). It would be such an easy fix to make the module a no-op if a browser environment is not available. Since a user tracking system like pixel is by definition "client-side" there is no point in making developers have to jump through hoops to force the module to not load on the server. I'm more concerned that @zsajjad hasn't weighed in on the issue yet. Of course we can always fork the module since it would be an easy fix, but it is highly preferable for the original maintainers (who may have other features/enhancements in the works) to implement a fix that would make the library much easier to use. Maybe @zsajjad is busy; I know maintaining open source libraries can be a burden. I'm certainly thankful for the code--just hoping we can get it back to being SSR friendly. |
Hi sincere apologies for the delayed response. I would highly appreciate any PR or good suggestions on fixing this issue |
@zsajjad Thanks for the response. Can you explain why the change was made in eb70fa4 (patch for existing integration)? This is the cause of the SSR issues:
Is there a specific reason why the initialized variable needs to know if the fbq object exists before the It would seem like a pretty easy fix would simply be to change that line to:
I see that @mimcz appears to have already submitted a pull request with essentially this very change. |
I have merged that MR. Will release the updated version in a while. Thanks for taking this up. |
Hi. When will this be released to npmjs.com? Currently, it is version 1.0.3 there and was last published 5 months ago. |
New version is live |
@zsajjad This still fails for Gatsby SSR build. Using latest version 1.0.4.
How to reproduce:
|
@iaarnio it appears you are using the pre-built module in the Edited - see post below |
@zsajjad I've tested the new release and have the same problem @iaarnio has. Even when bundling with webpack the library is using the
|
I've dug into this a bit more and I think what is missing here is the transpiled source (usually put in a When I get a bit of free time I'll submit a PR that adds the Edit - I've now sent a PR with the relevant fixes |
import ReactPixel from 'react-facebook-pixel'; Error: why its not working with next js in the global js file or anywhere else also ? |
I had this same problem. "use client";
import React, { useEffect } from "react";
import { usePathname, useSearchParams } from "next/navigation";
import ReactPixel from 'react-facebook-pixel';
export const FacebookPixelEvents: React.FC = () => {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
import("react-facebook-pixel")
.then((x) => x.default)
.then((ReactPixel) => {
ReactPixel.init(`${process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID}`);
ReactPixel.pageView();
});
ReactPixel.init(`${process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID}`);
ReactPixel.pageView();
}, [pathname, searchParams]);
return null;
};
export const fbPixelAddToCart = async () => {
ReactPixel.fbq('track', 'AddToCart');
}
export const fbPixelInitiateCheckout = async () => {
ReactPixel.fbq('track', 'InitiateCheckout');
} This line was throwing the error: "window is not defined": import ReactPixel from 'react-facebook-pixel'; So i've made some conditional imports: "use client";
import React, { useEffect, useState } from "react";
import { usePathname, useSearchParams } from "next/navigation";
let ReactPixel: any = null;
if (typeof window !== 'undefined') {
import('react-facebook-pixel')
.then((x) => x.default)
.then((currReactPixel) => {
ReactPixel = currReactPixel;
})
}
export const FacebookPixelEvents: React.FC = () => {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
if(!ReactPixel) {
import('react-facebook-pixel')
.then((x) => x.default)
.then((currReactPixel) => {
ReactPixel = currReactPixel;
})
ReactPixel.init(`${process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID}`);
ReactPixel.pageView();
} else {
ReactPixel.init(`${process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID}`);
ReactPixel.pageView();
}
}, [pathname, searchParams]);
return null;
};
export const fbPixelAddToCart = async () => {
if(ReactPixel) ReactPixel.fbq('track', 'AddToCart');
}
export const fbPixelInitiateCheckout = async () => {
if(ReactPixel) ReactPixel.fbq('track', 'InitiateCheckout');
} Do a conditional import only when the window type is not undefined, and on the first render I had the problem that the React pixel was sometimes null, so in useEffect I also did a conditional import. (pedilo) |
When I install and try to use the library with Next.js, and Init the Pixel, then I have the following error: "ReferenceError: window is not defined"
This error is in fb-pixel.js and is fixed adding: if (typeof window !== "undefined") at the beginning of the script.
This modification could generate another issue?
The text was updated successfully, but these errors were encountered: