Skip to content

Instantly share code, notes, and snippets.

@threepointone
Last active March 11, 2017 23:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threepointone/91670a9b5cf7dab254cc38ee983b3308 to your computer and use it in GitHub Desktop.
Save threepointone/91670a9b5cf7dab254cc38ee983b3308 to your computer and use it in GitHub Desktop.
// before
// index.js
import React from 'react'
import { render } from 'react-dom'
import App from './app'
render(<App/>, window.root)
// and assume you've done some import() shenanigans in app.js and/or other files
// after
// index.js
import React from 'react'
import { render } from 'react-dom'
import { gimme } from './pages'
import App from './app'
gimme('home', () => { // you'd have some conditional here matching window.location.href with names
// don't even do anything with the recived module here, just render App
render(<App/>, window.root)
})
// and in your app.js / other files, use `gimme(name)` instead of import(path)
// pages.js
let mapping = {
'home': () => import('./home'),
'list': () => import('./list'),
'details': () => import('./details')
}
let modules = {}
export function gimme(name, cb){
if(modules[name]){ // check cache, possibly sync
return cb(modules[name])
}
let fn = mapping[name]
fn().then(m => {
// populate cache for
modules[name] = m
cb(m)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment