javascript - react-html5video 是否与服务器端渲染兼容? (使用 next.js)

标签 javascript html reactjs html5-video next.js

我正在尝试使用 react-html5video 实现示例视频播放器组件,并且也在服务器端渲染中实现。我用过 next.js为此。

  1. 首先,我在 ./pages/playerPage.js 下创建了一个名为 playerPage 的文件
  2. 然后我在 ./components/player.js 下创建了播放器组件
  3. 然后关注https://github.com/mderrick/react-html5video/blob/1.4.0/README.md#advanced-usage
  4. 这是我的播放器组件的样子。

import React from ‘react’;
import { default as Video, Controls, Play, Mute, Seek, Fullscreen, Time, Overlay } from ‘react-html5video’;

class Player extends React.Component {
    render() {
        return (
            <div>
                <Video controls autoPlay loop muted poster=“http://sourceposter.jpg“>
                <source src=“http://sourcefile.mp4” type=“video/mp4" />
                <source src=“http://sourcefile.webm” type=“video/webm” />
                <h1>Optional HTML and components can be added also</h1>
                <Overlay />
                <Controls>
                    <Play />
                    <Seek />
                    <Time />
                    <Mute />
                    <Fullscreen />
                </Controls>
            </Video>
            </div>
        );
    }
}

export default Player;

  1. 现在在 playerpage.js 中引入了 player.js 组件。

import Player from '../components/player'

export default () => (
    <Player />
)

  1. 如果运行:npm run dev 并转到 http://localhost:3000/playerPage,我会收到类似图像的错误。
  2. Getting errors like this

我已经在 ZEIT community 中提到了我的问题其中一位说:

Sounds like react-html5video is not compatible with server side rendering or something.

现在我很困惑:有没有兼容 React 和服务器端渲染的视频播放器??

建议将不胜感激。

最佳答案

看来这个包确实不兼容SSR。您可以尝试使用 Next.js-native Dynamic Imports 在客户端延迟加载您的 Player 组件。 :

import dynamic from 'next/dynamic'

const Player = dynamic(import('../components/player'), {
  ssr: false,
  loading: () => <p>Loading player...</p>,
});

export default () => (
  <Player />
);

关于javascript - react-html5video 是否与服务器端渲染兼容? (使用 next.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076768/

相关文章:

html - 在 div 中垂直居中文本

reactjs - iOS DatePicker 在 React Native 中不显示

javascript - 包含行的表内的容器元素

JavaScript/PHP : Open window to install twitter API then send back user info to main window

javascript - 如果 AngularJS 复选框被过滤掉然后返回,则它们不会保持选中状态

html - 基于屏幕分辨率的固定尺寸元素

javascript - Jquery嵌套排序: Uncaught TypeError: Cannot read property 'match' of undefined

c# - 如何在 .NET (C#) 中获取 UserControl 的 HTML 输出?

node.js - require 客户端实现同构 javascript 与 React 无法识别

javascript - 如何处理react-redux应用中加载中断的情况