# solid-cached-resource

> Create a solid resource attached to a cached state by a unique key

Latest version **0.8.0** (published 2026-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install solid-cached-resource
pnpm add solid-cached-resource
yarn add solid-cached-resource
bun add solid-cached-resource
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2026-03-19 |
| First published | 2022-03-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 261.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Yonatan Bendahan |
| Maintainers | yonatan.bendahan |

## Links

- npm: https://www.npmjs.com/package/solid-cached-resource
- Homepage: https://github.com/yonathan06/solid-cached-resource
- npm.io page: https://npm.io/package/solid-cached-resource

## Recent versions

- 0.8.0 (latest) — 2026-03-19
- 0.7.0 — 2026-01-21
- 0.6.2 — 2026-01-21
- 0.6.1 — 2026-01-21
- 0.6.0 — 2026-01-21
- 0.5.6 — 2024-07-25
- 0.5.5 — 2023-10-22
- 0.5.4 — 2023-10-22
- 0.5.3 — 2023-06-20
- 0.5.2 — 2023-04-17
- 0.5.1 — 2022-10-31
- 0.5.0 — 2022-10-09
- 0.4.0 — 2022-08-24
- 0.3.0 — 2022-07-13
- 0.2.3 — 2022-04-14
- … 4 more at https://npm.io/package/solid-cached-resource/versions

## README

![NPM Version](https://img.shields.io/npm/v/solid-cached-resource) ![npm bundle size](https://img.shields.io/bundlephobia/min/solid-cached-resource) ![NPM License](https://img.shields.io/npm/l/solid-cached-resource)

# Solid Cached Resource

Inspired by TanStack Query, with minimal API and footprint, built only for SolidJS.
The (almost) same API as [createResource](https://www.solidjs.com/docs/latest/api#createresource).
Includes `createMutation` for easier mutation state handling.

[API references](https://yonathan06.github.io/solid-cached-resource/)

Features:

- Create resource with the same key in multiple places - fetch once
- Cache results for next component mount, and refresh when wanted
- Mutate local resource by key after a successful remote mutation request

## install

```sh
pnpm add solid-cached-resource
```

or `npm`/`yarn`

## createCachedResource

Inspired by [useQuery](https://react-query.tanstack.com/guides/queries) just for Solid `createResource`

```TypeScript
import { createCachedResource } from "solid-cached-resource";

export const createGetUserById = (userId: Accessor<string>) => {
  return createCachedResource(
    () => ["user", userId()],
    async ([, userId]) => {
      const response = await fetch(`/users/${userId}`);
      return response.json();
    });
}

// MyComp.tsx
const [user] = createGetUserById(() => props.userId);

<div>{user().name}</div>

// MyOtherComp.tsx
const [user] = createGetUserById(() => props.userId);

<span>{user().name}</span>
```

In the case above, if `props.userId` has the same value, the key will be the same, so even though both components are creating the same resource with the same fetcher, only one request will be made to the server.

### With options

`createCachedResource` accepts an optional [options](https://yonathan06.github.io/solid-cached-resource/interfaces/CachedResourceOptions.html) object as its third argument

```TypeScript
{
  initialValue?: T (default undefined)
  refetchOnMount?: boolean (default true)
}
```

## createMutations

Inspired by [useMutation](https://react-query.tanstack.com/guides/mutations), with onSuccess hook, and `mutateCachedValue` utility function.

```TypeScript
import {
  mutateCachedValue,
  createMutation,
} from "solid-cached-resource";

export const createUpdateUser = (userId: Accessor<string>) => {
  return createMutation(async (values) => {
    const response = fetch(`user/${userId()}`, {
      method: "POST",
      body: values,
    });
    return await response.json()
  }, {
    onSuccess: (user) => {
      mutateCachedValue(() => ["user", userId()], user);
    }
  });
}
```

`mutateCachedValue` will call the resources' `mutate` function with the provided key, so the signals will be updated across your components.

---
_Source: https://npm.io/package/solid-cached-resource · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
