npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sveltekit-cloudflare-isr

v1.0.7

Published

Similar to what next-13 does with its overload of globalThis.fetch, this hooks make available an ISR fetcher under your app.locals.

Downloads

7

Readme

sveltekit-cloudflare-isr

Similar to what next-13 does with its overload of globalThis.fetch, this hooks make available an ISR fetcher under your app.locals.

Rather than performing a page caching, it implements an ISR mechanism at the request level.

This library assumes that your are using the cloudflare page / worker adapter.

I don't really know if we can call it an isr library stricto sensu because we need a way to populate / refresh the cache at build time. But I think it's pretty close to the experience we had with next.js and it's still pretty fast.

  • Request level caching
  • Avoid cache / force refresh / duration request configuration
  • Programmatic revalidation
  • Programmatic cache avoidance global configuration (useful for preview mode for example).
  • Use cloudflare worker cache api by default but you can also use a KV store to replicate the cache at the edge.

How it works

It first look up in the cache of the data center of the request. If a match is found it will return it. If no match is found and you have a KV namespace configured it will look up in the KV store and return the eventual result. If no KV match, it will fetch the request and return the response.

If any of the following condition are truthy it will revalidate the cache (and KV f configured):

  • No match is found in the cache
  • The match in the cache is Expired
  • The shouldRevalidate function return true
  • The forceRevalidate request option is set to true

If any of the following conditions are truthy it will avoid the cache

  • No cache is found in the context
  • The shouldAvoidCache function return true
  • The avoidCache request option is set to true

The revalidation process uses the waitUntil cloudflare function in order to be executed in the background and not to block the main response to be delivered.

If the request need to be revalidated, the function will always return the stale response unless the swr option is set to false at the request level.

schema

How to use

The fetcher

Use the fetcher as a normal fetch function, but with a last argument which will tell the isr config.

interface ISRConfig {
  duration: duration;
  forceRefresh?: boolean;
  avoidCache?: boolean;
  swr?: boolean;
}
type duration = `${number} ${"second" | "minute" | "hour" | "day" | "week" | "month" | "year"}${"s" | ""}` | number;
// routes/+page.server.ts
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ locals }) => {
  const posts = await locals.fetch("https://api.example.com/posts", { duration: "1 day" });

  return {
    posts
  };
};

The hook

// src/hooks.server.ts
// Simple example
import { Handle } from "@sveltejs/kit";
import { isr } from "sveltekit-cloudflare-isr";

export const handle: Handle = isr({ key: "fetch" });
// src/hooks.server.ts
// Complex example
import { Handle } from "@sveltejs/kit";
import { isr } from "sveltekit-cloudflare-isr";

export const handle: Handle = isr({
  key: "fetch",
  longTermCacheDuration: "1 year",
  longTermKVDuration: "1 year",
  KVNamespace: "CACHE",
  shouldRefreshCache: ({ event }) => {
    return event.request.headers.has("refresh-cache");
  },
  shouldAvoidCache: ({ event }) => {
    return event.cookies.get("DATA-PREVIEW") === "true";
  },
  cacheName: "default"
});