sst.dev icon indicating copy to clipboard operation
sst.dev copied to clipboard

Comments: Code Splitting in Create React App

Open jayair opened this issue 9 years ago • 30 comments

Link to chapter - http://serverless-stack.com/chapters/code-splitting-in-create-react-app.html

jayair avatar May 21 '17 23:05 jayair

Ok, while this is very useful, it really assumes all components to be found exactly in the same folder, right? (at least the main ones, you can then compose your views from there), but how do you then those components have sub routes, or the different routes in a component lead to async load components in different folders? (first thing on the top of my mind would be to pass path as a prop)

qborreda avatar May 23 '17 09:05 qborreda

const { default: component } = await import(``../containers/${componentName}``); How is this possible? Dynamic import.

minhna avatar May 23 '17 13:05 minhna

@qborreda Yeah, it does assume that. But I'm pushing an update soon that fixes this.

@minhna Yeah it is a dynamic import that's available in the new CRA.

jayair avatar May 23 '17 14:05 jayair

@qborreda Updated the chapter with a more generic way to do the imports and it also allows us to control the chunks that are generated - 3cf522260e1133c3055862b2a3a6e03afe511d07

jayair avatar May 23 '17 15:05 jayair

@jayair I'm using meteor 1.5 rc6 and it doesn't work. Do you have any idea? Thanks for your help.

minhna avatar May 24 '17 01:05 minhna

@minhna This is for Create React App and it's using Webpack 2 internally. I'm not sure how it works in other places.

jayair avatar May 24 '17 14:05 jayair

There's one more issue: asyncComponent() is called in a rendering function so anytime it executes, a completely new component class is created. This will destroy all state of components below.

gaearon avatar May 25 '17 22:05 gaearon

It seems like you could make <AsyncRoute> instead of asyncComponent. <AsyncRoute> could take getComponent as a prop and do the same setState thing, rendering Route when the component in the state is ready. Since <AsyncRoute> is a regular component that's the same every time, it wouldn't have this issue.

gaearon avatar May 25 '17 22:05 gaearon

Hmm. Now that I think of it I guess it's not a bug issue because the prop is only used once. But it's still unfortunate the class is generated on every top level render. So AsyncRoute as a component would work better.

gaearon avatar May 25 '17 23:05 gaearon

@gaearon That's a good point. It doesn't make sense that asyncComponent runs every time. Let me play around with turning it into a component.

jayair avatar May 26 '17 08:05 jayair

Updated with fix. Now asyncComponent is only run once as opposed to every time - d0fe33f0828285396a0ec128a9bc8b041ac704e8

For folks looking for an explanation; React Router will create a new instance of the component if an inline function is passed into the component prop as opposed to simply updating the existing component. To avoid this, we run the asyncComponent once at the top and pass in the resulting component to the routes.

jayair avatar May 30 '17 15:05 jayair

This was the first chapter that gave me a problem - saying "unexpected token" on the import statements in Routes.js.

chrisbro avatar Jun 03 '17 18:06 chrisbro

@chrisbro We use the dynamic import() in this chapter and that's available in the new Create React App. Can I see your package.json?

jayair avatar Jun 05 '17 00:06 jayair

@jayair Sure thing. Here you go! Of note: I started creating this with node 4.8.3, and during troubleshooting this realized how far behind that was and upgraded to 8.0.0 with the same behavior. I suspect that the detection of an old version of node gave me an old version of something here? Some dependencies stripped out to simplify things.

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.5.4",
    "react-bootstrap": "^0.31.0",
    "react-router-dom": "^4.1.1",
    "react-textarea-autosize": "^5.0.6"
  },
  "devDependencies": {
    "react-addons-test-utils": "^15.5.1",
    "react-dom": "^15.5.4",
    "react-scripts": "0.9.5",
    "react-test-renderer": "^15.5.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "predeploy": "npm run build",
    "eject": "react-scripts eject"
  }
}

chrisbro avatar Jun 05 '17 23:06 chrisbro

@chrisbro Yeah the Create React App's react-scripts is out of date for you. Here is the one that we have in the tutorial - https://github.com/AnomalyInnovations/serverless-stack-demo-client/blob/code-splitting-in-create-react-app/package.json#L14

Upgrading is pretty simple, here are the instructions - https://github.com/facebookincubator/create-react-app/releases/tag/v1.0.0

jayair avatar Jun 06 '17 10:06 jayair

@jayair Upgraded and confirmed that was the problem. Thank you!

chrisbro avatar Jun 06 '17 23:06 chrisbro

Can you please help me understand? Why after splitting the code the main.js size decreased to 1.2kb and 0.chunk.js size increased to 21kb?

Before: screen shot 2017-06-13 at 12 21 08 pm

after: screen shot 2017-06-13 at 12 21 27 pm

tlg01 avatar Jun 13 '17 06:06 tlg01

@tmedetbekov I'm not sure what your code is like, so I can't give you an exact answer for your case. But AFAIK when you are code splitting, each chunk is bundled with it's dependencies. So it is possible that chunk 0 is including a dependency that it needs and maybe that dependency is also needed in the main chunk. Hence, the size of main didn't reduce all that much.

jayair avatar Jun 13 '17 17:06 jayair

seems these lines are faulty:

const AsyncHome = Loadable({
  loader: () => import('./containers/Home'),
  LoadingComponent: MyLoadingComponent
});

LoadingComponent:
shouldnt that be

loading:

origicom avatar Aug 12 '17 11:08 origicom

@origicom Yeah it looks like react-loadable has been updated.

Just made the change - https://github.com/AnomalyInnovations/serverless-stack-com/commit/02b273342cad736f43ba3349dfda2baa11007589. Thanks!

jayair avatar Aug 12 '17 21:08 jayair