Skip to content

Commit da720ce

Browse files
committed
refactor(html): address review — move router constants, simplify remote config and Orama caches
- Move HOVER_DELAY, PAGE_LIFETIME, MAX_PAGES to constants.mjs as ROUTER_* exports (per review: use the constants file) - Simplify useRemoteConfig: there is only one remote config URL per site, so a single module-level promise replaces the URL-keyed Map - Simplify useOrama: one search client per visit replaces the URL-keyed Map; createClient renamed to getClient, module-level let holds the singleton Assisted-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 790f66b commit da720ce

4 files changed

Lines changed: 55 additions & 58 deletions

File tree

‎packages/react/src/html/constants.mjs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,12 @@ export const FONTS = [
9292
'open-sans-latin-wght-italic.woff2',
9393
'ibm-plex-mono-latin-400-normal.woff2',
9494
];
95+
96+
// How long a hovered link waits before its page is prefetched.
97+
export const ROUTER_HOVER_DELAY = 80;
98+
99+
// How long a fetched page is reused for, whether prefetched or visited.
100+
export const ROUTER_PAGE_LIFETIME = 5 * 60 * 1000;
101+
102+
// How many fetched pages are kept at once.
103+
export const ROUTER_MAX_PAGES = 10;

‎packages/react/src/html/ui/hooks/useOrama.mjs‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ import { useState, useEffect } from 'react';
44
import { relativeOrAbsolute } from '../utils/relativeOrAbsolute.mjs';
55

66
/**
7-
* Search clients by the URL of their data, so that the index is downloaded and
8-
* loaded once per visit rather than once per page navigated to.
7+
* The search client for this visit, shared across every page navigated to
8+
* client-side. The Orama index (several MB) is fetched once on the first
9+
* search and reused from then on.
910
*
10-
* @type {Map<string, import('@orama/orama').AnyOrama>}
11+
* @type {import('@orama/orama').AnyOrama | null}
1112
*/
12-
const clients = new Map();
13+
let client = null;
1314

