Support caching for streamed responses
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 30/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend, performance
Hướng nghiên cứu
Bắt đầu với src/index.tsx, các dòng 116-140, và tái hiện fulfilled-promise path từ ví dụ StackBlitz/routes/index.tsx được liên kết. Theo dõi cách deferredServerData, serverData, freshData và adapter được so sánh; hoàn tất nghĩa là cache phân biệt dữ liệu đã cache với Promise fulfilled với dữ liệu cần revalidation, với hành vi đã được xác minh.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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]);
- Ngôn ngữ chính
- TypeScript
- Star
- 247
- Fork
- 3
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của code-forge-io/remix-client-cache
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
Usage with useRouteLoaderDataĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
code-forge-io/remix-client-cache#15 · 3 bình luận · 3 reaction ·
-
Idea: refetching statusĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
code-forge-io/remix-client-cache#3 · 3 bình luận ·
-
InvalidCache with wildcardsĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
code-forge-io/remix-client-cache#2 · 3 bình luận ·
Tất cả issue của code-forge-io/remix-client-cache
Issue tương tự
-
Out of order / duplicate key error shows the key bytes as decimal or UTF-8, not hexCó thể đã có người làm @bardrop đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
-
`/leaderboard`, the page and the share image say "be the first" for a program that hasn't startedĐang mởbug good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
codercops/discord-bot#152 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug i18n
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
endora-commerce/endora-commerce#1315 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Two stored artist rows are not act names: "DJS" from a Renate floor heading and "Stevie Cox -"Đang mởarea:data-quality importer size:S
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
enorm-labs/event-junkie#5136 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
fix(scripts): set-device-owner.ps1 fails to parse on Windows PowerShell 5.1 (UTF-8 without BOM)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày