Here is my set up:
const DesktopApp = lazy(() => import(/* webpackChunkName: "DesktopApp" */'./DesktopApp'));
const MobileApp = lazy(() => import(/* webpackChunkName: "MobileApp" */'./MobileApp'));
type Props = { shouldServeMobile: boolean };
export const App = ({ shouldServeMobile }: Props): JSX.Element => (
shouldServeMobile
? (
<Suspense fallback={<AppLoading />}>
<MobileApp />
</Suspense>
) : (
<Suspense fallback={<AppLoading />}>
{/* GlobalDesktopStyle is injected in multiple places due to a bug where the
theme gets reset when lazy loading via React.Lazy + webpack */}
<GlobalDesktopStyle />
<DesktopApp />
</Suspense>
)
);
This is being loaded by a webpack-dev-server
with the following configuration:
devServer: {
contentBase: paths.output.path,
// this host value allows devices on a LAN to connect to the dev server
host: '0.0.0.0',
https: true,
port: 9001,
hotOnly: true,
// lets any URL work
historyApiFallback: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
Now, imagine that we are rendering
import { hot } from 'react-hot-loader/root';
// some imports omitted
const HotApp = hot(App);
ReactDOM.render(
<HotApp shouldServeMobile={true} />,
document.getElementById('root')
);
On initial load, this works properly. The MobileApp
chunk downloads and the DesktopApp
does not. However, as soon as I change any data within my components and the HMR
kicks in - the reload downloads the DesktopApp
chunk.
This clearly defeats the purpose of code splitting. Does anybody have any idea how to stop this from happening?
To be clear: I have outputted console.log(shouldServeMobile)
and it is always true
. Also, I tried the suggestion here: Webpack-dev-server emits all chunks after every change and it didn't help at all.
react-hot-loader/root
instead just use webpackhot
reloader. ? – Plurality