@usethrottle/builder 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,7 +44,9 @@ notice: it reads the site generation (at most every 30 s, only while the tab is
44
44
  and calls `onChange`. `router.refresh()` renders from the server's data cache, so `onChange` first POSTs to a
45
45
  store route that clears it: `export const POST = createWatchSyncHandler({ builderKey: process.env.NEXT_PUBLIC_THROTTLE_BUILDER_KEY })`
46
46
  from `@usethrottle/builder/next`. The route reads the generation from the Builder (at most once per 20 s per
47
- instance) and clears the `throttle-builder` tag only when it moved. 500 with no key, 502 when the Builder cannot be read.
47
+ instance) and compares it with the one the store's data cache holds (a `throttle-builder`-tagged read of the same
48
+ answer, shared by every instance). It clears the tag only when they differ, so a cold start or a flood of calls
49
+ purges nothing: purges are bounded by real publishes. The route takes no secret (the call comes from a browser). 500 with no key, 502 when the Builder cannot be read.
48
50
 
49
51
  ## Build mode
50
52
 
package/dist/next.cjs CHANGED
@@ -98,31 +98,34 @@ async function defaultRevalidateTag2() {
98
98
  function createWatchSyncHandler(options) {
99
99
  const minInterval = Math.max(_nullishCoalesce(options.minIntervalMs, () => ( DEFAULT_MIN_INTERVAL_MS)), 5e3);
100
100
  const now = _nullishCoalesce(options.now, () => ( Date.now));
101
- let known;
102
101
  let lastReadAt;
103
102
  let running;
104
103
  const run = async (key) => {
105
104
  const doFetch = _nullishCoalesce(options.fetch, () => ( fetch));
106
105
  const base = (_nullishCoalesce(options.baseUrl, () => ( _chunkZLNUG6ZZcjs.DEFAULT_BASE_URL))).replace(/\/+$/u, "");
107
- let generation;
108
- try {
106
+ const read = async (init) => {
109
107
  const response = await doFetch(`${base}/v1/site`, {
110
108
  headers: { "X-Builder-Key": key },
111
- cache: "no-store",
112
- signal: AbortSignal.timeout(READ_TIMEOUT_MS)
109
+ signal: AbortSignal.timeout(READ_TIMEOUT_MS),
110
+ ...init
113
111
  });
114
112
  if (!response.ok) throw new Error(`Builder answered ${String(response.status)}`);
115
113
  const body = await response.json();
116
114
  if (typeof body.generation !== "number" || !Number.isSafeInteger(body.generation))
117
115
  throw new Error("Builder answered no generation");
118
- generation = body.generation;
116
+ return body.generation;
117
+ };
118
+ let generation;
119
+ let recorded;
120
+ try {
121
+ recorded = await read({ next: { tags: [_chunkZLNUG6ZZcjs.BUILDER_TAGS.all], revalidate: false } });
122
+ generation = await read({ cache: "no-store" });
119
123
  } catch (e2) {
120
124
  return json2(502, { error: "The Builder could not be read." });
121
125
  }
122
- if (generation === known) return json2(200, { changed: false, generation });
126
+ if (generation === recorded) return json2(200, { changed: false, generation });
123
127
  const revalidate = await _asyncNullishCoalesce(options.revalidateTag, async () => ( await defaultRevalidateTag2()));
124
128
  revalidate(_chunkZLNUG6ZZcjs.BUILDER_TAGS.all);
125
- known = generation;
126
129
  return json2(200, { changed: true, generation });
127
130
  };
128
131
  return () => {
package/dist/next.d.cts CHANGED
@@ -40,15 +40,16 @@ type CreateWatchSyncHandlerOptions = {
40
40
  * in an open tab only learns that the Builder's generation moved; it cannot
41
41
  * clear the store's data cache. The tab POSTs here (a store route such as
42
42
  * `app/api/throttle/builder-sync/route.ts`), and the route reads the site's
43
- * generation uncached from the Builder with the publishable key. When it
44
- * differs from the last one this instance saw (or this instance has seen
45
- * none), it clears the coarse `throttle-builder` tag, and the `router.refresh()`
46
- * that follows renders fresh. The Builder is read at most once per
47
- * `minIntervalMs` per instance and nothing is cleared unless the generation
48
- * really moved, so the route cannot be used to hammer the Builder or to empty
49
- * the cache. Tags only, never a path. Answers 200 `{ changed, generation? }`
50
- * (`throttled: true` inside the window), 500 with no key, 502 when the Builder
51
- * cannot be read.
43
+ * generation uncached from the Builder with the publishable key. It clears the
44
+ * coarse `throttle-builder` tag only when that generation differs from the one
45
+ * the store's own data cache holds (a `throttle-builder`-tagged read of the
46
+ * same answer, shared by every instance and every cold start; an instance that
47
+ * has seen nothing treats a miss as the live generation and clears nothing). So the number of purges is bounded by the
48
+ * number of real publishes, whoever calls the route and however often; the
49
+ * route is browser-facing and cannot hold a secret. The Builder is read at most
50
+ * once per `minIntervalMs` per instance. Tags only, never a path. Answers 200
51
+ * `{ changed, generation? }` (`throttled: true` inside the window), 500 with no
52
+ * key, 502 when the Builder cannot be read.
52
53
  */
53
54
  declare function createWatchSyncHandler(options: CreateWatchSyncHandlerOptions): (req?: Request) => Promise<Response>;
54
55
 
package/dist/next.d.ts CHANGED
@@ -40,15 +40,16 @@ type CreateWatchSyncHandlerOptions = {
40
40
  * in an open tab only learns that the Builder's generation moved; it cannot
41
41
  * clear the store's data cache. The tab POSTs here (a store route such as
42
42
  * `app/api/throttle/builder-sync/route.ts`), and the route reads the site's
43
- * generation uncached from the Builder with the publishable key. When it
44
- * differs from the last one this instance saw (or this instance has seen
45
- * none), it clears the coarse `throttle-builder` tag, and the `router.refresh()`
46
- * that follows renders fresh. The Builder is read at most once per
47
- * `minIntervalMs` per instance and nothing is cleared unless the generation
48
- * really moved, so the route cannot be used to hammer the Builder or to empty
49
- * the cache. Tags only, never a path. Answers 200 `{ changed, generation? }`
50
- * (`throttled: true` inside the window), 500 with no key, 502 when the Builder
51
- * cannot be read.
43
+ * generation uncached from the Builder with the publishable key. It clears the
44
+ * coarse `throttle-builder` tag only when that generation differs from the one
45
+ * the store's own data cache holds (a `throttle-builder`-tagged read of the
46
+ * same answer, shared by every instance and every cold start; an instance that
47
+ * has seen nothing treats a miss as the live generation and clears nothing). So the number of purges is bounded by the
48
+ * number of real publishes, whoever calls the route and however often; the
49
+ * route is browser-facing and cannot hold a secret. The Builder is read at most
50
+ * once per `minIntervalMs` per instance. Tags only, never a path. Answers 200
51
+ * `{ changed, generation? }` (`throttled: true` inside the window), 500 with no
52
+ * key, 502 when the Builder cannot be read.
52
53
  */
53
54
  declare function createWatchSyncHandler(options: CreateWatchSyncHandlerOptions): (req?: Request) => Promise<Response>;
54
55
 
package/dist/next.js CHANGED
@@ -98,31 +98,34 @@ async function defaultRevalidateTag2() {
98
98
  function createWatchSyncHandler(options) {
99
99
  const minInterval = Math.max(options.minIntervalMs ?? DEFAULT_MIN_INTERVAL_MS, 5e3);
100
100
  const now = options.now ?? Date.now;
101
- let known;
102
101
  let lastReadAt;
103
102
  let running;
104
103
  const run = async (key) => {
105
104
  const doFetch = options.fetch ?? fetch;
106
105
  const base = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/u, "");
107
- let generation;
108
- try {
106
+ const read = async (init) => {
109
107
  const response = await doFetch(`${base}/v1/site`, {
110
108
  headers: { "X-Builder-Key": key },
111
- cache: "no-store",
112
- signal: AbortSignal.timeout(READ_TIMEOUT_MS)
109
+ signal: AbortSignal.timeout(READ_TIMEOUT_MS),
110
+ ...init
113
111
  });
114
112
  if (!response.ok) throw new Error(`Builder answered ${String(response.status)}`);
115
113
  const body = await response.json();
116
114
  if (typeof body.generation !== "number" || !Number.isSafeInteger(body.generation))
117
115
  throw new Error("Builder answered no generation");
118
- generation = body.generation;
116
+ return body.generation;
117
+ };
118
+ let generation;
119
+ let recorded;
120
+ try {
121
+ recorded = await read({ next: { tags: [BUILDER_TAGS.all], revalidate: false } });
122
+ generation = await read({ cache: "no-store" });
119
123
  } catch {
120
124
  return json2(502, { error: "The Builder could not be read." });
121
125
  }
122
- if (generation === known) return json2(200, { changed: false, generation });
126
+ if (generation === recorded) return json2(200, { changed: false, generation });
123
127
  const revalidate = options.revalidateTag ?? await defaultRevalidateTag2();
124
128
  revalidate(BUILDER_TAGS.all);
125
- known = generation;
126
129
  return json2(200, { changed: true, generation });
127
130
  };
128
131
  return () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usethrottle/builder",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Headless client for Throttle Builder: read published pages, parts, slots and site settings from a storefront, with Next.js cache, revalidation and preview helpers.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -59,7 +59,7 @@
59
59
  ],
60
60
  "dependencies": {
61
61
  "server-only": "0.0.1",
62
- "@usethrottle/builder-schema": "^0.1.2"
62
+ "@usethrottle/builder-schema": "^0.2.0"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "next": ">=14",