prismicio / prismic-react

React components and hooks to fetch and present Prismic content
https://prismic.io/docs/technologies/homepage-reactjs
Apache License 2.0
153 stars 41 forks source link

Can't access property "load", window.twttr.widgets is undefined #89

Closed tim-richter closed 2 years ago

tim-richter commented 3 years ago

Hey everyone, we run into some issues with the twitter embed functionality and get error's thrown when navigating pages in our next.js app.

We use twitter analytics on our site. This means we already have window.twttr defined on all pages by default but window.twttr.widgets is not defined since this is only loaded when on a page with a twitter embed.

This is not checked correctly by the twitter embeds load check which only checks if window.twttr is present but not widgets. In some cases this causes our site to break because twttr.widgets is not loaded fast enough but the load function is still executed.

I think just adding an additional check for window.twttr.widgets in the if statement would resolve this.

export const embeds = {
  Twitter: {
    ...
    load: function() {
      if (window && window.twttr) {
        window.twttr.widgets.load();
      }
    }
  },
lihbr commented 3 years ago

Hey @tim-richter thanks for your contribution!

This sounds like a valid bug, if you'd like to submit a PR that'd be more than welcome, else we're adding that to our backlog, thanks!

Also just for your intention, we will refactor and enhance this kit in the near future :)

Cheers!