简体   繁体   English

使用Loadable从react-loadable拆分代码会导致屏幕闪烁

[英]code splitting with Loadable from react-loadable causes screen flicker

I'm trying to apply code splitting to my React app. 我正在尝试将代码拆分应用于我的React应用程序。 The below code is my routes with code splitting applied by Loadable from react-loadable . 下面的代码是我的路线,其中Loadablereact-loadable了代码拆分。 With the implementation, every time I access each page, the code chunk from webpack is properly loaded. 通过该实现,每次我访问每个页面时,都会正确加载来自webpack的代码块。 However, when I access my webpage initially, it brings about screen flicker initially. 但是,当我最初访问网页时,最初会导致屏幕闪烁。 After that, if I move to another page, no more screen flicker. 之后,如果我移至另一页,则不再有屏幕闪烁。 It only happens when accessing my website initially. 仅在最初访问我的网站时发生。

Does anyone know why it's happening? 有人知道为什么会这样吗? Is there something wrong on my configuration? 我的配置有问题吗?

By the way, I used server-side rendering with Node.js in my React app. 顺便说一句,我在React应用程序中使用了Node.js进行服务器端渲染。 Does it have something to do with screen flicker by code splitting? 它与代码拆分引起的屏幕闪烁有关吗?

routes.jsx routes.jsx

import React from "react";
import Loadable from "react-loadable";
import { Route, Switch } from "react-router-dom";

import NotFound from "routes/not-found";

const Landing = Loadable({
  loader: () => import(/* webpackChunkName: "landing" */ "routes/landing"),
  loading: () => null,
  modules: ["landing"]
});

const About = Loadable({
  loader: () => import(/* webpackChunkName: "about" */ "routes/about"),
  loading: () => null,
  modules: ["about"]
});

export default props => {
  return (
    <Switch>
      <Route
        exact
        path="/"
        render={routeProps => <Landing {...routeProps} options={options} />}
      />
      {/* <Route exact path="/" component={Homepage} /> */}
      <Route
        exact
        path="/about"
        render={routeProps => <About {...routeProps} options={options} />}
      />

      <Route component={NotFound} />
    </Switch>
  );
};

src/index.js SRC / index.js

const { store, history } = createStore();
const generateClassName = createGenerateClassName();

const Application = (
  <JssProvider generateClassName={generateClassName}>
    <MuiThemeProvider theme={theme}>
      <Provider store={store}>
        <ConnectedRouter history={history}>
          <Frontload noServerRender={true}>
            <App />
          </Frontload>
        </ConnectedRouter>
      </Provider>
    </MuiThemeProvider>
  </JssProvider>
);

const root = document.querySelector("#root");

if (root.hasChildNodes() === true) {
  // If it's an SSR, we use hydrate to get fast page loads by just
  // attaching event listeners after the initial render
  Loadable.preloadReady().then(() => {
    hydrate(Application, root, () => {
      document.getElementById("jss-styles").remove();
    });
  });
} else {
  // If we're not running on the server, just render like normal
  render(Application, root);
}

the flickering is happening because the router don't have the source of your component when routing and it renders null as you defined in loading func provided to Loadable HOC to prevent flickering you can render a Loading component that should look something like this: https://github.com/osamu38/react-loadable-router-example/blob/master/src/components/Loading.js 之所以会发生闪烁,是因为路由器在路由时没有组件的来源,并且如您在提供给Loadable HOC的loading func中定义的那样呈现为null ,以防止闪烁,因此您可以呈现应该如下所示的Loading组件: https: //github.com/osamu38/react-loadable-router-example/blob/master/src/components/Loading.js

or you can preload all the pages onComponentDidMount like this (you should convert your component to class component for it): 或者您可以像这样预加载ComponentDidMount上的所有页面(您应该将其转换为类组件):

 componentDidMount = () => {
    About.preload()
    Landing.preload()
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM