简体   繁体   English

无法在 Next.js 中获取动态组件以跳过服务器端渲染并仅在客户端上显示

[英]Unable to get dynamic component in Next.js to skip server side rendering and show only on the client

I am trying to dynamically render a custom react component containing react-owl-carousel in a next js application.我正在尝试在下一个 js 应用程序中动态呈现包含react-owl-carousel的自定义反应组件。

However, due to the nature of the react-owl-carousel it cannot be server rendered.然而,由于 react-owl-carousel 的性质,它不能被服务器渲染。 So i decided to skip server side rendering for the dynamic import by setting ssr to false like so:所以我决定通过将ssr设置为 false 来跳过动态导入的服务器端渲染,如下所示:

 const Testimonials = dynamic(
  () => import('../components/home/Testimonials'),
  {
    ssr: false,
    loading: () => <p>...loafing</p>
  }
)

The complete Home component looks like this:完整的 Home 组件如下所示:

import React from 'react';

const Testimonials = dynamic(
  () => import('../components/home/Testimonials'),
  {
    ssr: false,
    loading: () => <p>...loafing</p>
  }
)

export default class Home extends Component {

    render () {
      return (
       <div>
        <Testimonials />
       </div>
      );

}

The Testimonial component looks like so: Testimonial 组件如下所示:

import React from 'react';
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';

const Testimonials = () => {


  return (
    <div>
      <p>skjdjks djks dk sjdk</p>
      <OwlCarousel
        className="owl-theme"
        loop
        margin={10}
        nav
      >
        <div class="item"><h4>1</h4></div>
        <div class="item"><h4>2</h4></div>
        <div class="item"><h4>3</h4></div>
        <div class="item"><h4>4</h4></div>
        <div class="item"><h4>5</h4></div>
        <div class="item"><h4>6</h4></div>
        <div class="item"><h4>7</h4></div>
        <div class="item"><h4>8</h4></div>
        <div class="item"><h4>9</h4></div>
        <div class="item"><h4>10</h4></div>
        <div class="item"><h4>11</h4></div>
        <div class="item"><h4>12</h4></div>
    </OwlCarousel>
    </div>
  );
}

export default Testimonials;

However, i have been battling with the dynamic component to load on the client side for about 4 hours now.但是,我一直在与动态组件作斗争以在客户端加载大约 4 个小时。 On the client side it shows only the loading indicator.在客户端,它只显示加载指示器。 if i remove the ssr option it tries to server render the component and throws an error.如果我删除ssr选项,它会尝试服务器呈现组件并引发错误。 Please find attached a screenshot showing what i am currently seeing on the client side.请找到附件截图,显示我目前在客户端看到的内容。 客户端截图

UPDATE: if i comment out everything in the 'Testimonials' component that has to do with OWL Carousel, the component shows the loading indicator and is rendered in the client side.更新:如果我注释掉“推荐”组件中与 OWL Carousel 相关的所有内容,该组件将显示加载指示器并在客户端呈现。 So i am guesing the issue is with Owl carousel.所以我猜测问题出在 Owl carousel 上。 not sure what it is tho yet.还不确定它是什么。

Eventually, after much debugging and trying different things i was able to get it to work by following these steps:最终,经过多次调试和尝试不同的事情后,我能够按照以下步骤使其工作:

1) Install jquery npm install jquery --save 1) 安装 jquery npm install jquery --save

2) Update the next.config.js file to include jquery via the webpack's provide plugin like so: 2) 通过 webpack 的提供插件更新 next.config.js 文件以包含 jquery,如下所示:

const withCSS = require('@zeit/next-css')
const withLess = require('@zeit/next-less')
const webpack = require('webpack');

const withFonts = require('next-fonts');
module.exports = withFonts(withLess(withCSS(
  {
    webpack: function (config) {
      config.module.rules.push({
        test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 100000,
            name: '[name].[ext]'
          }
        }
      })

      config.plugins.push(new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
      }))

      return config
    }
  }
)));

In my case i am also using these next js module: @zeit/next-css @zeit/next-less , hence my configuration looks like so.就我而言,我还使用了下一个 js 模块: @zeit/next-css @zeit/next-less ,因此我的配置看起来像这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Next.js:如何将仅外部客户端的 React 组件动态导入到开发的服务器端渲染应用程序中? - Next.js: How to dynamically import external client-side only React components into Server-Side-Rendering apps developed? Next.js 未在服务器端渲染中渲染 CSS - Next.js is not rendering CSS in Server Side Rendering Next.js 中的动态路由是呈现在服务器端还是客户端? - Are dynamic routes in Next.js rendered server-side or client-side? Next.js:服务端渲染页面请求高时无响应(动态路由) - Next.js : no response when request goes high in server-side rendering page(dynamic routing) Next.js static 服务器端渲染和 Gatsby.js - Next.js static server side rendering and Gatsby.js Next.js 中的客户端和服务器端是什么? - What is client side and server side in Next.js? 使用 Next.js 对服务器端渲染进行 React Query - React Query with server side rendering using Next.js 使用 next.js 与传统 SSR 进行服务器端渲染 - Server side rendering with next.js vs traditional SSR 如何让 Next.js 做 SSR(服务器端渲染)? - How to make Next.js do SSR (Server Side Rendering)? 用于服务器端渲染的 Next.js 路由器中的匹配和参数对象 - Match and Param object in Next.js router for Server side rendering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM