Skip to content

Commit

Permalink
Fait un rechargement complet de la page en cas d'erreur de chargement…
Browse files Browse the repository at this point in the history
… dynamique d'un composant
  • Loading branch information
marc-rutkowski committed Nov 7, 2023
1 parent 5a2475b commit 743acad
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';
import {Spacer} from 'ui/dividers/Spacer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';
import {Spacer} from 'ui/dividers/Spacer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';
import {Spacer} from 'ui/dividers/Spacer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Profil = lazy(() => import('app/pages/Profil/Profil'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const ToutesLesCollectivites = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Accueil = lazy(() => import('app/pages/collectivite/Accueil/Accueil'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const BibliothequeDocs = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const JournalActivite = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Indicateurs = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const ParcoursLabellisation = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const PersoReferentiel = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const PersoReferentielThematique = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const FicheAction = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const CreerPlan = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const ImporterPlan = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Selection = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const PlanAction = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const PlansActions = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Synthese = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Vue = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {useParams} from 'react-router-dom';
import {renderLoader} from 'utils/renderLoader';
import {Referentiel} from 'types/litterals';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const ReferentielTabs = lazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';

const Membres = lazy(() => import('app/pages/collectivite/Users/Membres'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {lazy, Suspense} from 'react';
import {Suspense} from 'react';
import {lazy} from 'utils/lazy';
import {renderLoader} from 'utils/renderLoader';
import {TActionPreuvePanelProps} from './ActionPreuvePanel';

Expand Down
35 changes: 35 additions & 0 deletions app.territoiresentransitions.react/src/utils/lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {ComponentType, lazy as originalLazy} from 'react';

type ImportComponent = () => Promise<{default: ComponentType<any>}>;

const FORCE_REFRESH = 'force-refresh';

// Encapsule la fonction `React.lazy` pour faire un rechargement complet de la
// page en cas d'erreur de chargement dynamique d'un composant, et ce afin
// d'éviter les `ChunkLoadError` lors des mises à jour du front.
// Ref: https://gist.github.com/raphael-leger/4d703dea6c845788ff9eb36142374bdb#file-lazywithretry-js
export const lazy = (importComponent: ImportComponent) =>
originalLazy((async () => {
// vérifie si on a déjà forcé le rechargement complet
const isPageHasBeenForceRefreshed = JSON.parse(
localStorage.getItem(FORCE_REFRESH) || 'false'
);

try {
// essaye de charger le composant
const component = await importComponent();

localStorage.setItem(FORCE_REFRESH, 'false');

return component;
} catch (error) {
// en cas d'erreur on essaye de faire un rechargement complet de la page
// si ça n'a pas déjà été tenté
if (!isPageHasBeenForceRefreshed) {
localStorage.setItem(FORCE_REFRESH, 'true');
return document.location.reload();
}

throw error;
}
}) as ImportComponent);

0 comments on commit 743acad

Please sign in to comment.