Skip to main content

Recipe

This recipe shows how to use persistentStore (TypeScript) or persistent_store (Python) to persist data across executions of the same project.
TypeScript
import { BrowserContext, Page } from "playwright-core";
import { persistentStore } from "@intuned/runtime";

interface Params {
  // Add your params here
}

export default async function handler(
  params: Params,
  page: Page,
  context: BrowserContext
) {
  const USERS_CACHE_KEY = "users_cache";

  // 1. Try to read from cache
  const cachedUsers = await persistentStore.get(USERS_CACHE_KEY);

  if (cachedUsers) {
    console.log("Loaded users from cache");
    return cachedUsers;
  }

  // 2. Cache miss - fetch from API
  console.log("Fetching users from API");
  const response = await fetch("https://jsonplaceholder.typicode.com/users");
  const users = await response.json();

  // 3. Store result for future runs
  await persistentStore.set(USERS_CACHE_KEY, users);

  return users;
}

How it works

  1. Check the cache - Use persistentStore.get() / persistent_store.get() to look for existing data
  2. Return cached data - If found, return immediately without making external requests
  3. Fetch and store - On cache miss, fetch data and store it with persistentStore.set() / persistent_store.set() for future runs
Data persists across all executions within the same project, making it ideal for caching API responses, configuration, or any data that doesn’t change frequently.