@findlane/browser 0.28.0 → 0.28.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/dist/images.d.ts CHANGED
@@ -16,6 +16,16 @@ export declare function imageOriginOf(header: string | null | undefined): string
16
16
  /** Where an index's images loaded from on an earlier page, kept in this browser (localStorage), so the next page
17
17
  * can open that connection before its first answer says; null when unknown or without storage. */
18
18
  export declare function rememberedImageOrigin(id: string): string | null;
19
+ /** What an index's last answer in this browser said about its images (0.28.1): the hosts that may be resized and
20
+ * where from, so a page showing results it kept (a reload, Back) resizes them before its first search; null when
21
+ * unknown, off, or without storage. */
22
+ export declare function rememberedImages(id: string): {
23
+ hosts: Set<string>;
24
+ origin: string | null;
25
+ } | null;
26
+ /** Keeps what an answer said about an index's images for later pages (0.28.1), or forgets it once it has none. The
27
+ * 0.28.0 key with the origin alone is kept in step, for the widget. */
28
+ export declare function rememberImages(id: string, hosts: Set<string>, origin: string | null): void;
19
29
  /** Keeps where an index's images load from for later pages, or forgets it (null) once the index has none. */
20
30
  export declare function rememberImageOrigin(id: string, origin: string | null): void;
21
31
  /** Opens a connection to `origin` before a request needs it, as a preconnect hint in the page would: once per page,
package/dist/images.js CHANGED
@@ -32,6 +32,7 @@ export function imageOriginOf(header) {
32
32
  }
33
33
  }
34
34
  const originKey = (id) => `findlane:image-origin:${id}`;
35
+ const imagesKey = (id) => `findlane:images:${id}`;
35
36
  /** Where an index's images loaded from on an earlier page, kept in this browser (localStorage), so the next page
36
37
  * can open that connection before its first answer says; null when unknown or without storage. */
37
38
  export function rememberedImageOrigin(id) {
@@ -42,6 +43,33 @@ export function rememberedImageOrigin(id) {
42
43
  return null;
43
44
  }
44
45
  }
