Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add cached navigation for better performance

thecodrr68e769d6c510
diff --git a/apps/web/src/app.css b/apps/web/src/app.css
index a227a89bb..cc8ce7f6d 100644
--- a/apps/web/src/app.css
+++ b/apps/web/src/app.css
@@ -16,3 +16,10 @@
padding: 0px 10px !important;
background-color: var(--background) !important;
}
+
+.route {
+ display: flex;
+ flex-direction: column;
+ flex: 1;
+ opacity: 1;
+}
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index a2a59cd05..7ec6b1503 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -153,7 +153,7 @@ function App() {
}}
>
{isMobile && <Banner />}
- <Routes />
+ <CachedRouter />
</Animated.Flex>
<Flex
width={[show ? 0 : "100%", show ? 0 : "100%", "100%"]}
@@ -171,9 +171,34 @@ function App() {
);
}
-function Routes() {
- const routeResult = useRoutes(routes, { fallbackRoute: "/" });
- return routeResult || null;
+const cache = {};
+function CachedRouter() {
+ const RouteResult = useRoutes(routes, { fallbackRoute: "/" });
+ useEffect(() => {
+ const key = RouteResult.key || "general";
+ const routeContainer = document.getElementById("routeContainer");
+ routeContainer.childNodes.forEach((node) => {
+ node.style.display = "none";
+ });
+
+ var route = document.getElementById(key);
+ if (route) {
+ route.style.display = "flex";
+ if (key !== "general") return;
+ }
+
+ if (!cache[key] || !route) {
+ if (!route) {
+ cache[key] = true;
+ route = document.createElement("div");
+ route.id = key;
+ route.className = "route";
+ routeContainer.appendChild(route);
+ }
+ ReactDOM.render(<ThemeProvider>{RouteResult}</ThemeProvider>, route);
+ }
+ }, [RouteResult]);
+ return <div style={{ display: "flex", flex: 1 }} id="routeContainer" />;
}
function EditorSwitch() {
diff --git a/apps/web/src/navigation/routes.js b/apps/web/src/navigation/routes.js
index 9102ed9b6..b447121fe 100644
--- a/apps/web/src/navigation/routes.js
+++ b/apps/web/src/navigation/routes.js
@@ -14,9 +14,16 @@ import { navigate } from "../navigation";
import Trash from "../views/trash";
const routes = {
- "/": () => <RouteContainer type="notes" title="Notes" route={<Home />} />,
+ "/": () => (
+ <RouteContainer key="home" type="notes" title="Notes" route={<Home />} />
+ ),
"/notebooks": () => (
- <RouteContainer type="notebooks" title="Notebooks" route={<Notebooks />} />
+ <RouteContainer
+ key="notebooks"
+ type="notebooks"
+ title="Notebooks"
+ route={<Notebooks />}
+ />
),
"/notebooks/:notebook": ({ notebook }) => {
const nbItem = db.notebooks.notebook(notebook);
@@ -54,6 +61,7 @@ const routes = {
},
"/favorites": () => (
<RouteContainer
+ key="favorites"
title="Favorites"
type="favorites"
route={
@@ -65,9 +73,11 @@ const routes = {
/>
),
"/trash": () => (
- <RouteContainer type="trash" title="Trash" route={<Trash />} />
+ <RouteContainer key="trash" type="trash" title="Trash" route={<Trash />} />
+ ),
+ "/tags": () => (
+ <RouteContainer key="tags" title="Tags" type="tags" route={<Tags />} />
),
- "/tags": () => <RouteContainer title="Tags" type="tags" route={<Tags />} />,
"/tags/:tag": ({ tag }) => {
const tagItem = db.tags.tag(tag);
if (!tagItem) return navigate("/");
@@ -96,10 +106,16 @@ const routes = {
);
},
"/settings": () => (
- <RouteContainer noSearch title="Settings" route={<Settings />} />
+ <RouteContainer
+ key="settings"
+ noSearch
+ title="Settings"
+ route={<Settings />}
+ />
),
"/search": () => (
<RouteContainer
+ key="search"
type="search"
canGoBack
noSearch
diff --git a/apps/web/src/views/home.js b/apps/web/src/views/home.js
index ec21a3a04..fcf1e48d5 100644
--- a/apps/web/src/views/home.js
+++ b/apps/web/src/views/home.js
@@ -4,13 +4,20 @@ import ListContainer from "../components/list-container";
import NotesPlaceholder from "../components/placeholders/notesplacholder";
import { db } from "../common";
import { hashNavigate } from "../navigation";
+import { Profiler } from "react";
function Home() {
+ const notes = useStore((store) => store.notes);
const [isLoading, setIsLoading] = useState(true);
+
useEffect(() => {
(async function () {
- await db.notes.init();
- store.refresh();
+ const intitialized = db.notes.initialized;
+ if (!intitialized) {
+ await db.notes.init();
+ store.refresh();
+ setIsLoading(false);
+ }
// const note = db.notes.note("f90f344ee3c13c2f686bd5c1").data;
// const data = await db.content.raw(note.contentId);
@@ -22,10 +29,8 @@ function Home() {
// await db.content.add(data3);
// await db.notes.add({ id: note.id, conflicted: true, resolved: false });
// console.log(data3);
- setIsLoading(false);
})();
}, []);
- const notes = useStore((store) => store.notes);
return (
<ListContainer
@@ -40,4 +45,4 @@ function Home() {
/>
);
}
-export default Home;
+export default React.memo(Home, () => true);
diff --git a/apps/web/src/views/notes.js b/apps/web/src/views/notes.js
index 9f1bdf267..b42293f09 100644
--- a/apps/web/src/views/notes.js
+++ b/apps/web/src/views/notes.js
@@ -26,7 +26,7 @@ function Notes(props) {
<ListContainer
type="notes"
isLoading={isLoading}
- context={props.context}
+ context={context}
items={context.notes}
placeholder={props.placeholder || NotesPlaceholder}
button={{