chintan9 / plyr-react

A simple, accessible and customisable react media player for Video, Audio, YouTube and Vimeo
https://github.com/chintan9/plyr-react
MIT License
472 stars 51 forks source link

NextJs integration support #873

Open oalexdoda opened 2 years ago

oalexdoda commented 2 years ago

When loading a page with a player on localhost, a TypeError: Cannot read properties of null (reading 'getAttribute') error pops up.

To Reproduce

Steps to reproduce the behavior:

  1. Place a video on a page using a Plyr player.
  2. Navigate from one route (without a player) to another (with a player)

Expected Behavior

No popup error.

Screenshots

image

Desktop (please complete the following information):

Additional Context

Component Source Code

import Plyr from 'plyr-react';
import { Box } from '@mui/material';
import { useTheme } from '@mui/material/styles';
import 'plyr-react/dist/plyr.css';

const Player = ({
    src,
    type = 'video',
    provider = 'youtube',
    sources,
    sx,
    ...props
}) => {
    const theme = useTheme();

    return (
        <Box
            sx={{
                '--plyr-color-main': theme.palette.primary.main,
                borderRadius: `${theme.shape.borderRadius}px`,
                overflow: 'hidden',
                ...sx,
            }}
        >
            <Plyr
                source={{
                    type,
                    sources: sources || [
                        {
                            src: src,
                            provider: 'youtube',
                        },
                    ],
                }}
                {...props}
            />
        </Box>
    );
};

export default Player;
chintan9 commented 1 year ago

can create codesandbox demo for us?

haluvibe commented 1 year ago

can create codesandbox demo for us?

You can't because the issue only appears during yarn dev - not after it's built so codesandbox won't be able to replicate the issue but it's easy as starting a new nextjs project and

export default function IndexPage() {
  return (
    <div>
      <Plyr
        source={{
          type: "video",
          sources: [{ src: "9bZkp7q19f0", provider: "youtube" }]
        }}
      />
    </div>
  );
}
realamirhe commented 1 year ago

Hey @haluvibe , the only non-serverside action I'm suspicious of it is about this line: https://github.com/chintan9/plyr-react/blob/f5d6ab7b26fc4335b8eca499001369c77495cdfd/src/index.tsx#L38

But the error and our last investigation show that the problem was related to the plyr itself. Even when I used the next dynamic import with ssr: false on the custom Plyr wrapper component it didn't work well.

Any try-out or suggestions for fixing this issue is pleased

haluvibe commented 1 year ago

no idea, I can't have multiple videos with this library unfortunately atm

AmmadKhanM commented 1 year ago

@chintan9 the same issue I am facing in my project, can anyone give a solution, please...

erip2 commented 1 year ago

Same issue here. It also happens on this repo's Next.js example project.

oalexdoda commented 1 year ago

No news?

realamirhe commented 1 year ago

Hey all Barely forgot the issue, any help would be appreciated we are short of devs unfortunately but I put a reminder in my calendar to give it another shot next week if everything goes well.

oalexdoda commented 1 year ago

Thanks a lot @realamirhe - it's a major issue causing errors on every page load, multiple apps, etc. and makes Plyr React super difficult to use.

chintan9 commented 1 year ago

https://bobbyhadz.com/blog/javascript-cannot-read-property-getattribute-of-null

It look like there some issue how we use nextjs with library.

Same issue here. It also happens on this repo's Next.js example project.

@erip2 can you share deployed project?

oalexdoda commented 1 year ago

Whoever wrote that blog post using GPT to sell their book at the end… amateur 🤣

https://bobbyhadz.com/blog/javascript-cannot-read-property-getattribute-of-null

chintan9 commented 1 year ago

Whoever wrote that blog post using GPT to sell their book at the end… amateur 🤣

https://bobbyhadz.com/blog/javascript-cannot-read-property-getattribute-of-null

At least SEO was good.

I will look much deeper on this issue.

@altechzilla It very helpful if you create sample repo for this issue.

Please use latest stable version without material UI.

realamirhe commented 1 year ago

Hey @altechzilla, @erip2, @haluvibe

