Why React PWA refetch lazy load chunks based on routes when offline

Viewed 797

I am trying to do PWA with CRA and SWR for data fetching, so I did

serviceWorker.register();

then created minifist.json file.

and I did lazy loaded some components based on the routes. so When I tried to test the offline flow on the browser by changing the url through Reach Router Link while opening the network tab, cause the app to re-fetch the component's chunk which failed - cuz offline status -, therefore causing the Error Boundary to trigger an error.

hint : I did visit the page, before becoming offline so the chunk should have been fetched.

secondly, how could I add a custom offline page with CRA and PWA?

Update: Here's the code for handling the error cases.

export const dev = axios.create({ ...config.dev });
// useSWR fetcher function.
export const fetcher = (url: string) =>
  dev
    .get(url)
    .then((res) => res.data.data)
    .catch(
      (err) => err?.response?.data?.message || err.message || err?.data?.message
    );

axiosRetry(dev, {
  retries: 3,
  retryDelay: (retryCount) => {
    return retryCount * 1500;
  },
});

I did add the fetcher function globally at the top level

   <SWRConfig
        value={{
          fetcher,
        }}
      >
    ...
  </SWRConfig>

and within the component that using useSWR,

 const { data: actions, error }: any = useSWR(url);

  if (error)
    return (
      <h3 className="title">
        Opps, {error} please try again later
      </h3>
    );
1 Answers

As discussed, the issue is occurring maybe due to some code logic in one of your main component and not related to lazy loading at all. If you check your console then you will see below error. This error is causing the application to fail.

TypeError: b.map is not a function
    at Auth App Page.180ff7f0.chunk.js?__WB_REVISION__=cdce21b6ef032b90bc09:1
    at Ko (react-dom.production.min.js:153)
    at Ma (react-dom.production.min.js:175)
    at ms (react-dom.production.min.js:263)
    at cu (react-dom.production.min.js:246)
    at su (react-dom.production.min.js:246)
    at Zs (react-dom.production.min.js:239)
    at react-dom.production.min.js:123
    at scheduler.production.min.js:19
    at Hi (react-dom.production.min.js:122)

Once you fix this issue your application will work as expected.

Related