@zappie/sdk 0.12.0 → 0.14.0
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/CHANGELOG.md +26 -0
- package/README.md +97 -4
- package/dist/config/index.d.ts +5 -1
- package/dist/debug.js +1 -1
- package/dist/index.js +1 -1
- package/dist/navigation-progress/index.d.ts +42 -0
- package/dist/navigation-progress/index.js +42 -0
- package/dist/navigation-progress/index.js.map +6 -0
- package/dist/navigation-progress/styles.css +37 -0
- package/dist/server/chosen-branch.js +24 -2
- package/dist/server/chosen-branch.js.map +1 -1
- package/dist/server/edge-cache.js +395 -0
- package/dist/server/edge-cache.js.map +6 -0
- package/dist/server/index.d.ts +161 -7
- package/dist/server/index.js +24 -0
- package/dist/server/index.js.map +1 -1
- package/dist/site-config/validate.js +8 -11
- package/dist/site-config/validate.js.map +1 -1
- package/dist/vite/index.d.ts +39 -4
- package/dist/vite/index.js +112 -7
- package/package.json +10 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @zappie/sdk
|
|
2
2
|
|
|
3
|
+
## 0.14.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 40e49dc: New `@zappie/sdk/navigation-progress` entry with `NavigationProgress`, a thin line along the top of the page while a navigation runs, and its stylesheet `@zappie/sdk/navigation-progress.css`. It shows only once a navigation has taken 150 ms (`delayMs`), so a quick one never flickers, grows toward the end of the line while it waits, then fills it and fades out. With reduced motion it is a still line. Pass the router's state as `active` and the brand colour as `color`. It also dims a link or button the moment it's pressed, through a rule a site's own styles override, unless you pass `pressed={false}`. `useDelayedActive(active, delayMs)` gives the same delay to anything else.
|
|
8
|
+
|
|
9
|
+
The starter draws the line in its accent colour and prefetches the home and account pages when a shopper points at their links.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 31be183: `cachePage` renders afresh, and never shares, a page whose request carries `X-Forwarded-Host` or `Forwarded`, so a site that builds its links from a forwarded host can't have a page drawn for a caller-chosen host served to other shoppers. `pageCacheBypass` reports it as `"forwarded"`.
|
|
14
|
+
|
|
15
|
+
## 0.13.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- 846083d: A site Worker now answers from its own cache. `cachedApiFetch({ ctx })` from `@zappie/sdk/server` is a fetch for `createZappie` that keeps `GET /store` and `GET /menus` for as long as the API's `Cache-Control` says, answers a stale read at once and refreshes it after the response through `ctx.waitUntil`. Its key holds a hash of the publishable key, never the key. `cachePage(request, render, { ctx, deployId, branchCookie })` keeps anonymous documents, their `.data`, robots and the sitemap, keyed by URL, branch cookie, deploy and site. It renders afresh for drafts, reviews, previews, the closed state, any `Authorization` header or session cookie and anything but a `GET`, keeps only a cookie-free `200`, and gives each cached HTML page a new CSP nonce. `x-zappie-edge-cache` says how each response was answered. `openEdgeCache()` opens the Worker's own cache, because Workers for Platforms disables `caches.default`.
|
|
20
|
+
|
|
21
|
+
`loadStoreAndMenu` starts the menu read beside the store read for a branch named by slug once a store read in the same isolate has given that slug's ID, and its `cookie` option is now optional.
|
|
22
|
+
|
|
23
|
+
`@zappie/sdk/vite` adds `zappieWorker()`, which minifies the Worker, renders it with React's streaming renderer alone and sets `import.meta.env.ZAPPIE_DEPLOY_ID` for each build. `zappieSource` now pre-bundles every browser entry of `@zappie/sdk` and `@zappie/foundations` and scans `app/` on every dev server, so a new page never re-bundles and reloads a Studio preview.
|
|
24
|
+
|
|
25
|
+
`prerender` in `zappie.config.ts` did nothing, because Zappie hosting prerenders no page. `validateConfig` now warns `unused_setting` when it is set, and the type marks it deprecated.
|
|
26
|
+
|
|
27
|
+
The starter caches its pages and API reads in the Worker, reads the store and menu side by side, answers robots and the sitemap with a 5-minute shared lifetime, builds with `zappieWorker()` and drops `prerender`. Its server build is 214 KB instead of 715 KB. Set `ZAPPIE_EDGE_CACHE=off` to turn the cache off.
|
|
28
|
+
|
|
3
29
|
## 0.12.0
|
|
4
30
|
|
|
5
31
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ Prices always come from Zappie. The cart shows a local estimate while it waits,
|
|
|
70
70
|
| `@zappie/sdk/error-pages` and `/error-pages/react` | Zappie's page not found and something went wrong pages, for the site host's fallback |
|
|
71
71
|
| `@zappie/sdk/site-error` and `/site-error/react` | Your own 404 and error pages: which page, safe words, Try again and the home link |
|
|
72
72
|
| `@zappie/sdk/site-closed` | Your own closed page while the merchant has switched the storefront off |
|
|
73
|
+
| `@zappie/sdk/navigation-progress` | A line along the top of the page while a slow navigation runs, and the pressed state |
|
|
73
74
|
| `@zappie/sdk/receipts` | Receipt PDFs as files, and whether the device can share them |
|
|
74
75
|
| `@zappie/sdk/testing` | A mock client for your tests |
|
|
75
76
|
| `@zappie/sdk/url` | Storefront URL parsing and canonical redirects |
|
|
@@ -170,6 +171,36 @@ Your root loader reads the same `siteClosed(request)` and draws the closed
|
|
|
170
171
|
page instead of the shop. The host asks only for GET and HEAD pages and waits
|
|
171
172
|
5 seconds for an answer.
|
|
172
173
|
|
|
174
|
+
`NavigationProgress` from `@zappie/sdk/navigation-progress` draws a thin line
|
|
175
|
+
along the top of the page while a navigation runs. It shows only once the
|
|
176
|
+
navigation has taken 150 ms (`delayMs`), so a quick one never flickers, grows
|
|
177
|
+
toward the end of the line while it waits, then fills it and fades out. With
|
|
178
|
+
reduced motion it is a still line while the navigation runs. Pass your router's
|
|
179
|
+
state as `active` and your brand colour as `color`. It also dims a link or
|
|
180
|
+
button the moment it's pressed, through a rule any of your own styles override.
|
|
181
|
+
Pass `pressed={false}` to leave that out. Import the stylesheet once, after your
|
|
182
|
+
theme's. `useDelayedActive(active, delayMs)` gives the same delay for anything
|
|
183
|
+
else that should wait before it shows.
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
import { NavigationProgress } from "@zappie/sdk/navigation-progress";
|
|
187
|
+
import "@zappie/sdk/navigation-progress.css";
|
|
188
|
+
import { useNavigation } from "react-router";
|
|
189
|
+
|
|
190
|
+
export function Shell({ children }: { children: React.ReactNode }) {
|
|
191
|
+
const navigation = useNavigation();
|
|
192
|
+
return (
|
|
193
|
+
<div>
|
|
194
|
+
<NavigationProgress
|
|
195
|
+
active={navigation.state !== "idle"}
|
|
196
|
+
color="var(--z-accent)"
|
|
197
|
+
/>
|
|
198
|
+
{children}
|
|
199
|
+
</div>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
173
204
|
Receipts come as PDF files. `customer.receipt(orderId)` reads an online order's
|
|
174
205
|
receipt, and `receiptFile(url)` from `@zappie/sdk/receipts` reads any receipt
|
|
175
206
|
address, such as a table bill's `inVenue.receiptUrl(orderId)`. Hand the file to
|
|
@@ -322,10 +353,72 @@ When the link, the cookie or the fallback names the branch by ID (`br_…`),
|
|
|
322
353
|
the menu read starts alongside the store read, so the page waits for one round
|
|
323
354
|
trip instead of two. You get the menu once the store confirms that branch,
|
|
324
355
|
with the same errors as `store.get` followed by `menu.get`. A branch named by
|
|
325
|
-
slug
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
356
|
+
slug starts early too once a store read in the same Worker isolate has given
|
|
357
|
+
that slug's ID. The first such request reads the menu after the store, because
|
|
358
|
+
the API takes branch IDs only. `cookie` is optional for a site that keeps no
|
|
359
|
+
branch cookie. `requestedBranch(request, options)` returns the raw value a
|
|
360
|
+
request names, link first, before you have the store.
|
|
361
|
+
|
|
362
|
+
### Edge cache in a site Worker
|
|
363
|
+
|
|
364
|
+
On Zappie hosting each site runs as a Worker in a Workers for Platforms
|
|
365
|
+
dispatch namespace. There `caches.default` is disabled, and each Worker's
|
|
366
|
+
`caches.open()` cache is its own. `@zappie/sdk/server` keeps two kinds of
|
|
367
|
+
response in that cache, through `openEdgeCache()`.
|
|
368
|
+
|
|
369
|
+
`cachedApiFetch({ ctx })` is a fetch for `createZappie` on the server. It keeps
|
|
370
|
+
`GET /store` and `GET /menus` for as long as the API's own `Cache-Control`
|
|
371
|
+
says (`s-maxage`, then `stale-while-revalidate`). A stale read answers at once
|
|
372
|
+
and refreshes after the response through `ctx.waitUntil`, with the menu's ETag.
|
|
373
|
+
Its key is the URL, the API version and a hash of the publishable key, never
|
|
374
|
+
the key itself. A customer's, cart's, order's or quote's call, and any call
|
|
375
|
+
signed in as a shopper or carrying a cookie, goes straight to the API.
|
|
376
|
+
|
|
377
|
+
`cachePage(request, render, { ctx, deployId, branchCookie })` answers a site
|
|
378
|
+
Worker's anonymous pages from the cache: documents, their `.data`, robots and
|
|
379
|
+
the sitemap, for as long as each response's `Cache-Control` says. Its key is
|
|
380
|
+
the full URL, the branch cookie's value, the deploy, and the site the host
|
|
381
|
+
names. It renders afresh for a request that isn't a `GET`, a draft, review or
|
|
382
|
+
preview, the closed state, an `Authorization` header and a session or sign-in
|
|
383
|
+
cookie. It keeps only a `200` that sets no cookie and isn't the closed page. A
|
|
384
|
+
cached HTML page gets a new CSP nonce in its policy and its markup for each
|
|
385
|
+
shopper. `x-zappie-edge-cache` says how each response was answered: `HIT`,
|
|
386
|
+
`STALE`, `MISS` or `BYPASS`.
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import { cachePage, cachedApiFetch } from "@zappie/sdk/server";
|
|
390
|
+
|
|
391
|
+
const client = createZappie({
|
|
392
|
+
store,
|
|
393
|
+
key,
|
|
394
|
+
baseUrl,
|
|
395
|
+
fetch: cachedApiFetch({ ctx }),
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
export default {
|
|
399
|
+
fetch: (request, env, ctx) =>
|
|
400
|
+
cachePage(request, (fresh) => handle(fresh, siteContext(env, ctx)), {
|
|
401
|
+
ctx,
|
|
402
|
+
deployId: import.meta.env.ZAPPIE_DEPLOY_ID,
|
|
403
|
+
branchCookie: "site_branch",
|
|
404
|
+
}),
|
|
405
|
+
};
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### Vite plugins
|
|
409
|
+
|
|
410
|
+
`@zappie/sdk/vite` holds the build and dev plugins a site uses.
|
|
411
|
+
|
|
412
|
+
- `zappieSource({ previewHost })` marks elements with their source on the
|
|
413
|
+
Studio dev server. On every dev server it also pre-bundles each browser
|
|
414
|
+
entry of the installed `@zappie/sdk` and `@zappie/foundations`, and scans
|
|
415
|
+
every module under `app/`, so opening a page that imports an entry the
|
|
416
|
+
session hasn't seen never re-bundles and reloads the preview.
|
|
417
|
+
- `zappieWorker()` minifies the site's Worker, renders it with React's
|
|
418
|
+
streaming renderer alone, and sets `import.meta.env.ZAPPIE_DEPLOY_ID` to a
|
|
419
|
+
new id for each build. The browser's code is unchanged.
|
|
420
|
+
- `zappieHoistImports()` lists each route's chunks in its own file.
|
|
421
|
+
- `zappieUploads({ host })` serves the site's uploads on a local dev server.
|
|
329
422
|
|
|
330
423
|
### Checkout defaults
|
|
331
424
|
|
package/dist/config/index.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ type ZappieSiteConfig = {
|
|
|
18
18
|
store: string;
|
|
19
19
|
branches?: string[];
|
|
20
20
|
theme: string;
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Zappie hosting prerenders no page: every page renders in your Worker, and its page cache keeps the
|
|
23
|
+
* anonymous ones. `validateConfig` warns when it is set. Remove it.
|
|
24
|
+
*/
|
|
21
25
|
prerender?: string[];
|
|
22
26
|
menu?: {
|
|
23
27
|
hiddenCategories?: string[];
|
|
@@ -96,7 +100,7 @@ type ZappieSiteConfig = {
|
|
|
96
100
|
*/
|
|
97
101
|
type ConfigIssue = {
|
|
98
102
|
path: string;
|
|
99
|
-
code: "invalid_type" | "missing_required" | "unknown_key" | "invalid_format" | "duplicate" | "redundant_allowlist" | "budget_override";
|
|
103
|
+
code: "invalid_type" | "missing_required" | "unknown_key" | "invalid_format" | "duplicate" | "redundant_allowlist" | "budget_override" | "unused_setting";
|
|
100
104
|
message: string;
|
|
101
105
|
severity: "error" | "warning";
|
|
102
106
|
};
|
package/dist/debug.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -11,7 +11,7 @@ import { ZappieError, isZappieError } from "./errors.js";
|
|
|
11
11
|
export * from "./models.js";
|
|
12
12
|
import { format, money } from "./money.js";
|
|
13
13
|
import { RestTransport } from "./transport/rest.js";
|
|
14
|
-
var VERSION = true ? "0.
|
|
14
|
+
var VERSION = true ? "0.14.0" : "0.0.0-development";
|
|
15
15
|
export {
|
|
16
16
|
API_VERSION,
|
|
17
17
|
RestTransport,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
/** Props for `NavigationProgress`. */
|
|
4
|
+
type NavigationProgressProps = {
|
|
5
|
+
/** True while a navigation runs, such as `useNavigation().state !== "idle"` from React Router. */
|
|
6
|
+
active: boolean;
|
|
7
|
+
/** How long a navigation runs, in milliseconds, before the bar shows. Defaults to 150. */
|
|
8
|
+
delayMs?: number;
|
|
9
|
+
/** Any CSS colour, usually the site's brand token, such as `"var(--brand)"`. Defaults to `"currentColor"`. */
|
|
10
|
+
color?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Dims a link or button the moment it's pressed, through a `:where(a[href],button:enabled):active` rule that any
|
|
13
|
+
* of your own styles override. Defaults to `true`.
|
|
14
|
+
*/
|
|
15
|
+
pressed?: boolean;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Whether `active` has stayed true for `delayMs`. It turns false as soon as `active` does, so work shorter than the
|
|
19
|
+
* delay never shows.
|
|
20
|
+
*
|
|
21
|
+
* @param active - Whether the work runs, such as a navigation.
|
|
22
|
+
* @param delayMs - How long the work runs, in milliseconds, before this turns true. Defaults to 150.
|
|
23
|
+
*/
|
|
24
|
+
declare function useDelayedActive(active: boolean, delayMs?: number): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* A thin line along the top of the page while a navigation runs. It shows only once the navigation has run for
|
|
27
|
+
* `delayMs`, so a quick one never flickers, and fills the width and fades out when it ends. It is decorative and
|
|
28
|
+
* hidden from assistive technology. Server rendering draws it hidden. Import `@zappie/sdk/navigation-progress.css`
|
|
29
|
+
* once for the bar's styles.
|
|
30
|
+
*
|
|
31
|
+
* ```tsx
|
|
32
|
+
* import { NavigationProgress } from "@zappie/sdk/navigation-progress";
|
|
33
|
+
* import "@zappie/sdk/navigation-progress.css";
|
|
34
|
+
* import { useNavigation } from "react-router";
|
|
35
|
+
*
|
|
36
|
+
* <NavigationProgress active={useNavigation().state !== "idle"} color="var(--brand)" />
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function NavigationProgress({ active, delayMs, color, pressed, }: NavigationProgressProps): react.JSX.Element;
|
|
40
|
+
|
|
41
|
+
export { NavigationProgress, useDelayedActive };
|
|
42
|
+
export type { NavigationProgressProps };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/navigation-progress/index.tsx
|
|
4
|
+
import { useEffect, useState } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
var PRESSED = ":where(a[href],button:enabled):active{opacity:.7}";
|
|
7
|
+
function useDelayedActive(active, delayMs = 150) {
|
|
8
|
+
const [shown, setShown] = useState(false);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (!active) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const timer = setTimeout(() => setShown(true), delayMs);
|
|
14
|
+
return () => {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
setShown(false);
|
|
17
|
+
};
|
|
18
|
+
}, [active, delayMs]);
|
|
19
|
+
return active && shown;
|
|
20
|
+
}
|
|
21
|
+
function NavigationProgress({
|
|
22
|
+
active,
|
|
23
|
+
delayMs,
|
|
24
|
+
color = "currentColor",
|
|
25
|
+
pressed = true
|
|
26
|
+
}) {
|
|
27
|
+
return /* @__PURE__ */ jsx(
|
|
28
|
+
"div",
|
|
29
|
+
{
|
|
30
|
+
className: "zappie-nav-progress",
|
|
31
|
+
"data-run": useDelayedActive(active, delayMs) || void 0,
|
|
32
|
+
"aria-hidden": true,
|
|
33
|
+
style: { background: color },
|
|
34
|
+
children: pressed && /* @__PURE__ */ jsx("style", { children: PRESSED })
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
NavigationProgress,
|
|
40
|
+
useDelayedActive
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/navigation-progress/index.tsx"],
|
|
4
|
+
"mappings": ";;;AACA,SAAS,WAAW,gBAAgB;AAqElB;AAnDlB,IAAM,UAAU;AAST,SAAS,iBAAiB,QAAiB,UAAU,KAAc;AACxE,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,KAAK;AACxC,YAAU,MAAM;AACd,QAAI,CAAC,QAAQ;AACX;AAAA,IACF;AACA,UAAM,QAAQ,WAAW,MAAM,SAAS,IAAI,GAAG,OAAO;AACtD,WAAO,MAAM;AACX,mBAAa,KAAK;AAClB,eAAS,KAAK;AAAA,IAChB;AAAA,EACF,GAAG,CAAC,QAAQ,OAAO,CAAC;AACpB,SAAO,UAAU;AACnB;AAgBO,SAAS,mBAAmB;AAAA,EACjC;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR,UAAU;AACZ,GAA4B;AAC1B,SACE;AAAA,IAAC;AAAA;AAAA,MACC,WAAU;AAAA,MACV,YAAU,iBAAiB,QAAQ,OAAO,KAAK;AAAA,MAC/C,eAAW;AAAA,MACX,OAAO,EAAE,YAAY,MAAM;AAAA,MAE1B,qBAAW,oBAAC,WAAO,mBAAQ;AAAA;AAAA,EAC9B;AAEJ;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* The bar NavigationProgress draws. It grows toward 90% while a navigation runs. When it ends, the animation stops,
|
|
2
|
+
the bar fills the width and fades out. With reduced motion there is no animation or fade: the bar is a still,
|
|
3
|
+
full-width line while the navigation runs and goes at once when it ends. Before any navigation the bar has never
|
|
4
|
+
been visible, so nothing fades on load. */
|
|
5
|
+
.zappie-nav-progress {
|
|
6
|
+
position: fixed;
|
|
7
|
+
inset: 0 0 auto;
|
|
8
|
+
height: 3px;
|
|
9
|
+
z-index: 9999;
|
|
10
|
+
pointer-events: none;
|
|
11
|
+
transform-origin: 0;
|
|
12
|
+
opacity: 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.zappie-nav-progress[data-run] {
|
|
16
|
+
opacity: 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
20
|
+
.zappie-nav-progress {
|
|
21
|
+
transition: opacity 0.2s 0.1s;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.zappie-nav-progress[data-run] {
|
|
25
|
+
transition: 0s;
|
|
26
|
+
animation: zappie-nav-progress 8s cubic-bezier(0.1, 0.7, 0.2, 1) forwards;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@keyframes zappie-nav-progress {
|
|
31
|
+
from {
|
|
32
|
+
transform: scaleX(0);
|
|
33
|
+
}
|
|
34
|
+
to {
|
|
35
|
+
transform: scaleX(0.9);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// src/server/chosen-branch.ts
|
|
2
2
|
var find = (store, wanted) => wanted ? store.branches.find((b) => b.id === wanted || b.slug === wanted) : void 0;
|
|
3
3
|
function readCookie(request, name) {
|
|
4
|
+
if (!name) {
|
|
5
|
+
return void 0;
|
|
6
|
+
}
|
|
4
7
|
for (const part of (request.headers.get("cookie") ?? "").split(";")) {
|
|
5
8
|
const index = part.indexOf("=");
|
|
6
9
|
if (index > 0 && part.slice(0, index).trim() === name) {
|
|
@@ -29,17 +32,36 @@ var ignore = () => {
|
|
|
29
32
|
};
|
|
30
33
|
async function loadStoreAndMenu(client, request, options) {
|
|
31
34
|
const named = requestedBranch(request, options);
|
|
32
|
-
const
|
|
35
|
+
const learned = slugs(client);
|
|
36
|
+
const guess = named?.startsWith("br_") ? named : named && learned.get(named);
|
|
37
|
+
const early = guess && !client.store.peek() ? client._internal.readMenu(guess, {}) : void 0;
|
|
33
38
|
early?.catch(ignore);
|
|
34
39
|
const store = await client.store.get();
|
|
40
|
+
for (const entry of store.branches) {
|
|
41
|
+
learned.set(entry.slug, entry.id);
|
|
42
|
+
}
|
|
35
43
|
const resolved = resolveBranch(store, request, options);
|
|
36
44
|
const { branch } = resolved;
|
|
37
45
|
if (!branch) {
|
|
38
46
|
return { store, ...resolved, menu: void 0 };
|
|
39
47
|
}
|
|
40
|
-
const menu = await (early && branch.id ===
|
|
48
|
+
const menu = await (early && branch.id === guess ? early : client.menu.get({ branch: branch.id }));
|
|
41
49
|
return { store, ...resolved, menu };
|
|
42
50
|
}
|
|
51
|
+
var learnedSlugs = /* @__PURE__ */ new Map();
|
|
52
|
+
function slugs(client) {
|
|
53
|
+
const { config } = client;
|
|
54
|
+
const id = JSON.stringify([config.baseUrl ?? "", config.store, config.key]);
|
|
55
|
+
let found = learnedSlugs.get(id);
|
|
56
|
+
if (!found) {
|
|
57
|
+
if (learnedSlugs.size >= 100) {
|
|
58
|
+
learnedSlugs.clear();
|
|
59
|
+
}
|
|
60
|
+
found = /* @__PURE__ */ new Map();
|
|
61
|
+
learnedSlugs.set(id, found);
|
|
62
|
+
}
|
|
63
|
+
return found;
|
|
64
|
+
}
|
|
43
65
|
export {
|
|
44
66
|
loadStoreAndMenu,
|
|
45
67
|
requestedBranch,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/server/chosen-branch.ts"],
|
|
4
|
-
"mappings": ";AA0BA,IAAM,OAAO,CAAC,OAAc,WAC1B,SACI,MAAM,SAAS,KAAK,CAAC,MAAM,EAAE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