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

window is not defined on SSR with NextJS #167

Open
gokhancelikkaya opened this issue Oct 13, 2023 · 5 comments
Open

window is not defined on SSR with NextJS #167

gokhancelikkaya opened this issue Oct 13, 2023 · 5 comments

Comments

@gokhancelikkaya
Copy link

The component works fine but with an error when using with NextJS 13. It is because it tries to access window on SSR part. The error happens only when reloading the page and error is logged on the server side (not on browser console).

 ⨯ node_modules/react-share-on-social/dist/react-share-on-social.cjs.development.js (480:0) @ eval
 ⨯ ReferenceError: window is not defined
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (./src/components/modal/social-bar.tsx:11:79)
    at (ssr)/./src/components/modal/social-bar.tsx (.next/server/app/(blog)/[blog]/page.js:401:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (./src/components/post/post.tsx:18:76)
    at (ssr)/./src/components/post/post.tsx (.next/server/app/(blog)/[blog]/page.js:478:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (./src/components/blog/blog-timeline.tsx:7:79)
    at (ssr)/./src/components/blog/blog-timeline.tsx (.next/server/app/(blog)/[blog]/page.js:214:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
@fonzarely
Copy link

Same issue for me, and I could not manage to propose a fix.
The component looks great any way.

@kassemitani
Copy link

Same issue with NextJS

@nikolai-vysotskyi
Copy link

@kassemitani @fonzarely @gokhancelikkaya

If anyone runs into this problem on nextjs, just make only this component render on client:

    import dynamic from "next/dynamic";
    
    const DynamicShareOnSocial = dynamic(
        () => import('react-share-on-social'),
        {ssr: false}
    );

and then use it the same way:

     <DynamicShareOnSocial
      textToShare="Check out this new wardrobe I just found from IKEA!"
      link="https://ikea.com/wardrobes/kalle"
      linkTitle="KALLE Wardorbe which chooses your clothes for you using AI - IKEA"
      linkMetaDesc="Stop going through the agony of choosing clothes that fit the weather and your mood."
      linkFavicon={favicon}
      noReferer
    >
      <button>Share this product</button>
    </DynamicShareOnSocial>

@manolo-battista
Copy link

@kassemitani @fonzarely @gokhancelikkaya

If anyone runs into this problem on nextjs, just make only this component render on client:

    import dynamic from "next/dynamic";
    
    const DynamicShareOnSocial = dynamic(
        () => import('react-share-on-social'),
        {ssr: false}
    );

and then use it the same way:

     <DynamicShareOnSocial
      textToShare="Check out this new wardrobe I just found from IKEA!"
      link="https://ikea.com/wardrobes/kalle"
      linkTitle="KALLE Wardorbe which chooses your clothes for you using AI - IKEA"
      linkMetaDesc="Stop going through the agony of choosing clothes that fit the weather and your mood."
      linkFavicon={favicon}
      noReferer
    >
      <button>Share this product</button>
    </DynamicShareOnSocial>

I try it, it doesn't work on Next.js 14.2.3

@manolo-battista
Copy link

@manolo-battista do you use "use client"; in the component where you call it? If not, try this. This way works with pages router, I can test with app router in few hours

Yes, I tried with use client, this is my component:

"use client";
import ShareIcon from "@/components/icons/ShareIcon";
import Button from "@/components/ui/Button";
import { cn } from "@/lib/cn";
import { IFile } from "@/types/File";
import React from "react";
import dynamic from "next/dynamic";
    
const ShareOnSocial = dynamic(
      () => import('react-share-on-social'),
      {ssr: false}
);

export default function ShareMeetup({
  styleBtn,
  image,
  title,
  description,
}: {
  styleBtn?: string;
  image: string | IFile;
  title: string;
  description?: string;
}) {
  return (
    // TODO: temporary fix for window undefined on SSR
    typeof window !== "undefined" && (
      <ShareOnSocial
        textToShare="Ciao! Vuoi partecipare a questo meetup?"
        link={window.location.href}
        linkTitle={title}
        linkFavicon={image}
        copyToClipboardText="Copia link"
        copySuccessText="Copiato"
        linkMetaDesc={description}
        shareTo={[
          "whatsapp",
          "linkedin",
          "telegram",
          "facebook",
          "twitter",
          "email",
        ]}
        closeText="Chiudi">
        <Button
          className={cn(
            "bg-primary border-none text-background w-full",
            styleBtn,
          )}
          startAdornment={<ShareIcon />}>
          Share
        </Button>
      </ShareOnSocial>
    )
  );
}

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

5 participants