Code Monkey home page Code Monkey logo

route-level-code-split's Introduction

Route level code splitting without flickering

Go to Demo (Test this using Slow 3G for better results)

Many people had mentioned how to use route level code splitting for performance. In React, you can use it with React.lazy and React.Suspense. The main drawback of this approach is that the fallback prop of the Suepense can only be written something like

<div>Loading...</div>

which should't exist in your million-dollar app ๐Ÿ˜. You might have also seen some flickering issues which happen when page one is unmounted, fallback is shown and then page two is mounted, where all of these are happening relatively fast.

On the other hand, many sites like Facebook, Instragram and Youbute try to keep the previous page until the next page is downloaded and ready to be mounted, when navigating.

I've tried many different approaches. You can see them in this repo's branches. The only one that have worked out for me is to update the fallback after a page is rendered(mounted) and to memorize the routes that are rendered.

The idea here is that we must keep fallback as a state and update it when a page is renderd. But if we do so, there will be an infinite rendering because the page itself is a child of Suspense. When parent state changes, all children will be re-rendered,again updating the fallback. Here's when useMemo come into play. We'll have to memoize all child routes that are rendered so that when fallback is updated they are not rendered again. And when a page is rendered, we'll update the fallback but how?

Again, we'll use useMemo to memoize the component(page) and render the memoized and update the fallback with the memoized so that they will be in sync. Both have to be a single instance because the page can also have popups and form states and when navigating, those popups and form states must remain in fallback.

The above approach is not well tested though. I have only tested a fake modal. And if you have to include very complex UI components (like popovers and portals) , I wouldn't garuantee this works. Feel free to open an issue then.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.