!Wolf of Wall Street gif
react-brokerA lightweight library for lazy components using React 16.8+. It's perfect for code splitting and has the simplest SSR story you've ever seen out-of-the-box.
Critically, this package is only intended to work with Webpack, specifically Webpack 4 and future versions. There are no plans to implement a design accommodating Parcel or other bundlers. There is also a hard requirement for babel-plugin-macros (which is shipped with CRA) if you opt to use the macro.
``js import {BrokerProvider} from 'react-broker' import lazy from 'react-broker/macro'
// Automatically generates dynamic imports for webpack with babel-plugin-macros. // Just give it the path. const LazyPage = lazy('../pages/Page', {loading: props => 'Loading...'})
//////////////////////////////////////////////////////////////////////////////// // ⬇ BECOMES ⬇ // ///////////////////////////////////////////////////////////////////////////////
const LazyPage = require('react-broker').lazy( 'src/pages/Page', () => import(/ webpackChunkName: "src/pages/Page" / '../pages/Page'), {loading: props => 'Loading...'} ) function App () { // Look at me! I'm used like a normal component. return ( ) } `
$3 - Webpack 4+ (because chunks
) - React 16.8+ (because hooks
) - Babel (because babel-plugin-macros
)
$3 yarn add react-broker
or npm i react-broker
$3 Hello world Hello world w/ Router --------------------------------------------------------------------------------
Documentation $3 The function that transforms your imports and delegates your async components.
`
js import lazy from 'react-broker/macro'`
####
lazy(component
, options )component {String} A path to a React component you want to lazy load. The component must be in the default export of the file. Paths cannot be passed via an identifier, it has to be a plain string. It is used just like a regular component.
You may also lazy load external library components, but just know that the component in question must be the
default export.`js // Used like a regular component const LazyPage = lazy('./pages/Home', {loading: props => 'Loading ${props.id}...'}) // ...
`options
{Object} - loading (props, context{retry, error}) - props props passed the component - context - retry is a function which will force a reload of the component - error is any error returned by Promise.reject, this is only relevant if an error component isn't also defined in options - error (props, context{retry, error}) - See loading --------------------------------------------------------------------------------
$3 Manages code-splitting and the resolution of your async components by keeping track of which chunk names have been loaded and also determining which need to be included from the server-side. Broker.Provider must be defined at the top-level of your lazy loaded components.#### Props ##### chunkCache
{Broker.createChunkCache} You only provide a chunkCache on the server side. In the client it is not allowed. The chunk cache is used for tracking which chunks were loaded during the latest render phase of the app.Broker.createChunkCache
`js import * as Broker from 'react-broker'const chunkCache = Broker.createChunkCache()
function App (props) { return ( ) }
` --------------------------------------------------------------------------------
$3 Creates a context for Broker.Provider to track chunks in and provides helper methods to provide access to those chunks.#####
createChunkCache.getChunkNames() Returns an array of all the Webpack chunk names loaded into the current app.#####
createChunkCache.getChunks(webpackStats) Returns a Set of all the Webpack chunks loaded into the current app. - webpackStats - The stats object created by Webpack. ##### createChunkCache.getChunkScripts(webpackStats, options) Returns a string representation of all the