46
+ /** What an index's last answer in this browser said about its images (0.28.1): the hosts that may be resized and
47
+ * where from, so a page showing results it kept (a reload, Back) resizes them before its first search; null when
48
+ * unknown, off, or without storage. */
49
+ export function rememberedImages(id) {
50
+ try {
51
+ const kept = JSON.parse(localStorage.getItem(imagesKey(id)) ?? "null");
52
+ const hosts = Array.isArray(kept?.hosts) ? imageHostsOf(kept.hosts.filter((host) => typeof host === "string").join(",")) : new Set();
53
+ return hosts.size ? { hosts, origin: imageOriginOf(typeof kept?.origin === "string" ? kept.origin : null) } : null;
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ /** Keeps what an answer said about an index's images for later pages (0.28.1), or forgets it once it has none. The
60
+ * 0.28.0 key with the origin alone is kept in step, for the widget. */
61
+ export function rememberImages(id, hosts, origin) {
62
+ try {
63
+ if (hosts.size)
64
+ localStorage.setItem(imagesKey(id), JSON.stringify({ hosts: [...hosts], origin }));
65
+ else
66
+ localStorage.removeItem(imagesKey(id));
67
+ }
68
+ catch {
69
+ // Storage that can't be used: later pages learn them from their first answer.
70
+ }
71
+ rememberImageOrigin(id, hosts.size ? origin : null);
72
+ }
45
73
  /** Keeps where an index's images load from for later pages, or forgets it (null) once the index has none. */
46
74
  export function rememberImageOrigin(id, origin) {
47
75
  try {
package/dist/index.d.ts CHANGED
@@ -424,8 +424,11 @@ export declare class SearchBrowserClient {
424
424
  private imageHosts;
425
425
  /** Where they load from (x-findlane-image-origin, 0.28.0); the API's own origin without one. */
426
426
  private imageOrigin;
427
- /** Where they loaded from on an earlier page in this browser, read once; undefined until read. */
428
- private knownImageOrigin;
427
+ /** What an earlier page's answers said about them in this browser (0.28.1), read once, used until this page's first
428
+ * answer; undefined until read. */
429
+ private knownImages;
430
+ /** Whether an answer has said, so what was remembered is no longer used. */
431
+ private answered;
429
432
  constructor(options: BrowserClientOptions);
430
433
  /** The signed key searches send: the one given, or the function's, asked for again 30 seconds before it
431
434
  * expires, or at once when `fresh`. */
@@ -433,18 +436,18 @@ export declare class SearchBrowserClient {
433
436
  private post;
434
437
  private get;
435
438
  /** Each answer says which hosts the index's images may be loaded from resized, and where from (0.28.0); refusals
436
- * say nothing. Where from is kept for later pages, and its connection opened now, before the results' images ask. */
439
+ * say nothing. Both are kept for later pages (0.28.1), and the connection opened now, before the results' images ask. */
437
440
  private noteImages;
438
- private rememberedOrigin;
441
+ private remembered;
439
442
  /** Opens the connection to where the index's images loaded from on an earlier page, with the first request or
440
443
  * when a search box gets focus, so the results' first image doesn't wait for it. Nothing for an index without. */
441
444
  private warmImages;
442
445
  /** Optimized images (0.27.0): a record image's address, resized to `width` pixels (rounded up to one of
443
446
  * `imageWidths`) and sent as WebP from the address the API names (images.findlane.dev since 0.28.0; the API's own
444
447
  * before), when the index has optimized images on and the image is on a host it allows; otherwise `src` as it is.
445
- * Known from the first answer on, so build image addresses from results, not before the first search. Load them
446
- * with `crossorigin="anonymous"`, which uses the connection opened for them ahead of need. React's `Image` does all
447
- * of this. */
448
+ * Known from the first answer on; since 0.28.1, before it too from what an earlier page's answers said in this
449
+ * browser, so results a page kept (a reload, Back) are resized. Load them with `crossorigin="anonymous"`, which uses
450
+ * the connection opened for them ahead of need. React's `Image` does all of this. */
448
451
  imageUrl(src: string, width: number): string;
449
452
  /** Opens the connection to the API before the first search needs it, as a preconnect hint in the page
450
453
  * would: call it when the shopper focuses a search box. Once per page; nothing outside a browser. Since 0.28.0 it
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { imageHostsHeader, imageHostsOf, imageOriginHeader, imageOriginOf, optimizedImageUrl, preconnect, rememberedImageOrigin, rememberImageOrigin } from "./images.js";
1
+ import { imageHostsHeader, imageHostsOf, imageOriginHeader, imageOriginOf, optimizedImageUrl, preconnect, rememberedImages, rememberImages } from "./images.js";
2
2
  export { imageWidth, imageWidths } from "./images.js";
3
3
  export { placeholderCss, placeholderUrl, revealCss } from "./placeholder.js";
4
4
  /** A query suggestion's text in parts: what was typed (`typed: true`) and the rest. */
@@ -290,8 +290,11 @@ export class SearchBrowserClient {
290
290
  imageHosts = new Set();
291
291
  /** Where they load from (x-findlane-image-origin, 0.28.0); the API's own origin without one. */
292
292
  imageOrigin = null;
293
- /** Where they loaded from on an earlier page in this browser, read once; undefined until read. */
294
- knownImageOrigin;
293
+ /** What an earlier page's answers said about them in this browser (0.28.1), read once, used until this page's first
294
+ * answer; undefined until read. */
295
+ knownImages;
296
+ /** Whether an answer has said, so what was remembered is no longer used. */
297
+ answered = false;
295
298
  constructor(options) {
296
299
  if (!/^pub_[a-f0-9]{32}$/.test(options.publicId))
297
300
  throw new Error("A valid public index ID (pub_…) is required");
@@ -353,39 +356,44 @@ export class SearchBrowserClient {
353
356
  return data;
354
357
  }
355
358
  /** Each answer says which hosts the index's images may be loaded from resized, and where from (0.28.0); refusals
356
- * say nothing. Where from is kept for later pages, and its connection opened now, before the results' images ask. */
359
+ * say nothing. Both are kept for later pages (0.28.1), and the connection opened now, before the results' images ask. */
357
360
  noteImages(response) {
358
361
  if (!response.ok)
359
362
  return;
363
+ this.answered = true;
360
364
  this.imageHosts = imageHostsOf(response.headers?.get(imageHostsHeader));
361
365
  this.imageOrigin = this.imageHosts.size ? imageOriginOf(response.headers?.get(imageOriginHeader)) : null;
362
366
  if (this.imageOrigin)
363
367
  preconnect(this.imageOrigin);
364
- if (this.imageOrigin !== this.rememberedOrigin()) {
365
- rememberImageOrigin(this.publicId, this.imageOrigin);
366
- this.knownImageOrigin = this.imageOrigin;
368
+ const known = this.remembered();
369
+ const same = known ? known.origin === this.imageOrigin && known.hosts.size === this.imageHosts.size &&
370
+ [...this.imageHosts].every((host) => known.hosts.has(host)) : !this.imageHosts.size;
371
+ if (!same) {
372
+ rememberImages(this.publicId, this.imageHosts, this.imageOrigin);
373
+ this.knownImages = this.imageHosts.size ? { hosts: new Set(this.imageHosts), origin: this.imageOrigin } : null;
367
374
  }
368
375
  }
369
- rememberedOrigin() {
370
- if (this.knownImageOrigin === undefined)
371
- this.knownImageOrigin = rememberedImageOrigin(this.publicId);
372
- return this.knownImageOrigin;
376
+ remembered() {
377
+ if (this.knownImages === undefined)
378
+ this.knownImages = rememberedImages(this.publicId);
379
+ return this.knownImages;
373
380
  }
374
381
  /** Opens the connection to where the index's images loaded from on an earlier page, with the first request or
375
382
  * when a search box gets focus, so the results' first image doesn't wait for it. Nothing for an index without. */
376
383
  warmImages() {
377
- const origin = this.rememberedOrigin();
384
+ const origin = this.remembered()?.origin;
378
385
  if (origin)
379
386
  preconnect(origin);
380
387
  }
381
388
  /** Optimized images (0.27.0): a record image's address, resized to `width` pixels (rounded up to one of
382
389
  * `imageWidths`) and sent as WebP from the address the API names (images.findlane.dev since 0.28.0; the API's own
383
390
  * before), when the index has optimized images on and the image is on a host it allows; otherwise `src` as it is.
384
- * Known from the first answer on, so build image addresses from results, not before the first search. Load them
385
- * with `crossorigin="anonymous"`, which uses the connection opened for them ahead of need. React's `Image` does all
386
- * of this. */
391
+ * Known from the first answer on; since 0.28.1, before it too from what an earlier page's answers said in this
392
+ * browser, so results a page kept (a reload, Back) are resized. Load them with `crossorigin="anonymous"`, which uses
393
+ * the connection opened for them ahead of need. React's `Image` does all of this. */
387
394
  imageUrl(src, width) {
388
- return optimizedImageUrl(this.imageOrigin ?? this.origin, this.publicId, this.imageHosts, src, width) ?? src;
395
+ const known = this.answered ? null : this.remembered();
396
+ return optimizedImageUrl((known ? known.origin : this.imageOrigin) ?? this.origin, this.publicId, known?.hosts ?? this.imageHosts, src, width) ?? src;
389
397
  }
390
398
  /** Opens the connection to the API before the first search needs it, as a preconnect hint in the page
391
399
  * would: call it when the shopper focuses a search box. Once per page; nothing outside a browser. Since 0.28.0 it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@findlane/browser",
3
- "version": "0.28.0",
3
+ "version": "0.28.1",
4
4
  "description": "Findlane browser SDK: fast, typo-tolerant public search from the browser with an index's public ID, plus React hooks and components.",
5
5
  "keywords": ["findlane", "search", "full-text search", "typo tolerance", "instant search", "ecommerce search", "browser", "react", "autocomplete"],
6
6
  "license": "MIT",