Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional loading / Lazy loading #169

Open
ptomasroos opened this issue May 21, 2015 · 11 comments
Open

Conditional loading / Lazy loading #169

ptomasroos opened this issue May 21, 2015 · 11 comments

Comments

@ptomasroos
Copy link

Any plans or thoughts how to handle conditional loading?
There are a few cases where the entire app in javascript might be too big.
Async loading of the async deferred script needed for the current launched page.

Thoughts or ideas?
Require.js / Webpack

@ptomasroos ptomasroos changed the title Conditional loading Conditional loading / Lazy loading May 21, 2015
@gpbl
Copy link
Contributor

gpbl commented May 21, 2015

I'd go with webpack's code splitting: for example, if you are using fluxible-router you could put a require.ensure in a route action and call the done function in its callback.

@agrippanux
Copy link

Code splitting + gzip compression helped us a ton

@jamesjjk
Copy link

Has anyone achieved this with an isomorphic app?

@mridgway
Copy link
Collaborator

We have been using webpack's code splitting by creating a loader.js file that maps a bundle name to a require.ensure call. We then have a Fluxible bundle plugin where you register all the bundles on the server-side and then call context.loadBundle from within an action. The bundle plugin keeps track of which bundles have been "loaded" and rehydrates those bundles on the client using the loader.js. Any new actions that execute loadBundle on the client will lazy load new bundles as they're needed.

Here is a code snippet of our plugin and the loader.js file. It's not perfect, but it works for now: https://gist.github.com/mridgway/53745e0da019058cc277

@jamesjjk
Copy link

jamesjjk commented Aug 1, 2015

@mridgway Nice work, do you get any checksum errors? How do you avoid when the bundle is already loaded server side?

@jamesjjk
Copy link

@mridgway Can you give an example of how you use this plugin?
Much appreciated!

@taylorjames
Copy link

@mridgway Id also like to see an example

@rickihastings
Copy link

Has anyone managed to achieve this? The loader.js gist doesn't really help without a decent example on how to use it.

@localnerve
Copy link
Contributor

An example of this in an isomorphic app is https://github.com/localnerve/flux-react-example-sw.

In that example, a modal action has contents that can be conditionally loaded.

Specifically, the modal presents the Settings component, consisting of an action,
component, and store (and utility code) as a separate bundle (JS only).
You can (and probably should) also bundle your styles in the same bundle, but that example uses an older style build technique.

With Webpack, you have to hard code the split (the args to require.ensure), see the repo's utils/splits
file.

Hope that helps,
Alex
On Nov 22, 2015 11:03 AM, "Ricki Hastings" [email protected] wrote:

Has anyone managed to achieve this? The loader.js gist doesn't really
help without a decent example on how to use it.


Reply to this email directly or view it on GitHub
#169 (comment).

@rickihastings
Copy link

Thanks for the example. I've been able to get lazy loading "kind of" working. I can get the bundling and conditional loading working fine. The problem is that fluxible-router requires the handler to be defined, or even if one isn't defined, it can't be added or modified later on.

Or maybe it can, am I missing something?

@localnerve
Copy link
Contributor

Maybe I'm not understanding.
Can't you expand the available routes by sending RECEIVE_ROUTES in the downloaded code or in the ensure callback?
It sure looks like it will expand:

// RouteStore RECEIVE_ROUTES handler
_handleReceiveRoutes: function (payload) {
        this._routes = Object.assign(this._routes || {}, payload);
// etc...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants