7

I'm using react lazy for route based code splitting as described here: https://reactjs.org/docs/code-splitting.html#route-based-code-splitting

My routes file looks like this:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import React, { Suspense, lazy } from 'react';

const APage = lazy(() => import(/* webpackChunkName: "APage" */ '../pages/APage'));
const BPage = lazy(() => import(/* webpackChunkName: "BPage" */ '../pages/BPage'));

const App = () => (
  <Router>
    <Suspense fallback={<div>Loading...</div>}>
      <Switch>
        <Route exact path="/products/a" component={APage}/>
        <Route exact path="/products/b" component={BPage}/>
      </Switch>
    </Suspense>
  </Router>
);

When I compile the app it correctly generates 4 JS chunks:

app.js
APage.js
BPage.js
APage~BPage.js

however when I navigate to this path: /products/a, I get these load errors because my component files are located in the root, not in a 'products' folder:

http://localhost.com:3000/products/APage~BPage.js net::ERR_ABORTED 404 (Not Found)
http://localhost.com:3000/products/APage.js net::ERR_ABORTED 404 (Not Found)

How can I configure webpack/react to load components from the root of my site and/or from an external CDN?

1 Answer 1

6

in webpack 's module add output values like this :

    output: {
      publicPath: '/',
      path: path.join(__dirname, 'root'),
    }
Sign up to request clarification or add additional context in comments.

2 Comments

cool looks like it works for local paths and to change the domain for a CDN
I too had got stuck at this issue. Glad it was of help. 👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.