1415
/**
15-
* Creates a search client whose data is fetched on its first search.
16+
* Returns the shared search client, creating it on the first call.
1617
*
17-
* @param {string} url - The search data's absolute URL: the client outlives
18-
* the page it was created on, which a relative URL would resolve against.
18+
* @param {string} url - Absolute URL of the search data, resolved once at
19+
* creation so the client outlives the page it was first used on.
1920
*/
20-
const createClient = url => {
21-
const db = create({
22-
schema: {},
23-
});
21+
const getClient = url => {
22+
if (client) {
23+
return client;
24+
}
2425

26+
const db = create({ schema: {} });
2527
let loaded;
2628

2729
// TODO(@avivkeller): Ask Orama to support this functionality natively
28-
/**
29-
* @param {any} options
30-
*/
30+
/** @param {any} options */
3131
db.search = async options => {
3232
loaded ??= fetch(url)
3333
.then(response => response.ok && response.json())
@@ -41,30 +41,28 @@ const createClient = url => {
4141
return search(db, options);
4242
};
4343

44-
return db;
44+
client = db;
45+
46+
return client;
4547
};
4648

4749
/**
48-
* Hook for initializing and managing Orama search database.
50+
* Hook for initializing and managing the Orama search client.
4951
* The search data is lazily fetched on the first search call.
5052
*
5153
* @param {string} pathname - The current page's path (e.g., '/api/fs')
5254
*/
5355
export default pathname => {
54-
const [client, setClient] = useState(null);
56+
const [db, setDb] = useState(null);
5557

5658
useEffect(() => {
5759
const url = new URL(
5860
relativeOrAbsolute('/orama-db.json', pathname),
5961
location.href
6062
).href;
6163

62-
if (!clients.has(url)) {
63-
clients.set(url, createClient(url));
64-
}
65-
66-
queueMicrotask(() => setClient(clients.get(url)));
64+
queueMicrotask(() => setDb(getClient(url)));
6765
}, [pathname]);
6866

69-
return client;
67+
return db;
7068
};

‎packages/react/src/html/ui/hooks/useRemoteConfig.mjs‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,30 @@ import { remoteConfigUrl } from '#theme/config';
1717
*/
1818

1919
/**
20-
* The remote configs fetched so far, by URL. Each is fetched once per visit and
21-
* shared by every island that reads it, on every page navigated to client-side:
22-
* islands hydrate as separate roots, so no context provider could span them.
20+
* The remote config fetched for this visit, shared by every island that reads
21+
* it. Islands hydrate as separate roots, so no context provider could span
22+
* them; module scope is the shared store.
2323
*
24-
* @type {Map<string, Promise<RemoteConfig | null>>}
24+
* @type {Promise<RemoteConfig | null> | null}
2525
*/
26-
const remoteConfigs = new Map();
26+
let remoteConfig = null;
2727

2828
/**
29-
* Fetches a remote config, unless it is already loaded or on its way.
29+
* Fetches the remote config, unless it is already loaded or on its way.
3030
*
31-
* @param {string} url
3231
* @returns {Promise<RemoteConfig | null>}
3332
*/
34-
const loadRemoteConfig = url => {
35-
if (!remoteConfigs.has(url)) {
36-
remoteConfigs.set(
37-
url,
38-
fetch(url)
39-
.then(response => response.json())
40-
.catch(() => {
41-
// Not kept, so that the next island to mount tries again
42-
remoteConfigs.delete(url);
33+
const loadRemoteConfig = () => {
34+
remoteConfig ??= fetch(remoteConfigUrl)
35+
.then(response => response.json())
36+
.catch(() => {
37+
// Not kept, so that the next island to mount tries again
38+
remoteConfig = null;
4339

44-
return null;
45-
})
46-
);
47-
}
40+
return null;
41+
});
4842

49-
return remoteConfigs.get(url);
43+
return remoteConfig;
5044
};
5145

5246
/**
@@ -70,7 +64,7 @@ export default () => {
7064

7165
let mounted = true;
7266

73-
loadRemoteConfig(remoteConfigUrl).then(loaded => {
67+
loadRemoteConfig().then(loaded => {
7468
if (mounted) {
7569
setConfig(loaded);
7670
}

‎packages/react/src/html/ui/router.mjs‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@
1818
* navigation in a browser without the Navigation API.
1919
*/
2020

21+
import {
22+
ROUTER_HOVER_DELAY,
23+
ROUTER_MAX_PAGES,
24+
ROUTER_PAGE_LIFETIME,
25+
} from '../constants.mjs';
26+
2127
/**
2228
* @typedef {{ url: string, html: string }} Page A fetched page: its final
2329
* URL, after redirects, and its markup.
2430
*/
2531

26-
// How long a hovered link waits before its page is prefetched, so that links
27-
// the pointer merely crosses on its way somewhere else are skipped.
28-
const HOVER_DELAY = 80;
29-
30-
// How long a fetched page is reused for, whether it was prefetched or visited.
31-
const PAGE_LIFETIME = 5 * 60 * 1000;
32-
33-
// How many fetched pages are kept at once.
34-
const MAX_PAGES = 10;
35-
3632
// The `<head>` elements that belong to the page rather than to the site, and
3733
// are replaced with it: `<meta>` tags (`og:title`) and the links that are not
3834
// resources (`canonical`). Scripts and stylesheets run and apply once.
@@ -177,9 +173,9 @@ export const startRouter = ({ unmount, islands }) => {
177173
.catch(() => null);
178174

179175
pages.delete(url);
180-
pages.set(url, { page, expires: Date.now() + PAGE_LIFETIME });
176+
pages.set(url, { page, expires: Date.now() + ROUTER_PAGE_LIFETIME });
181177

182-
if (pages.size > MAX_PAGES) {
178+
if (pages.size > ROUTER_MAX_PAGES) {
183179
pages.delete(pages.keys().next().value);
184180
}
185181

@@ -324,7 +320,7 @@ export const startRouter = ({ unmount, islands }) => {
324320
getLinkedPage(target);
325321

326322
if (url) {
327-
hovered = setTimeout(loadPage, HOVER_DELAY, url);
323+
hovered = setTimeout(loadPage, ROUTER_HOVER_DELAY, url);
328324
}
329325
},
330326
{ passive: true }

0 commit comments

Comments
 (0)