I have a Symfony application that uses React to build the front-end. As of our last release the build size of the react app has become too large and actually runs out of memory on the server trying to compile. I have been attempting to implement code splitting and have the initial page load to be much more efficient. I am running into a problem with the routes.
My router is defined similar to this.
import React from 'react';
import Loadable from 'react-loadable';
import DelayedLoading from 'components/DelayedLoading';
export default [
component: Loadable({loader: () => import('containers/Page'), loading: DelayedLoading}),
routes: [
{
path: '/',
component: Loadable({loader: () => import('containers/MainPage'), loading: DelayedLoading}),
},
{
path: '/foo/bar',
component: Loadable({loader: () => import('containers/FooBarPage'), loading: DelayedLoading}),
},
]
];
In my webpack config I have defined
module.exports = {
output: {
path: path.resolve(__dirname, 'web'),
filename: 'react/js/[name].js',
},
}
Any time a path is two or more deep then the chunks try to load from the wrong directory. For example, the foo/bar route above will try to load chunks at foo/react/js/___.js, but chunks loaded from the / route, or any route that is just one deep will correctly load from react/js/___.js.
How can I get all of the chunks to load from react/js?
pathproperty topath.resolve(__dirname, 'web/react/js')and thefilenameproperty to[name].js. You shouldn't be including a path in thefilenameproperty.${output.path}/${output.filename}. In my caseweb/react/js/is the directory that these assets need to be placed on the filesystem so they can be served. What this led me to was webpack always trying to load the resources directly from/{resource}.js. By add an additional propertypublicPath: '/react/js'I was able to get webpack to spit out the files to theweb/react/js/directory and serve them from/react/js/without prefixing directory paths.