1

Im trying to lazy load routes but no matter what I do, I only have one main chunk. Here is the route code.

import React, { Suspense, lazy } from "react";
import {
HomePage,
ProductsPage,
SingleProductsPage,
CartPage,
FAQPage,
SuccessPage,
FailedPage,
DigitalPage,
ReviewPage,
Page404,
PrivacyPolicyPage,
TermsConditionsPage,
} from "./pages";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

const ProductsPageLazy = lazy(() =>
import("./pages/ProductsPage/ProductsPage")
);
const SingleProductPageLazy = lazy(() =>
import("./pages/SingleProductPage/SingleProductsPage")
);
const CartPageLazy = lazy(() => import("./pages/CartPage/CartPage"));
const FAQPageLazy = lazy(() => import("./pages/FAQPage/FAQPage"));
const SuccessPageLazy = lazy(() =>
import("./pages/SuccessFailedPage/SuccessPage")
);
const FailedPageLazy = lazy(() =>
import("./pages/SuccessFailedPage/FailedPage")
);
const ReviewPageLazy = lazy(() => import("./pages/ReviewPage/ReviewPage"));

const App = () => {
  return (
    <Router>
      <Routes>
        <Route path="/" exact element={<HomePage />} />
      </Routes>
      <Suspense fallback={<div>loading...</div>}>
        <Routes>
          <Route path="/products" exact element={<ProductsPageLazy />} />
          <Route path="/products/:id" element={<SingleProductPageLazy />} />
          <Route path="/cart" element={<CartPageLazy />} />
          <Route path="/faqs" element={<FAQPageLazy />} />
          <Route path="/success/:id" exact element={<SuccessPageLazy />} />
          <Route path="/review/:id" exact element={<ReviewPageLazy />} />
          <Route path="/failed" exact element={<FailedPageLazy />} />
        </Routes>
      </Suspense>
      <Routes>
        <Route path="/digital" exact element={<DigitalPage />} />
        <Route path="/privacypolicy" exact element={<PrivacyPolicyPage />} />
        <Route path="/terms" exact element={<TermsConditionsPage />} />
        <Route path="*" element={<Page404 />} />
      </Routes>
    </Router>
  );
};

export default App;

If I wrap the Route with suspense i get blank page. I can only use suspense two way. One inside the element and 2nd is wraping the Routes. In both case I get only one main chunk and its not spliting.

enter image description here

What am I missing. Please help.

5
  • Are you using webpack to build? If so can you provide a webpack configuration? Commented Oct 3, 2022 at 7:23
  • I don't have webpack configuration file. I thought with create-react-app you don't need it? Commented Oct 4, 2022 at 2:08
  • You don't need it with create-react-app. Nevertheless react-scripts use webpack underneath. I'll try to reproduce your issue, but it seems kind of unlikely. Commented Oct 4, 2022 at 13:53
  • Is there anything else that needs to be done before using suspense and lazy? What if I create a new app with create-react-app and try to transfer my project on the new app? Commented Oct 4, 2022 at 15:27
  • Also Im using react context which is wrapping the whole app. Do you think this could be interfering with react lazy. Commented Oct 5, 2022 at 17:35

0

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.