@findlane/browser 0.28.0 → 0.29.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/dist/images.d.ts +12 -0
- package/dist/images.js +30 -0
- package/dist/index.d.ts +26 -9
- package/dist/index.js +37 -18
- package/package.json +1 -1
package/dist/images.d.ts
CHANGED
|
@@ -11,11 +11,23 @@ export declare function imageHostsOf(header: string | null | undefined): Set<str
|
|
|
11
11
|
/** The header naming where the index's optimized images load from (0.28.0), such as https://images.findlane.dev.
|
|
12
12
|
* Without it they load from the API's own origin. */
|
|
13
13
|
export declare const imageOriginHeader = "x-findlane-image-origin";
|
|
14
|
+
/** Where the hosted API's optimized images load from, for a client told its image hosts before any answer (0.29.0). */
|
|
15
|
+
export declare const defaultImageOrigin = "https://images.findlane.dev";
|
|
14
16
|
/** The origin an `x-findlane-image-origin` header names, or null for none or one that isn't an http(s) origin. */
|
|
15
17
|
export declare function imageOriginOf(header: string | null | undefined): string | null;
|
|
16
18
|
/** Where an index's images loaded from on an earlier page, kept in this browser (localStorage), so the next page
|
|
17
19
|
* can open that connection before its first answer says; null when unknown or without storage. */
|
|
18
20
|
export declare function rememberedImageOrigin(id: string): string | null;
|
|
21
|
+
/** What an index's last answer in this browser said about its images (0.28.1): the hosts that may be resized and
|
|
22
|
+
* where from, so a page showing results it kept (a reload, Back) resizes them before its first search; null when
|
|
23
|
+
* unknown, off, or without storage. */
|
|
24
|
+
export declare function rememberedImages(id: string): {
|
|
25
|
+
hosts: Set<string>;
|
|
26
|
+
origin: string | null;
|
|
27
|
+
} | null;
|
|
28
|
+
/** Keeps what an answer said about an index's images for later pages (0.28.1), or forgets it once it has none. The
|
|
29
|
+
* 0.28.0 key with the origin alone is kept in step, for the widget. */
|
|
30
|
+
export declare function rememberImages(id: string, hosts: Set<string>, origin: string | null): void;
|
|
19
31
|
/** Keeps where an index's images load from for later pages, or forgets it (null) once the index has none. */
|
|
20
32
|
export declare function rememberImageOrigin(id: string, origin: string | null): void;
|
|
21
33
|
/** 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
|
@@ -19,6 +19,8 @@ export function imageHostsOf(header) {
|
|
|
19
19
|
/** The header naming where the index's optimized images load from (0.28.0), such as https://images.findlane.dev.
|
|
20
20
|
* Without it they load from the API's own origin. */
|
|
21
21
|
export const imageOriginHeader = "x-findlane-image-origin";
|
|
22
|
+
/** Where the hosted API's optimized images load from, for a client told its image hosts before any answer (0.29.0). */
|
|
23
|
+
export const defaultImageOrigin = "https://images.findlane.dev";
|
|
22
24
|
/** The origin an `x-findlane-image-origin` header names, or null for none or one that isn't an http(s) origin. */
|
|
23
25
|
export function imageOriginOf(header) {
|
|
24
26
|
if (!header)
|
|
@@ -32,6 +34,7 @@ export function imageOriginOf(header) {
|
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
const originKey = (id) => `findlane:image-origin:${id}`;
|
|
37
|
+
const imagesKey = (id) => `findlane:images:${id}`;
|
|
35
38
|
/** Where an index's images loaded from on an earlier page, kept in this browser (localStorage), so the next page
|
|
36
39
|
* can open that connection before its first answer says; null when unknown or without storage. */
|
|
37
40
|
export function rememberedImageOrigin(id) {
|
|
@@ -42,6 +45,33 @@ export function rememberedImageOrigin(id) {
|
|
|
42
45
|
return null;
|
|
43
46
|
}
|
|
44
47
|
}
|
|
48
|
+
/** What an index's last answer in this browser said about its images (0.28.1): the hosts that may be resized and
|
|
49
|
+
* where from, so a page showing results it kept (a reload, Back) resizes them before its first search; null when
|
|
50
|
+
* unknown, off, or without storage. */
|
|
51
|
+
export function rememberedImages(id) {
|
|
52
|
+
try {
|
|
53
|
+
const kept = JSON.parse(localStorage.getItem(imagesKey(id)) ?? "null");
|
|
54
|
+
const hosts = Array.isArray(kept?.hosts) ? imageHostsOf(kept.hosts.filter((host) => typeof host === "string").join(",")) : new Set();
|
|
55
|
+
return hosts.size ? { hosts, origin: imageOriginOf(typeof kept?.origin === "string" ? kept.origin : null) } : null;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Keeps what an answer said about an index's images for later pages (0.28.1), or forgets it once it has none. The
|
|
62
|
+
* 0.28.0 key with the origin alone is kept in step, for the widget. */
|
|
63
|
+
export function rememberImages(id, hosts, origin) {
|
|
64
|
+
try {
|
|
65
|
+
if (hosts.size)
|
|
66
|
+
localStorage.setItem(imagesKey(id), JSON.stringify({ hosts: [...hosts], origin }));
|
|
67
|
+
else
|
|
68
|
+
localStorage.removeItem(imagesKey(id));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// Storage that can't be used: later pages learn them from their first answer.
|
|
72
|
+
}
|
|
73
|
+
rememberImageOrigin(id, hosts.size ? origin : null);
|
|
74
|
+
}
|
|
45
75
|
/** Keeps where an index's images load from for later pages, or forgets it (null) once the index has none. */
|
|
46
76
|
export function rememberImageOrigin(id, origin) {
|
|
47
77
|
try {
|
package/dist/index.d.ts
CHANGED
|
@@ -405,6 +405,14 @@ export type BrowserClientOptions = {
|
|
|
405
405
|
* allowed sites. Or a function that fetches one: called for the first search, again shortly before the key
|
|
406
406
|
* expires, and once more if the API says it has. Make a new client when the person changes. */
|
|
407
407
|
signedKey?: string | (() => string | Promise<string>);
|
|
408
|
+
/** Optimized images (0.29.0): the hosts your records' images are on, when you know the index has optimized images on
|
|
409
|
+
* for them, such as ["cdn.shop.example"]. `imageUrl` then resizes them from the first render, before an answer has
|
|
410
|
+
* said which hosts the index allows. What an earlier answer said in this browser comes first, and from the first
|
|
411
|
+
* answer on the answers decide; a host the index doesn't allow loads as it is after one refused request. */
|
|
412
|
+
imageHosts?: string[];
|
|
413
|
+
/** Where those images load from until an answer names it (0.29.0): https://images.findlane.dev with the hosted API,
|
|
414
|
+
* else the API's own address. */
|
|
415
|
+
imageOrigin?: string;
|
|
408
416
|
};
|
|
409
417
|
export declare class SearchBrowserClient {
|
|
410
418
|
private readonly url;
|
|
@@ -424,8 +432,13 @@ export declare class SearchBrowserClient {
|
|
|
424
432
|
private imageHosts;
|
|
425
433
|
/** Where they load from (x-findlane-image-origin, 0.28.0); the API's own origin without one. */
|
|
426
434
|
private imageOrigin;
|
|
427
|
-
/**
|
|
428
|
-
|
|
435
|
+
/** What an earlier page's answers said about them in this browser (0.28.1), read once, used until this page's first
|
|
436
|
+
* answer; undefined until read. */
|
|
437
|
+
private knownImages;
|
|
438
|
+
/** Whether an answer has said, so what was remembered is no longer used. */
|
|
439
|
+
private answered;
|
|
440
|
+
/** The image hosts the page said it knows (options.imageHosts), and where they load from, until an answer says. */
|
|
441
|
+
private readonly givenImages;
|
|
429
442
|
constructor(options: BrowserClientOptions);
|
|
430
443
|
/** The signed key searches send: the one given, or the function's, asked for again 30 seconds before it
|
|
431
444
|
* expires, or at once when `fresh`. */
|
|
@@ -433,18 +446,22 @@ export declare class SearchBrowserClient {
|
|
|
433
446
|
private post;
|
|
434
447
|
private get;
|
|
435
448
|
/** Each answer says which hosts the index's images may be loaded from resized, and where from (0.28.0); refusals
|
|
436
|
-
* say nothing.
|
|
449
|
+
* say nothing. Both are kept for later pages (0.28.1), and the connection opened now, before the results' images ask. */
|
|
437
450
|
private noteImages;
|
|
438
|
-
private
|
|
439
|
-
/**
|
|
440
|
-
*
|
|
451
|
+
private remembered;
|
|
452
|
+
/** What's known of the index's images before this page's first answer: what an earlier answer said in this browser,
|
|
453
|
+
* else what the page said it knows (options.imageHosts). */
|
|
454
|
+
private beforeAnswer;
|
|
455
|
+
/** Opens the connection to where the index's images loaded from on an earlier page (or will, as the page said), with
|
|
456
|
+
* the first request or when a search box gets focus, so the results' first image doesn't wait for it. */
|
|
441
457
|
private warmImages;
|
|
442
458
|
/** Optimized images (0.27.0): a record image's address, resized to `width` pixels (rounded up to one of
|
|
443
459
|
* `imageWidths`) and sent as WebP from the address the API names (images.findlane.dev since 0.28.0; the API's own
|
|
444
460
|
* 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,
|
|
446
|
-
*
|
|
447
|
-
*
|
|
461
|
+
* Known from the first answer on; since 0.28.1, before it too from what an earlier page's answers said in this
|
|
462
|
+
* browser, so results a page kept (a reload, Back) are resized; since 0.29.0 from `imageHosts` when the page says it
|
|
463
|
+
* knows them, so a first visit's first render is resized too. Load them with `crossorigin="anonymous"`, which uses
|
|
464
|
+
* the connection opened for them ahead of need. React's `Image` does all of this. */
|
|
448
465
|
imageUrl(src: string, width: number): string;
|
|
449
466
|
/** Opens the connection to the API before the first search needs it, as a preconnect hint in the page
|
|
450
467
|
* 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,
|
|
1
|
+
import { defaultImageOrigin, 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,13 @@ 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
|
-
/**
|
|
294
|
-
|
|
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;
|
|
298
|
+
/** The image hosts the page said it knows (options.imageHosts), and where they load from, until an answer says. */
|
|
299
|
+
givenImages;
|
|
295
300
|
constructor(options) {
|
|
296
301
|
if (!/^pub_[a-f0-9]{32}$/.test(options.publicId))
|
|
297
302
|
throw new Error("A valid public index ID (pub_…) is required");
|
|
@@ -303,6 +308,9 @@ export class SearchBrowserClient {
|
|
|
303
308
|
this.test = testLabel(options.publicId);
|
|
304
309
|
this.events = eventSender(options.eventsUrl ?? `${this.url}/events`, options.fetch, options.events ?? true, options.visitorIds ? deviceIds(options.publicId) : null, this.test);
|
|
305
310
|
this.recentSearches = new RecentSearches(options.publicId);
|
|
311
|
+
const given = imageHostsOf(options.imageHosts?.join(","));
|
|
312
|
+
this.givenImages = given.size ? { hosts: given,
|
|
313
|
+
origin: imageOriginOf(options.imageOrigin) ?? (this.origin === new URL(defaultBaseUrl).origin ? defaultImageOrigin : null) } : null;
|
|
306
314
|
}
|
|
307
315
|
/** The signed key searches send: the one given, or the function's, asked for again 30 seconds before it
|
|
308
316
|
* expires, or at once when `fresh`. */
|
|
@@ -353,39 +361,50 @@ export class SearchBrowserClient {
|
|
|
353
361
|
return data;
|
|
354
362
|
}
|
|
355
363
|
/** Each answer says which hosts the index's images may be loaded from resized, and where from (0.28.0); refusals
|
|
356
|
-
* say nothing.
|
|
364
|
+
* say nothing. Both are kept for later pages (0.28.1), and the connection opened now, before the results' images ask. */
|
|
357
365
|
noteImages(response) {
|
|
358
366
|
if (!response.ok)
|
|
359
367
|
return;
|
|
368
|
+
this.answered = true;
|
|
360
369
|
this.imageHosts = imageHostsOf(response.headers?.get(imageHostsHeader));
|
|
361
370
|
this.imageOrigin = this.imageHosts.size ? imageOriginOf(response.headers?.get(imageOriginHeader)) : null;
|
|
362
371
|
if (this.imageOrigin)
|
|
363
372
|
preconnect(this.imageOrigin);
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
this.
|
|
373
|
+
const known = this.remembered();
|
|
374
|
+
const same = known ? known.origin === this.imageOrigin && known.hosts.size === this.imageHosts.size &&
|
|
375
|
+
[...this.imageHosts].every((host) => known.hosts.has(host)) : !this.imageHosts.size;
|
|
376
|
+
if (!same) {
|
|
377
|
+
rememberImages(this.publicId, this.imageHosts, this.imageOrigin);
|
|
378
|
+
this.knownImages = this.imageHosts.size ? { hosts: new Set(this.imageHosts), origin: this.imageOrigin } : null;
|
|
367
379
|
}
|
|
368
380
|
}
|
|
369
|
-
|
|
370
|
-
if (this.
|
|
371
|
-
this.
|
|
372
|
-
return this.
|
|
381
|
+
remembered() {
|
|
382
|
+
if (this.knownImages === undefined)
|
|
383
|
+
this.knownImages = rememberedImages(this.publicId);
|
|
384
|
+
return this.knownImages;
|
|
373
385
|
}
|
|
374
|
-
/**
|
|
375
|
-
*
|
|
386
|
+
/** What's known of the index's images before this page's first answer: what an earlier answer said in this browser,
|
|
387
|
+
* else what the page said it knows (options.imageHosts). */
|
|
388
|
+
beforeAnswer() {
|
|
389
|
+
return this.remembered() ?? this.givenImages;
|
|
390
|
+
}
|
|
391
|
+
/** Opens the connection to where the index's images loaded from on an earlier page (or will, as the page said), with
|
|
392
|
+
* the first request or when a search box gets focus, so the results' first image doesn't wait for it. */
|
|
376
393
|
warmImages() {
|
|
377
|
-
const origin = this.
|
|
394
|
+
const origin = this.beforeAnswer()?.origin;
|
|
378
395
|
if (origin)
|
|
379
396
|
preconnect(origin);
|
|
380
397
|
}
|
|
381
398
|
/** Optimized images (0.27.0): a record image's address, resized to `width` pixels (rounded up to one of
|
|
382
399
|
* `imageWidths`) and sent as WebP from the address the API names (images.findlane.dev since 0.28.0; the API's own
|
|
383
400
|
* 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,
|
|
385
|
-
*
|
|
386
|
-
*
|
|
401
|
+
* Known from the first answer on; since 0.28.1, before it too from what an earlier page's answers said in this
|
|
402
|
+
* browser, so results a page kept (a reload, Back) are resized; since 0.29.0 from `imageHosts` when the page says it
|
|
403
|
+
* knows them, so a first visit's first render is resized too. Load them with `crossorigin="anonymous"`, which uses
|
|
404
|
+
* the connection opened for them ahead of need. React's `Image` does all of this. */
|
|
387
405
|
imageUrl(src, width) {
|
|
388
|
-
|
|
406
|
+
const known = this.answered ? null : this.beforeAnswer();
|
|
407
|
+
return optimizedImageUrl((known ? known.origin : this.imageOrigin) ?? this.origin, this.publicId, known?.hosts ?? this.imageHosts, src, width) ?? src;
|
|
389
408
|
}
|
|
390
409
|
/** Opens the connection to the API before the first search needs it, as a preconnect hint in the page
|
|
391
410
|
* 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.
|
|
3
|
+
"version": "0.29.0",
|
|
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",
|