Suppose you have one of those React apps that lazy-load some chunk. It just basically means it injects a .js static asset URL into the DOM and once it's downloaded by the browser, it carries on the React rendering with the new code loaded. Well, what if the network is really slow? In local development, it can be hard to simulate this. You can mess with the browser's Devtools to try to slow down the network, but even that can be too fast sometimes.

What I often do is, I take this:


const SettingsApp = React.lazy(() => import("./app"));

...and change it to this:


const SettingsApp = React.lazy(() =>
  import("./app").then((module) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(module as any);
      }, 10000);
    });
  })
);

Now, it won't load that JS chunk until 10 seconds later. Only temporarily, in local development.

I know it's admittedly just a hack but it's nifty. Just don't forget to undo it when you're done simulating your snail-speed web app.

PS. That resolve(module as any); is for TypeScript. You can just change that to resolve(module); if it's regular JavaScript.

Comments

Your email will never ever be published.

Previous:
Umlauts (non-ascii characters) with git on macOS March 22, 2021 Python, macOS
Next:
My contribution to 2021 Earth Day: optimizing some bad favicons on MDN Web Docs April 23, 2021 Web development, MDN
Related by category:
How to SSG a Vite SPA April 26, 2025 JavaScript, React
Switching from Next.js to Vite + wouter July 28, 2023 JavaScript, React
An ideal pattern to combine React Router with TanStack Query November 18, 2024 JavaScript, React
get in JavaScript is the same as property in Python February 13, 2025 JavaScript
Related by keyword:
Add a lazy getter, that is a function call, on a object in JavaScript August 28, 2024 JavaScript
React 16.6 with Suspense and lazy loading components with react-router-dom October 26, 2018 Web development, React, JavaScript
Using lazy loading images on Air Mozilla April 23, 2015 Mozilla, JavaScript