Support caching for streamed responses
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 30/100
- Issue-Typ
- Bug
- Klarheit
- Muss geklärt werden
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- react, typescript
- Bereich
- frontend, performance
Rechercherichtung
Beginne mit src/index.tsx Zeilen 116-140 und bilde den Pfad für ein erfülltes Promise aus dem verlinkten StackBlitz/routes/index.tsx-Beispiel nach. Verfolge, wie deferredServerData, serverData, freshData und adapter verglichen werden; abgeschlossen bedeutet, dass der Cache erfüllte gecachte Daten von Daten unterscheidet, die eine Revalidierung erfordern, und dieses Verhalten verifiziert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
When using defer to stream a response from a loader that returned a promise which later used in a boundary, we cannot properly serialize the promise to store say in localStorage or IndexedDB (JSON.stringify will result in an empty object {}). The only way to store this type of data is directly in memory, so on the next navigation, clientLoader will return fulfilled promise with the data, rather than calling the server `loader'.
let cache: SerializeFrom<typeof loader>;
export const clientLoader = defineClientLoader(async ({ serverLoader }) => {
if (!cache) {
cache = await serverLoader<typeof loader>();
}
return cache;
});
clientLoader.hydrate = true;
By using remix-client-cache, we can create an adapter for when we need to cache streaming responses, rather than relying on a global setting using, for example, localStorage.
import { lru } from 'tiny-lru';
import { type CacheAdapter, createCacheAdapter } from 'remix-client-cache';
const cache = lru(100);
class LRUAdapter implements CacheAdapter {
async getItem(key: string) {
return cache.get(key);
}
async setItem(key: string, value: any) {
return cache.set(key, value);
}
async removeItem(key: string) {
return cache.delete(key);
}
}
export const { adapter: lruAdapter } = createCacheAdapter(() => new LRUAdapter());
routes/index.tsx:
import { defer } from '@remix-run/node';
import { Await, ClientLoaderFunctionArgs, Link } from '@remix-run/react';
import { Suspense } from 'react';
import { cacheClientLoader, useCachedLoaderData } from 'remix-client-cache';
import { lruAdapter } from '~/client-cache-adapter';
async function getQueryData() {
await new Promise((resolve) => setTimeout(resolve, 3000));
return { data: [{ id: 1 }] };
}
export async function loader() {
const query = getQueryData();
return defer({
query,
});
}
export const clientLoader = (args: ClientLoaderFunctionArgs) => cacheClientLoader(args, { adapter: lruAdapter });
clientLoader.hydrate = true;
export default function Page() {
const { query } = useCachedLoaderData<typeof loader>();
return (
<>
<Link to="/">Home</Link>
<br />
<br />
<Suspense fallback="loading...">
<Await resolve={query}>
{({ data }) => (
<ul>
{data?.map((entry) => <li key={entry.id}>{entry.id}</li>)}
</ul>
)}
</Await>
</Suspense>
</>
);
}
This opens up a question whether it's a good idea to store data in server memory, it would be interesting if we could transform fulfilled promises using a library like turbo-stream to store on the client-side or use a web worker and decode to the original form for consumption by .
There is currently a bug where a returned promise from the cache has already been fulfilled, the internal logic of remix-client-cache cannot understand when a revalidation should occur, or fulfilled data is currently present, since we must store promises directly in memory and not as a string.
https://github.com/forge42dev/remix-client-cache/blob/main/src/index.tsx#L116-L140
// Unpack deferred data from the server
useEffect(() => {
let isMounted = true;
if (loaderData.deferredServerData) {
loaderData.deferredServerData.then((newData: any) => {
if (isMounted) {
adapter.setItem(loaderData.key, newData);
setFreshData(newData);
}
});
}
return () => {
isMounted = false;
};
}, [loaderData, adapter]);
// Update the cache if the data changes
useEffect(() => {
if (
loaderData.serverData &&
JSON.stringify(loaderData.serverData) !== JSON.stringify(freshData)
) {
setFreshData(loaderData.serverData);
}
}, [loaderData?.serverData, freshData]);
- Vorherrschende Sprache
- TypeScript
- Sterne
- 247
- Forks
- 3
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Kein Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus code-forge-io/remix-client-cache
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 48/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
code-forge-io/remix-client-cache#15 · 3 Kommentare · 3 Reaktionen ·
-
Idea: refetching statusOffen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
code-forge-io/remix-client-cache#3 · 3 Kommentare ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
code-forge-io/remix-client-cache#2 · 3 Kommentare ·
Alle Issues in code-forge-io/remix-client-cache
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
langchain-ai/langchainjs#12239 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Board view: the Monitoring column is always empty because compact issue rows drop displayStatusOffen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
paperclipai/paperclip#21837 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 73/100
code-yeongyu/senpi#6813 ·
Maintainer antworten meist innerhalb von 1 Tag
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 93/100
lingdojo/kana-dojo#34169 · 1 Kommentar · 5 Reaktionen ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100