Frontity Docs
  • ยป Welcome to Frontity
  • ๐Ÿš€Getting started
    • Quick start guide
  • ๐Ÿ“ƒAbout Frontity
    • Frontity features
    • Browser support
    • Get involved
  • ๐Ÿ“šCore Concepts
    • 1. Project
    • 2. Settings
    • 3. Packages
    • 4. Roots
    • 5. State
    • 6. Actions
    • 7. Libraries
    • 8. Namespaces
    • 9. Styles
  • ๐Ÿ—๏ธArchitecture
    • Decoupled Mode
    • Embedded Mode
  • ๐ŸŒŽDeployment
    • Deploy Frontity using Vercel
    • Deploy Frontity on Layer0
    • Deploy Frontity on Heroku
  • ๐ŸŒ—Isomorphic React
  • โšก๏ธ Perfomance
    • Caching
    • Link prefetching
    • Lazy Loading
    • Code Splitting
  • ๐Ÿ”ŽSEO
  • ๐Ÿ“–Guides
    • Setting the URL of the WordPress data source
    • Using Environment Variables in a Frontity project
    • WordPress requirements for Frontity
    • URLs in a Migration from WordPress to Frontity Decoupled Mode
    • Frontity Query Options
    • Redirections with Frontity
    • Understanding a Frontity project
    • Add a new Frontity package or theme to your project
    • How to share your Frontity project
    • Understanding Mars Theme
    • Working with processors
    • How to process page-builder content in Frontity
    • Keep Frontity Updated
    • Troubleshooting
    • JavaScript
    • React
  • ๐Ÿ‘Contributing
    • How to contribute?
    • Contributing Guide
Powered by GitBook
On this page
  • Code Splitting Use Case: Comments
  • Loadable Components Documentation

Was this helpful?

  1. โšก๏ธ Perfomance

Code Splitting

Code Splitting lets you split your code into various bundles, instead of using a single one with all the code. These smaller bundles are dynamically loaded at runtime depending on the URL.

If used properly, this can mean important performance gains.

The bundles can be loaded on demand or in parallel, which allows you to just load the code that is currently needed by the user. This way you can avoid loading heavy code until it is required and reduce the amount of code during the initial load.

Frontity has configured everything to make Code Splitting really easy.

To use it you just have to import the { loadable } module from frontity and then dynamically import the React component that you don't want to be loaded until it is strictly needed.

import { loadable } from "frontity";
const OtherComponent = loadable(() => import('./OtherComponent'))

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  )
}

Code Splitting Use Case: Comments

Imagine you are using a big library for showing your comments. You will want to load it just when it is needed, so it doesn't increase the bundle size if that React component is not loaded.

Code splitting lets you do it.

You have to use loadable with a dynamic import() inside:

import { loadable } from "frontity";
import Content from "./components/content";

// Thanks to loadable we prevent comments from loading until it's needed.
const HeavyComments = loadable(() => import('./components/comments'));

const Post = ({ state }) => (
    <>
        <Content />
        {state.comments.areOpened && <HeavyComments />}
    </>
);

export default connect(Post);

Instead of using the normal import ... from.

import { loadable } from "frontity";
import Content from "./components/content";
import HeavyComments from "./components/comments";

const Post = ({ state }) => (
    <>
        <Content />
        {state.comments.areOpened && <HeavyComments />}
    </>
);

export default connect(Post);

By default, state.comments.areOpened === false .

The heavy library used for comments won't be loaded until you change the state to true such as when, for example, you click a button to open the comments. At that moment the code for that React component is downloaded and executed.

If we don't use loadable , the <HeavyComments> component is included in the main bundle and loaded at the initial page load, even if the comments are never shown.

Loadable Components Documentation

PreviousLazy LoadingNextSEO

Last updated 3 years ago

Was this helpful?

For managing the Code Splitting, Frontity has integrated and configured .

If you want to go deeper, you should take a look at . You don't need to read the docs on how to install and configure Loadable Components since we have already done that work for you. Below are concepts that are interesting and helpful to read up on:

What is ?

Most of the time, you want to a component, it means it will be loaded when the browser is idle.

Specify a in loadable options.

Handle loading errors with .

To avoid flashing a loader if the loading is very fast, you could implement a minimum

Infinite loading is not good for user experience, to avoid it implementing a is a good workaround.

Use to defer the loading of a library.

Create a reusable Loadable Component by using a .

Loadable Components
their docs
Code Splitting
prefetch
Fallback
Error Boundaries
Delay
timeout
Library Splitting
Dynamic Import
loadable components