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 in Next.js #774

Open
Dnguye92 opened this issue Mar 22, 2021 · 1 comment
Open

window is not defined in Next.js #774

Dnguye92 opened this issue Mar 22, 2021 · 1 comment

Comments

@Dnguye92
Copy link

Dnguye92 commented Mar 22, 2021

Trying to integrate the web sdk into a nextjs app but the snippet from the docs already throws syntax errors.
image

I've also tried integrating with the npm package but when I try to init then I get a window is not defined error even though I'm initializing in a useEffect hook. Any clues as to what's happening?
image

@schonfeld
Copy link

I think the trick is to import the SDK dynamically, as explained in https://nextjs.org/docs/advanced-features/dynamic-import.

I'm doing something along these lines:

useEffect(() => {
  async function initAndFetch() {
    const BranchSDK = (await import('branch-sdk')).default

    BranchSDK.init(process.env.NEXT_PUBLIC_BRANCH_KEY!)

    BranchSDK.data(function (err, data) {
      if (err) {
        console.warn(`Branch failed to resolve link: ${err}`)
        return
      }

      // do something with `data`?
    })
  }

  initAndFetch()
}, [])

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