I post the issue in the plyr package and will post the update in this repo after getting an answer, make sure to upvote the discussion or repost it as an issue if you can help there.

https://github.com/sampotts/plyr/discussions/2599

oalexdoda commented 1 year ago

Thanks @realamirhe

MokDevelopment commented 1 year ago

damn ... we planned to use Plyr for 3 upcoming NextJs projects ... hopefully it can be fixed... strange is also that the error is thrown randomly - but in every NextJs project we tested it - solution for now is that we use another player.

Is it fixable in general or is Plyr just not compatible with the way NextJs works behind the scenes?

realamirhe commented 1 year ago

please consider keeping https://github.com/sampotts/plyr/discussions/2599 active till it gets its desired attraction.

I also tested mocking the media object in the plyr instance in the rendering, but it happens in the initiation phase, it might be possible to make another youtube object and load that in the plyr youtube player. but need internal plyr change.

could someone test the error occurrence with the patched plyr package (patch-package) and see error still exists, if that resolves the issue we might open a pull request for solving it ourselves?

kasvith commented 1 year ago

Im having the same issue

chintan9 commented 1 year ago

https://github.com/sampotts/plyr/issues/2636

kasvith commented 1 year ago

@MokDevelopment whats the alternative player you tried?

3mr-5aled commented 12 months ago

I Provided an id before the source attribute and it worked

<Plyr
      id={videoId}
      source={{
        type: "video",
        sources: [{ src: videoId, provider: "youtube" }],
      }}
      options={{
        // You can add any additional Plyr options here
        controls: [
          "play-large",
          "play",
          "progress",
          "current-time",
          "mute",
          "volume",
          "speed",
          "fullscreen",
        ],
        youtube: { noCookie: true }, // Use YouTube's privacy-enhanced mode (optional)
      }}
    />
henrybabbage commented 9 months ago

I'm having the same issue (I think). Player appears for a moment and then disappears. I am trying to render an array of video players. The error that Next JS throws is: TypeError: e.embed is null. I'm running it in NextJS app router, on the latest version 13.5.2. My component looks like this:

'use client'

import { css } from '@/styled-system/css'
import type { VideoProps } from '@/types'
import dynamic from 'next/dynamic'
import type { APITypes } from 'plyr-react'
import 'plyr-react/plyr.css'
import { useRef } from 'react'

const Plyr = dynamic(() => import('plyr-react'), { ssr: false })

export type PlayerProps = {
    provider: 'vimeo'
    // videoOptions?: any
} & VideoProps

export default function VideoPlayer(props: PlayerProps) {
    const { url, provider, _key, title } = props

    const ref = useRef<APITypes>(null)

    return (
        <div key={_key} className={css({ width: '100%', height: '100%', position: 'relative' })}>
            <Plyr
                ref={ref}
                key={_key}
                id="plyr-player"
                playsInline
                source={{
                    type: 'video',
                    sources: [
                        {
                            src: url,
                            provider: provider
                        }
                    ]
                }}
                // options={videoOptions}
            />
        </div>
    )
}
chintan9 commented 7 months ago

I have created discussing on nextjs repo

https://github.com/vercel/next.js/discussions/58911

please add more question and details also there

GabrielJesusS commented 2 months ago

When i tried to use react-plyr i bumped into the same issue:

image

Disclaimer: Ignore the rest of the page, is an example test only btw.

Debugging into the internal plyr and react-plyr files i finally found the function what is called to initiate the youtube script routine. Logging inside this function, i found a strange behavior, the function was been called twice, but the component or the other items inside they didn't suffered a purposeful re-render, which reminded me about a flag of react, the strict mode flag:

image

According to react docs, this flags causes an re-run on some functions and other items, among then, the functional components.

This "get attribute" error occurs just in dev mode because the react strict mode is active by default into next js app router since the version 13.4:

image

Testing and debugging without the strict mode flag, the error doesn't occurs, but this maybe not a good practice.

Conclusion: You can still using the react-plyr lib quietly, because this is not a "bug", is more like an scenario where most of us isn't accustomed to work. Even so, the lib must should predict scenarios like that to avoid errors like that caused by a default react behavior.