From 28f7dec7e98a9065568b88a226bedcecad3a8250 Mon Sep 17 00:00:00 2001
From: Galen Guyer <galen@galenguyer.com>
Date: Wed, 13 Apr 2022 22:43:46 -0400
Subject: add lazy loading

---
 src/App.jsx | 79 +++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 48 insertions(+), 31 deletions(-)

(limited to 'src/App.jsx')

diff --git a/src/App.jsx b/src/App.jsx
index 42c3f36..5afcb03 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1,10 +1,10 @@
 import { useLocation, Routes, Route, Link } from "react-router-dom";
-import { useState } from "react";
+import { useState, lazy, Suspense } from "react";
 import useFetch from "./useFetch";
-import Index from "./pages/Index";
 import { DateTime } from "luxon";
 import "./App.css";
-import Graph from "./pages/Graph";
+const Index = lazy(() => import("./pages/Index"));
+const Graph = lazy(() => import("./pages/Graph"));
 import { useEffect } from "react";
 
 const App = () => {
@@ -12,9 +12,10 @@ const App = () => {
 
     let routerLocation = useLocation();
     useEffect(() => {
-        !window.goatcounter ?? window.goatcounter.count({
-            path: location.pathname + location.search + location.hash,
-        });
+        !window.goatcounter ??
+            window.goatcounter.count({
+                path: location.pathname + location.search + location.hash,
+            });
     }, [routerLocation]);
 
     const response = useFetch(url);
@@ -47,48 +48,64 @@ const App = () => {
                 <Route
                     path="/totalstudents"
                     element={
-                        <Graph
-                            name={"Total Student Cases"}
-                            response={response}
-                            dataKey={"total_students"}
-                            timeDifference={timeDifference}
-                        />
+                        <Suspense fallback={null}>
+                            <Graph
+                                name={"Total Student Cases"}
+                                response={response}
+                                dataKey={"total_students"}
+                                timeDifference={timeDifference}
+                            />
+                        </Suspense>
                     }
                 ></Route>
                 <Route
                     path="/totalstaff"
                     element={
-                        <Graph
-                            name={"Total Staff Cases"}
-                            response={response}
-                            dataKey={"total_staff"}
-                            timeDifference={timeDifference}
-                        />
+                        <Suspense fallback={null}>
+                            <Graph
+                                name={"Total Staff Cases"}
+                                response={response}
+                                dataKey={"total_staff"}
+                                timeDifference={timeDifference}
+                            />
+                        </Suspense>
                     }
                 ></Route>
                 <Route
                     path="/newstudents"
                     element={
-                        <Graph
-                            name={"New Student Cases"}
-                            response={response}
-                            dataKey={"new_students"}
-                            timeDifference={timeDifference}
-                        />
+                        <Suspense fallback={null}>
+                            <Graph
+                                name={"New Student Cases"}
+                                response={response}
+                                dataKey={"new_students"}
+                                timeDifference={timeDifference}
+                            />
+                        </Suspense>
                     }
                 ></Route>
                 <Route
                     path="/newstaff"
                     element={
-                        <Graph
-                            name={"New Staff Cases"}
-                            response={response}
-                            dataKey={"new_staff"}
-                            timeDifference={timeDifference}
-                        />
+                        <Suspense fallback={null}>
+                            <Graph
+                                name={"New Staff Cases"}
+                                response={response}
+                                dataKey={"new_staff"}
+                                timeDifference={timeDifference}
+                            />
+                        </Suspense>
                     }
                 ></Route>
-                <Route exact path="/" element={<Index response={response} timeDifference={timeDifference} />} />
+                <Route
+                    exact
+                    path="/"
+                    element={
+                        <Suspense fallback={null}>
+                            <Index response={response} timeDifference={timeDifference} />
+                        </Suspense>
+                    }
+                />
             </Routes>
             <footer>
                 <p>
-- 
cgit v1.2.3

