Cache Solidjs resources by key (derived from the resource source)

Overview

Solid Cached Resource (Experimental)

Create a solid resource attached to a cached state by a unique key. Heavily inspired by react-query, but for solid's createResource Works fluently with Solid, by keeping the same API as createResource, the resource source (the first function parameter signal) is being converted to a string key.

API references

Features:

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

install

pnpm add solid-cached-resource

or npm/yarn

createCachedResource

Inspired by useQuery just for Solid createResource

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

export const createGetUserById = (userId: Accessor<string>) => {
  return createCachedResource(
    () => ["user", userId()],
    async ([, userId]) => {
      const response = fetch(`/users/${userId}`);
      return await 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 event 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 and optional options object as it's third argument

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

createMutations

Inspired by useMutation, with onSuccess hook, and mutateCachedValue utility function.

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.

You might also like...

This document introduces an early implementation of the Node-RED runtime that runs on resource-constrained microcontrollers (MCUs).

Node-RED MCU Edition Copyright 2022, Moddable Tech, Inc. All rights reserved. Peter Hoddie Updated June 25, 2022 Introduction This document introduces

Jan 3, 2023

PxLoader is a simple JavasScript library for creating preloaders and resource downloaders for HTML5 apps.

PxLoader is a Javascript library that helps you download images, sound files or anything else you need before you take a specific action on your site

Dec 30, 2022

JS Cloudimage 360 View - A simple, interactive resource that can be used to provide a virtual tour of your product

JS Cloudimage 360 View - A simple, interactive resource that can be used to provide a virtual tour of your product

JS Cloudimage 360 View - A simple, interactive resource that can be used to provide a virtual tour of your product

Jan 7, 2023

This tool allows you to draw up plans for facilities from Foxhole's new Inferno update. It takes power and resource needs into account to help you efficiently design your facilities.

This tool allows you to draw up plans for facilities from Foxhole's new Inferno update. It takes power and resource needs into account to help you efficiently design your facilities.

Foxhole Facility Planner This tool allows you to draw up plans for facilities from Foxhole's new Inferno update. It takes power and resource needs int

Dec 23, 2022

open-source implementation of the Turborepo custom remote cache server.

open-source implementation of the Turborepo custom remote cache server.

This project is an open-source implementation of the Turborepo custom remote cache server. If Vercel's official cache server isn't a viable option, th

Dec 30, 2022

Source of the (Un)official Moodle Developer Resources

Moodle Developer Resources Introduction This repository includes the source for the Moodle Developer Resources - a collection of resources aimed at ma

Dec 16, 2022

Statichunt is a free open-source Jamstack directory that lists hundreds of themes, starters, and resources for Jamstack sites.

Statichunt Statichunt is an open-source directory that enlists hundreds of themes, starters, and resources for static site generators submitted by the

Dec 29, 2022

Unstyled, dev error overlays for SolidJS

Unstyled, dev error overlays for SolidJS

solid-error-overlay Unstyled, headless Error Overlay for SolidJS Install npm i solid-error-overlay yarn add solid-error-overlay pnpm add solid-error-o

Dec 29, 2022

A form management library for SolidJS that is very lightweight and simple!

solform A form management library for SolidJS that is very lightweight and simple! Why solform Very easy, fast, lightweight and powerful! It has built

Nov 15, 2022
Releases(0.2.0)
  • 0.2.0(Mar 30, 2022)

    More stable, with optional options (initialValue and refetchOnMount flag)

    Full Changelog: https://github.com/yonathan06/solid-cached-resource/commits/0.2.0

    Source code(tar.gz)
    Source code(zip)
Sanity plugin for viewing resources which reference a particular resource.

@indent-oss/sanityio-referenced-by Plugin to see which documents reference a particular document referenced-by-sanityio.mov Video Alt Text: Demonstrat

Indent 16 Nov 2, 2022
Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much more and completely Open Source.

Shiryoku Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much

Kunal Keshan 22 Dec 15, 2022
Solidex is a list of SolidJS ecosystem resources and packages.

Solidex The following is a list of articles, packages and other resources that focus on the SolidJS ecosystem. Solidex is a platform for listing and m

Solid 7 Dec 18, 2022
A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods

A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods. It provides an invalidation mechanism based both on exact string and regex.

cadienvan 10 Dec 15, 2022
Tool to sign data with a Cardano-Secret-Key // verify data with a Cardano-Public-Key // generate CIP-8 & CIP-36 data

Tool to sign data with a Cardano-Secret-Key // verify data with a Cardano-Public-Key // generate CIP-8 & CIP-36 data

Martin Lang 11 Dec 21, 2022
Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormation.

CDK Github Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormatio

Pepperize 8 Nov 25, 2022
Palaemon is an open-source developer tool for monitoring health and resource metrics of Kubernetes clusters and analyzing Out of Memory (OOMKill) errors

Palaemon ?? ?? An Electron based developer tool for Kubernetes cluster monitoring and error analysis Palaemon is a Greek, child sea-god who came to ai

OSLabs Beta 99 Dec 28, 2022
A new Node.js resource built using Gatsby.js with React.js, TypeScript, and Remark.

Nodejs.dev Nodejs.dev site built using Gatsby.js with React.js, TypeScript, SCSS, and Remark. You can find the latest Figma design protype here. ?? Ge

Node.js 2.2k Jan 5, 2023
MLPleaseHelp is a simple ML resource search engine.

README MLPleaseHelp is a simple ML resource search engine. How To Use You can use this search engine right now at https://jgreenemi.github.io/MLPlease

Joseph Greene 5 Jan 20, 2021
Utility for collecting resource-based policies from an AWS account

AWS resource-based policy collector This library aims to collect resource-based policies from an AWS account. NOTE: This library does not cover all AW

Will Dady 22 Dec 5, 2022