diff --git a/src/components/Nvim/NvimEditor.tsx b/src/components/Nvim/NvimEditor.tsx index 75defb8..f3a6f70 100644 --- a/src/components/Nvim/NvimEditor.tsx +++ b/src/components/Nvim/NvimEditor.tsx @@ -2,6 +2,7 @@ import axios from "axios"; import { useEffect, useState } from "react"; export const NvimEditor = (props: { source: string | undefined }) => { + const [cache, setCache] = useState(new Map()); const [data, setData] = useState(); const [loading, setLoading] = useState(false); @@ -10,11 +11,28 @@ export const NvimEditor = (props: { source: string | undefined }) => { useEffect(() => { if (!props.source) return; + const cached = cache.get(props.source); + if (cached) { + console.log("cache hit"); + setData(cached); + return; + } + setLoading(true); - axios.get(props.source).then(({ data }) => { - setData(data); - setLoading(false); - }); + axios + .get(props.source) + .then(({ data }) => { + setData(data); + setLoading(false); + + setCache((cache) => { + cache.set(props.source!, data); + return cache; + }); + }) + .catch(() => { + setLoading(false); + }); }, [props.source]); let rows = data?.split("\n") ?? [];