Remix Client Cache 项目教程

Remix Client Cache 项目教程

remix-client-cache Utility library to cache your client data in Remix.run 项目地址: https://gitcode.com/gh_mirrors/re/remix-client-cache

1. 项目介绍

remix-client-cache 是一个为 Remix 框架设计的实用库,旨在客户端缓存服务器加载的数据。它默认使用 stale while revalidate 策略,即在数据从服务器重新加载时,先返回缓存中的旧数据,然后再更新缓存。该库还支持自定义缓存适配器,如 localStoragesessionStoragelocalforage,并且允许用户为特定键或多个键失效缓存。

2. 项目快速启动

安装

首先,通过 npm 安装 remix-client-cache

npm install remix-client-cache

基本使用

以下是一个使用 remix-client-cache 的基本示例,使用默认的内存适配器:

import { json, type LoaderFunctionArgs } from "@remix-run/node";
import { ClientLoaderFunctionArgs } from "@remix-run/react";
import { cacheClientLoader, useCachedLoaderData } from "remix-client-cache";

export const loader = async ({ params }: LoaderFunctionArgs) => {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users/${params.user}`);
  const user = await response.json();
  await new Promise((resolve) => setTimeout(resolve, 1000));
  return json({
    user: {
      ...user,
      description: Math.random(),
    },
  });
};

// 在客户端缓存加载器数据
export const clientLoader = (args: ClientLoaderFunctionArgs) => cacheClientLoader(args);

// 确保开启客户端加载器的水合功能
clientLoader.hydrate = true;

export default function Index() {
  // 数据会自动缓存,并在重新获取时热交换
  const [user] = useCachedLoaderData<typeof loader>();
  return (
    <div>
      {user.name} <hr />
      {user.email} <hr />
      {user.username} <hr />
      {user.website} <hr />
      {user.description}
    </div>
  );
}

3. 应用案例和最佳实践

使用自定义缓存适配器

在某些情况下,您可能希望使用自定义缓存适配器,例如使用 localStoragesessionStorage。以下是如何配置全局缓存适配器的示例:

import { RemixBrowser } from "@remix-run/react";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { configureGlobalCache } from "remix-client-cache";

// 使用 localStorage 作为缓存适配器
configureGlobalCache(() => localStorage);

startTransition(() => {
  hydrateRoot(document, (
    <StrictMode>
      <RemixBrowser />
    </StrictMode>
  ));
});

使用不同的缓存策略

remix-client-cache 支持不同的缓存策略,例如 staleWhileRevalidatenormal。您可以通过 cacheClientLoader 的第二个参数来指定缓存策略:

export const clientLoader = (args: ClientLoaderFunctionArgs) =>
  cacheClientLoader(args, {
    type: "staleWhileRevalidate", // 或 "normal"
    key: "/user/1", // 自定义缓存键
  });

4. 典型生态项目

Remix 框架

remix-client-cache 是专为 Remix 框架设计的,因此与 Remix 框架紧密集成。Remix 是一个现代的全栈 Web 框架,专注于性能和开发者体验。

React

由于 Remix 基于 React,remix-client-cache 也与 React 生态系统兼容。您可以使用 React 的钩子和组件来进一步增强您的应用。

TypeScript

remix-client-cache 完全支持 TypeScript,提供了类型安全的 API,使您能够更轻松地编写类型安全的代码。

通过以上模块的介绍,您应该能够快速上手并使用 remix-client-cache 来优化您的 Remix 应用的性能。

remix-client-cache Utility library to cache your client data in Remix.run 项目地址: https://gitcode.com/gh_mirrors/re/remix-client-cache

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任翊昆Mary

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值