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?: booleancacheDuration?: number(milliseconds, default86400000)cacheKey?: string(default is request URL)requestCache?: RequestCache(maps to nativefetchcacheoption)
When cache: true:
- Cache keys are saved in local storage with prefix
pp-network-cache: - Responses larger than
512KBare not cached pncache andptstorage share one combined limit- At most
50saved 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.getpn.headpn.postpn.putpn.deletepn.connectpn.optionspn.tracepn.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
trueif at least one cache entry was removed. - Returns
falseif no matching cache entry exists.
Storage key matching follows the same identity rules used by pn.fetch caching:
pn.fetchcache identity:- If
options.cacheKey?.trim()is non-empty, that trimmed value is used. - Otherwise the identity is
request.urlfromnew Request(input, options)(absolute, normalized URL).
- If
- Local storage key format:
pp-network-cache:<scope>:<identity><scope>is the current script storage scope (for exampleglobal).
pn.invalidateCache(key)matching:- Trims
key; empty string does nothing (false). - Always checks/removes the direct key:
pp-network-cache:<scope>:<trimmed key>. - If
keycan be parsed as a URL (including relative URLs withlocation.hrefas base), it also checks/removes the normalized URL identity key used bypn.fetch.
- Trims
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");