question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code splitting not working(based on react router's dynamic routes)

See original GitHub issue

Hi! I’m trying to implement code splitting based on router’s dynamic feature(https://github.com/rackt/react-router/blob/master/docs/guides/advanced/DynamicRouting.md) like that

  path: 'course/:courseId',

  getChildRoutes(location, callback) {
    require.ensure([], function (require) {
      callback(null, [
        require('./routes/Announcements'),
        require('./routes/Assignments'),
        require('./routes/Grades'),
      ])
    })
  },

  getIndexRoute(location, callback) {
    require.ensure([], function (require) {
      callback(null, {
        component: require('./components/Index'),
      })
    })
  },

  getComponents(location, callback) {
    require.ensure([], function (require) {
      callback(null, require('./components/Course'))
    })
  }

It works fine in this commit(just react-router and basic server render). https://github.com/EcutDavid/example-react-router-server-rendering-lazy-routes/commit/6e070ee4a573fefe730dec2eadbe6dc38d0e5529

But it not works on my application based on this project. image Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method ofRoutingContext.

Currently my code like that: -routes –index.js

// polyfill webpack require.ensure
if (typeof require.ensure !== 'function') require.ensure = (d, c) => c(require)

export default {
  component: 'div',
  getChildRoutes(location, cb) {
    require.ensure([], (require) => {
      cb(null, [
        require('./ShareGraphRoute'),
        require('./AppRoute'),
        require('./NotFoundRoute'),
      ])
    })
  }
}

-routes –ShareGraphRoute

// polyfill webpack require.ensure
if (typeof require.ensure !== 'function') require.ensure = (d, c) => c(require)

import {
  SHARE_GRAPH_PATH,
} from 'constants/defaults'

export default {
  path: SHARE_GRAPH_PATH,
  getComponent(location, cb) {
    require.ensure([], (require) => {
      cb(null, require('components/ShareGraph'))
    })
  },
}

Seems need change in server.js or I made some mistakes, any hint? thanks!!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
svrcekmichalcommented, Jan 13, 2016

You are welcome 😃

1reaction
svrcekmichalcommented, Jan 13, 2016

@EcutDavid have you got polifyll in your first file? What babel are you using? If you are using babel6, you need to specify default to use default export in require like this:

...
require('./routes/Announcements').default,
...

is your source hosted somewhere? I can look and help you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

React dynamic imports and route-centric code splitting guide
The react-router-dom library supports inbuilt route-level code splitting. It allows us to download chunks at the route level. Using this feature ...
Read more >
Code Splitting with React and React Router - Medium
If you run npm run build with an app created by Create React App, you'll see our app's been split. Each chunk is...
Read more >
Route based Code Splitting with React Router - YouTube
Route based Code Splitting with React Router ; Courses - https://learn.codevolution.dev/ ; Support UPI - https://support.codevolution.dev/ ...
Read more >
Code Splitting with React, React.lazy, and React Router - ui.dev
In this post you'll learn how to increase the performance of your React application by adding code splitting with React.lazy and React ......
Read more >
Routing and Code Splitting in React - Better Programming
In this piece, I'll demonstrate how to use React Router and at the same time take into account performance optimization using route-based code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found