Skip to main content
Version: v0.3.x

pn module (v0.3.x)

pn wraps fetch and adds optional response caching.

import { pn } from "@page-proxy/pp";

pn.fetch(input, options)

Supports standard fetch options plus these cache options:

  • cache?: boolean
  • cacheDuration?: number (milliseconds, default 86400000)
  • cacheKey?: string (default is request URL)
  • requestCache?: RequestCache (maps to native fetch cache option)

When cache: true:

  • Cache keys are saved in local storage with prefix pp-network-cache:
  • Responses larger than 512KB are not cached
  • pn cache and pt storage share one combined limit
  • At most 50 saved keys are kept per script scope
const response = await pn.fetch("https://api.example.com/items", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ limit: 20 }),
cache: true,
cacheDuration: 5 * 60 * 1000,
cacheKey: "items:list"
});

Method helpers

These call pn.fetch with the corresponding HTTP method:

  • pn.get
  • pn.head
  • pn.post
  • pn.put
  • pn.delete
  • pn.connect
  • pn.options
  • pn.trace
  • pn.patch
const response = await pn.get("https://api.example.com/items", { cache: true });
const data = await response.json();

pn.invalidateCache(key)

Deletes cached response entries for a cache key (or URL).

  • Returns true if at least one cache entry was removed.
  • Returns false if no matching cache entry exists.

Storage key matching follows the same identity rules used by pn.fetch caching:

  1. pn.fetch cache identity:
    • If options.cacheKey?.trim() is non-empty, that trimmed value is used.
    • Otherwise the identity is request.url from new Request(input, options) (absolute, normalized URL).
  2. Local storage key format:
    • pp-network-cache:<scope>:<identity>
    • <scope> is the current script storage scope (for example global).
  3. pn.invalidateCache(key) matching:
    • Trims key; empty string does nothing (false).
    • Always checks/removes the direct key: pp-network-cache:<scope>:<trimmed key>.
    • If key can be parsed as a URL (including relative URLs with location.href as base), it also checks/removes the normalized URL identity key used by pn.fetch.

This means invalidation works for both custom cacheKey values and URL-based cache entries created without cacheKey.

pn.invalidateCache("items:list");
pn.invalidateCache("https://api.example.com/items");