Support caching for streamed responses
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 30/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- react, typescript
- 领域
- frontend, performance
调研方向
从 src/index.tsx 第 116-140 行开始,并复现链接的 StackBlitz/routes/index.tsx 示例中的 fulfilled-promise path。跟踪 deferredServerData、serverData、freshData 和 adapter 的比较方式;完成意味着缓存能够区分 Promise 已 fulfilled 的缓存数据与需要重新验证的数据,并且行为已得到验证。
由索引模型根据 Issue 内容生成。
描述
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]);
- 主要语言
- TypeScript
- 星标
- 247
- 派生
- 3
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
code-forge-io/remix-client-cache 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 48/100
-
难度 5/5 一周以上 新手友好度 35/100
-
难度 5/5 一周以上 新手友好度 25/100
code-forge-io/remix-client-cache#15 · 3 条评论 · 3 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 35/100
code-forge-io/remix-client-cache#3 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
code-forge-io/remix-client-cache#2 · 3 条评论 ·
查看 code-forge-io/remix-client-cache 的全部 Issue
相似的 Issue
-
Out of order / duplicate key error shows the key bytes as decimal or UTF-8, not hex可能已有人在做 @bardrop 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 86/100
-
`/leaderboard`, the page and the share image say "be the first" for a program that hasn't started未关闭bug good first issue
难度 2/5 1-3 小时 新手友好度 84/100
codercops/discord-bot#152 ·
维护者通常 1 天内回复
-
bug i18n
难度 2/5 1-3 小时 新手友好度 74/100
endora-commerce/endora-commerce#1315 ·
维护者通常 1 天内回复
-
area:data-quality importer size:S
难度 2/5 1-3 小时 新手友好度 72/100
enorm-labs/event-junkie#5136 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 84/100
维护者通常 1 天内回复