@jasonshimmy/vite-plugin-cer-app 0.22.0 → 0.23.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/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
+ ## [v0.23.1] - 2026-04-17
5
+
6
+ - fix: fix useContentSearch loading state, debounce, and automatic retry on fetch failure (4d0207a)
7
+
8
+ ## [v0.23.0] - 2026-04-17
9
+
10
+ - feat: enhance useContentSearch with loading state and update related tests (d1cee0e)
11
+
4
12
  ## [v0.22.0] - 2026-04-16
5
13
 
6
14
  - feat: add customColors support for jitCss configuration and implement related tests (b8061ef)
package/commits.txt CHANGED
@@ -1 +1 @@
1
- - feat: add customColors support for jitCss configuration and implement related tests (b8061ef)
1
+ - fix: fix useContentSearch loading state, debounce, and automatic retry on fetch failure (4d0207a)
@@ -5,6 +5,9 @@ import type { ContentSearchResult } from '../../types/content.js';
5
5
  * Returns the same Promise on repeated calls — the index is built at most once
6
6
  * per session regardless of how many search components are mounted.
7
7
  *
8
+ * If the fetch fails the singleton is cleared so the next search attempt
9
+ * retries automatically (no page reload required after a transient error).
10
+ *
8
11
  * @internal Exported for unit testing only.
9
12
  */
10
13
  export declare function loadIndex(): Promise<unknown>;
@@ -13,6 +16,8 @@ export declare function resetIndexSingleton(): void;
13
16
  export interface UseContentSearchReturn {
14
17
  query: ReactiveState<string>;
15
18
  results: ReactiveState<ContentSearchResult[]>;
19
+ /** `true` from the moment the user starts typing until results (or an error) arrive. `false` when the query is empty or the search is complete. */
20
+ loading: ReactiveState<boolean>;
16
21
  }
17
22
  /**
18
23
  * Full-text content search composable.
@@ -21,8 +26,10 @@ export interface UseContentSearchReturn {
21
26
  * `/_content/search-index.json`. Both MiniSearch and the index are loaded via
22
27
  * dynamic import — neither is in the app bundle.
23
28
  *
24
- * Searches `title` and `description` fields. Results are empty until at least
25
- * 2 characters are entered.
29
+ * Searches `title` and `description` fields. Input is debounced (200 ms) so
30
+ * the index is not queried on every keystroke. `loading` becomes `true` as soon
31
+ * as the user starts typing and returns to `false` once results arrive. Results
32
+ * are empty when the query is empty.
26
33
  *
27
34
  * **SSR note**: search is always client-side. In SSR mode the component renders
28
35
  * with empty results and hydrates on mount.
@@ -30,10 +37,11 @@ export interface UseContentSearchReturn {
30
37
  * @example
31
38
  * ```ts
32
39
  * component('site-search', () => {
33
- * const { query, results } = useContentSearch()
40
+ * const { query, results, loading } = useContentSearch()
34
41
  *
35
42
  * return html`
36
43
  * <input type="search" :model="${query}" placeholder="Search…" />
44
+ * ${loading.value ? html`<p>Searching…</p>` : ''}
37
45
  * ${when(results.value.length > 0, () => html`
38
46
  * <ul>
39
47
  * ${each(results.value, r => html`
@@ -1 +1 @@
1
- {"version":3,"file":"use-content-search.d.ts","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAA;AACzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AASjE;;;;;;GAMG;AACH,wBAAsB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAiBlD;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAID,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IAC5B,OAAO,EAAE,aAAa,CAAC,mBAAmB,EAAE,CAAC,CAAA;CAC9C;AAmCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAEzD"}
1
+ {"version":3,"file":"use-content-search.d.ts","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAA;AACzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AASjE;;;;;;;;;GASG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAuB5C;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AA+BD,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IAC5B,OAAO,EAAE,aAAa,CAAC,mBAAmB,EAAE,CAAC,CAAA;IAC7C,mJAAmJ;IACnJ,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAChC;AA4ED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAEzD"}
@@ -1,4 +1,4 @@
1
- import { createComposable, ref, useOnConnected, watch, } from '@jasonshimmy/custom-elements-runtime';
1
+ import { createComposable, getCurrentComponentContext, ref, useOnConnected, useOnDisconnected, watch, } from '@jasonshimmy/custom-elements-runtime';
2
2
  import { contentSearchIndexUrl } from '../content/client.js';
3
3
  // ─── Module-level singleton ───────────────────────────────────────────────────
4
4
  // The MiniSearch instance is built lazily on first search from the manifest.
@@ -9,12 +9,15 @@ let _indexPromise = null;
9
9
  * Returns the same Promise on repeated calls — the index is built at most once
10
10
  * per session regardless of how many search components are mounted.
11
11
  *
12
+ * If the fetch fails the singleton is cleared so the next search attempt
13
+ * retries automatically (no page reload required after a transient error).
14
+ *
12
15
  * @internal Exported for unit testing only.
13
16
  */
14
- export async function loadIndex() {
17
+ export function loadIndex() {
15
18
  if (_indexPromise)
16
19
  return _indexPromise;
17
- _indexPromise = (async () => {
20
+ const attempt = (async () => {
18
21
  const [{ default: MiniSearch }, raw] = await Promise.all([
19
22
  import('minisearch'),
20
23
  fetch(contentSearchIndexUrl()).then((r) => {
@@ -29,40 +32,99 @@ export async function loadIndex() {
29
32
  idField: '_path',
30
33
  });
31
34
  })();
32
- return _indexPromise;
35
+ _indexPromise = attempt;
36
+ // Clear the singleton on failure so the next call retries the fetch.
37
+ // The === guard ensures a newer concurrent attempt is not accidentally cleared.
38
+ attempt.catch(() => {
39
+ if (_indexPromise === attempt)
40
+ _indexPromise = null;
41
+ });
42
+ return attempt;
33
43
  }
34
44
  /** Resets the module-level singleton. Used in tests only. @internal */
35
45
  export function resetIndexSingleton() {
36
46
  _indexPromise = null;
37
47
  }
48
+ const _STATE_KEY = '_cerSearchDebounce';
49
+ function getDebounceState(ctx) {
50
+ if (!Object.prototype.hasOwnProperty.call(ctx, _STATE_KEY)) {
51
+ Object.defineProperty(ctx, _STATE_KEY, {
52
+ value: { seq: 0, timer: null },
53
+ writable: false, // object ref is fixed; its properties are still mutable
54
+ enumerable: false, // invisible to the reactive proxy set-trap
55
+ configurable: false,
56
+ });
57
+ }
58
+ return ctx[_STATE_KEY];
59
+ }
38
60
  const _factory = createComposable(() => {
39
61
  const query = ref('');
40
62
  const results = ref([]);
41
- // Pre-warm index on mount
63
+ const loading = ref(false);
64
+ // Debounce state lives on the component context, not in local render-body variables.
65
+ // Local variables are re-created on every re-render; the context object is stable
66
+ // for the lifetime of the element. Storing state here lets the render-body
67
+ // watch() (see below) pick up the same timer and sequence counter across re-renders
68
+ // without the watcher accumulation that occurs when watch() is placed inside
69
+ // useOnConnected() (which runs once per mount but is not registered for cleanup
70
+ // by the reactive system, leaking watchers on every disconnect + reconnect cycle).
71
+ const state = getDebounceState(getCurrentComponentContext());
72
+ // Pre-warm the index on first mount so the first real search is faster.
42
73
  useOnConnected(() => {
43
74
  loadIndex().catch(() => { });
44
75
  });
45
- // Monotonic counter to discard stale async results
46
- let _seq = 0;
47
- watch(query, async (q) => {
48
- const seq = ++_seq;
49
- if (!q || q.length < 2) {
50
- results.value = [];
51
- return;
76
+ // Cancel any in-flight debounce on unmount so stale async work doesn't land
77
+ // after the component is gone.
78
+ useOnDisconnected(() => {
79
+ if (state.timer !== null) {
80
+ clearTimeout(state.timer);
81
+ state.timer = null;
52
82
  }
53
- try {
54
- const index = await loadIndex();
55
- if (seq !== _seq)
56
- return; // stale a newer query is in flight
57
- results.value = index.search(q, { prefix: true });
83
+ state.seq++; // discard any in-flight async search
84
+ loading.value = false;
85
+ });
86
+ // watch() is in the render body so the reactive system registers it under the
87
+ // current component and tears it down automatically on re-render and disconnect.
88
+ // The mutable state (seq / timer) lives on the context (above) and persists
89
+ // across re-renders — new watcher instances see the same timer and counter,
90
+ // which is what makes debounce cancellation correct even after a re-render.
91
+ watch(query, (q) => {
92
+ if (state.timer !== null) {
93
+ clearTimeout(state.timer);
94
+ state.timer = null;
58
95
  }
59
- catch {
60
- if (seq !== _seq)
61
- return;
96
+ if (!q) {
97
+ // Increment seq so any in-flight async search is discarded when it resolves
98
+ state.seq++;
99
+ loading.value = false;
62
100
  results.value = [];
101
+ return;
63
102
  }
103
+ // Signal loading immediately so the UI can respond before the debounce fires
104
+ loading.value = true;
105
+ state.timer = setTimeout(async () => {
106
+ state.timer = null;
107
+ const seq = ++state.seq;
108
+ try {
109
+ const index = await loadIndex();
110
+ if (seq !== state.seq)
111
+ return; // stale — a newer query is in flight
112
+ results.value = index.search(q, { prefix: true });
113
+ }
114
+ catch {
115
+ if (seq !== state.seq)
116
+ return;
117
+ results.value = [];
118
+ }
119
+ finally {
120
+ // Only clear loading for the most recent search; a newer in-flight search
121
+ // keeps loading=true until it settles.
122
+ if (seq === state.seq)
123
+ loading.value = false;
124
+ }
125
+ }, 200);
64
126
  });
65
- return { query, results };
127
+ return { query, results, loading };
66
128
  });
67
129
  /**
68
130
  * Full-text content search composable.
@@ -71,8 +133,10 @@ const _factory = createComposable(() => {
71
133
  * `/_content/search-index.json`. Both MiniSearch and the index are loaded via
72
134
  * dynamic import — neither is in the app bundle.
73
135
  *
74
- * Searches `title` and `description` fields. Results are empty until at least
75
- * 2 characters are entered.
136
+ * Searches `title` and `description` fields. Input is debounced (200 ms) so
137
+ * the index is not queried on every keystroke. `loading` becomes `true` as soon
138
+ * as the user starts typing and returns to `false` once results arrive. Results
139
+ * are empty when the query is empty.
76
140
  *
77
141
  * **SSR note**: search is always client-side. In SSR mode the component renders
78
142
  * with empty results and hydrates on mount.
@@ -80,10 +144,11 @@ const _factory = createComposable(() => {
80
144
  * @example
81
145
  * ```ts
82
146
  * component('site-search', () => {
83
- * const { query, results } = useContentSearch()
147
+ * const { query, results, loading } = useContentSearch()
84
148
  *
85
149
  * return html`
86
150
  * <input type="search" :model="${query}" placeholder="Search…" />
151
+ * ${loading.value ? html`<p>Searching…</p>` : ''}
87
152
  * ${when(results.value.length > 0, () => html`
88
153
  * <ul>
89
154
  * ${each(results.value, r => html`
@@ -1 +1 @@
1
- {"version":3,"file":"use-content-search.js","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,GAAG,EACH,cAAc,EACd,KAAK,GACN,MAAM,sCAAsC,CAAA;AAG7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,iFAAiF;AAEjF,6EAA6E;AAC7E,iFAAiF;AACjF,IAAI,aAAa,GAA4B,IAAI,CAAA;AAEjD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS;IAC7B,IAAI,aAAa;QAAE,OAAO,aAAa,CAAA;IACvC,aAAa,GAAG,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,GAAG,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACvD,MAAM,CAAC,YAAY,CAAC;YACpB,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;gBACxC,IAAI,CAAC,CAAC,CAAC,EAAE;oBAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAA;gBACvE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAA;YACjB,CAAC,CA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C;YACP,IAAI,GAAG,KAAK,IAAI;gBAAE,OAAM;YACxB,OAAO,CAAC,KAAK,GAAG,EAAE,CAAA;QACpB,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAA;AAC3B,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,QAAQ,EAAE,CAAA;AACnB,CAAC"}
1
+ {"version":3,"file":"use-content-search.js","sourceRoot":"","sources":["../../../src/runtime/composables/use-content-search.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,GAAG,EACH,cAAc,EACd,iBAAiB,EACjB,KAAK,GACN,MAAM,sCAAsC,CAAA;AAG7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,iFAAiF;AAEjF,6EAA6E;AAC7E,iFAAiF;AACjF,IAAI,aAAa,GAA4B,IAAI,CAAA;AAEjD;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS;IACvB,IAAI,aAAa;QAAE,OAAO,aAAa,CAAA;IACvC,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,GAAG,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACvD,MAAM,CAAC,YAAY,CAAC;YACpB,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;gBACxC,IAAI,CAAC,CAAC,CAAC,EAAE;oBAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAA;gBACvE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAA;YACjB,CAAC,CAAC;SACH,CAAC,CAAA;QACF,OAAO,UAAU,CAAC,QAAQ,CAAC,GAAG,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CAAC,KAAK,GAAG,IAAI,CAAA;YAClB,MAAM,GAAG,GAAG,EAAE,KAAK,CAAC,GAAG,CAAA;YAEvB,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,MAAM,SAAS,EAA+E,CAAA;gBAC5G,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAM,CAAC,qCAAqC;gBACnE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAA0B,CAAA;YAC5E,CAAC;YAAC,MAAM,CAAC;gBACP,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAM;gBAC7B,OAAO,CAAC,KAAK,GAAG,EAAE,CAAA;YACpB,CAAC;oBAAS,CAAC;gBACT,0EAA0E;gBAC1E,uCAAuC;gBACvC,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG;oBAAE,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;YAC9C,CAAC;QACH,CAAC,EAAE,GAAG,CAAC,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAA;AACpC,CAAC,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,QAAQ,EAAE,CAAA;AACnB,CAAC"}
@@ -868,16 +868,17 @@ import { queryContent } from '@jasonshimmy/vite-plugin-cer-app/composables'
868
868
 
869
869
  ### `useContentSearch()`
870
870
 
871
- Reactive full-text search over the content layer. Loads the MiniSearch index lazily on first use. Returns `query` and `results` refs that update reactively as the user types.
871
+ Reactive full-text search over the content layer. Loads the MiniSearch index lazily on first use. Returns `query`, `results`, and `loading` refs that update reactively as the user types.
872
872
 
873
873
  Requires `content: {}` in `cer.config.ts`. See [content.md](./content.md) for full documentation.
874
874
 
875
875
  ```ts
876
876
  component('page-search', () => {
877
- const { query, results } = useContentSearch()
877
+ const { query, results, loading } = useContentSearch()
878
878
 
879
879
  return html`
880
880
  <input type="search" :model="${query}" placeholder="Search…" />
881
+ ${loading.value ? html`<p>Searching…</p>` : ''}
881
882
  <ul>
882
883
  ${each(results.value, r => html`
883
884
  <li><a :href="${r._path}">${r.title}</a></li>
@@ -893,10 +894,11 @@ component('page-search', () => {
893
894
  interface UseContentSearchReturn {
894
895
  query: Ref<string> // bind with :model
895
896
  results: Ref<ContentSearchResult[]> // reactive search results
897
+ loading: Ref<boolean> // true while debounce is pending or search is in flight
896
898
  }
897
899
  ```
898
900
 
899
- Search activates when `query.value.length >= 2`. MiniSearch is loaded once and cached for the lifetime of the page. Searched fields are `title` and `description`.
901
+ Search is debounced (200 ms) so the index is not queried on every keystroke. `loading` is set to `true` as soon as the user starts typing and returns to `false` once results arrive or the query is cleared. An empty query clears results immediately and cancels any in-flight search. MiniSearch is loaded once and cached for the lifetime of the page. Searched fields are `title` and `description`.
900
902
 
901
903
  If you need it outside auto-imported directories:
902
904
 
package/docs/content.md CHANGED
@@ -473,18 +473,19 @@ Important behavior notes:
473
473
 
474
474
  **Auto-imported** in pages, layouts, and components.
475
475
 
476
- Returns reactive `query` and `results` refs. The MiniSearch index is loaded once on the client (lazily, the first time the composable is used). Search is debounce-free results update synchronously on each keypress, but the initial index fetch is async.
476
+ Returns reactive `query`, `results`, and `loading` refs. The MiniSearch index is loaded lazily the first time the component mounts (pre-warmed via `useOnConnected`) and cached for the lifetime of the session. Input is debounced (200 ms) so the index is not queried on every keystroke.
477
477
 
478
478
  ```ts
479
- const { query, results } = useContentSearch()
479
+ const { query, results, loading } = useContentSearch()
480
480
  ```
481
481
 
482
482
  ### Return value
483
483
 
484
484
  ```ts
485
485
  interface UseContentSearchReturn {
486
- query: Ref<string> // bind to an <input> value
486
+ query: Ref<string> // bind with :model or @input
487
487
  results: Ref<ContentSearchResult[]> // reactive search results
488
+ loading: Ref<boolean> // true from first keystroke until results arrive
488
489
  }
489
490
  ```
490
491
 
@@ -492,10 +493,11 @@ interface UseContentSearchReturn {
492
493
 
493
494
  ```ts
494
495
  component('page-search', () => {
495
- const { query, results } = useContentSearch()
496
+ const { query, results, loading } = useContentSearch()
496
497
 
497
498
  return html`
498
499
  <input type="search" :model="${query}" placeholder="Search…" />
500
+ ${loading.value ? html`<p>Searching…</p>` : ''}
499
501
  <ul>
500
502
  ${each(results.value, r => html`
501
503
  <li><a :href="${r._path}">${r.title}</a></li>
@@ -505,7 +507,7 @@ component('page-search', () => {
505
507
  })
506
508
  ```
507
509
 
508
- Search activates when `query.value.length >= 2`. Empty or single-character queries return an empty array.
510
+ `loading` becomes `true` as soon as the user types anything and returns to `false` once results arrive or the query is cleared. An empty query clears results immediately without waiting for the debounce. Search is always client-side in SSR mode the component renders with empty results and hydrates on mount.
509
511
 
510
512
  ### Searched fields
511
513
 
@@ -240,14 +240,14 @@ describe('Content search — useContentSearch()', () => {
240
240
  cy.get('[data-cy=content-search-input]').should('exist')
241
241
  })
242
242
 
243
- it('shows no results before typing 2 chars', () => {
243
+ it('shows results after typing a single character', () => {
244
244
  cy.visit('/content-search')
245
245
  cy.wait('@searchIndex')
246
246
  setSearchQuery('H')
247
- cy.get('[data-cy=content-search-result]').should('not.exist')
247
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('have.length.at.least', 1)
248
248
  })
249
249
 
250
- it('shows results after typing a 2-char query', () => {
250
+ it('shows results after typing a multi-char query', () => {
251
251
  cy.visit('/content-search')
252
252
  cy.wait('@searchIndex')
253
253
  setSearchQuery('He')
@@ -275,6 +275,26 @@ describe('Content search — useContentSearch()', () => {
275
275
  cy.get('[data-cy=content-search-result]', { timeout: 8000 }).first().should('have.attr', 'data-path')
276
276
  })
277
277
 
278
+ it('shows loading indicator while search is in flight', () => {
279
+ cy.visit('/content-search')
280
+ cy.wait('@searchIndex')
281
+ setSearchQuery('Hello')
282
+ // loading indicator appears immediately after typing
283
+ cy.get('[data-cy=content-search-loading]').should('exist')
284
+ // loading indicator disappears once results arrive
285
+ cy.get('[data-cy=content-search-loading]', { timeout: 8000 }).should('not.exist')
286
+ cy.get('[data-cy=content-search-result]').should('have.length.at.least', 1)
287
+ })
288
+
289
+ it('clears loading indicator when query is cleared', () => {
290
+ cy.visit('/content-search')
291
+ cy.wait('@searchIndex')
292
+ setSearchQuery('Hello')
293
+ cy.get('[data-cy=content-search-loading]').should('exist')
294
+ setSearchQuery('')
295
+ cy.get('[data-cy=content-search-loading]').should('not.exist')
296
+ })
297
+
278
298
  it('clearing query clears results', () => {
279
299
  cy.visit('/content-search')
280
300
  cy.wait('@searchIndex')
@@ -283,6 +303,63 @@ describe('Content search — useContentSearch()', () => {
283
303
  setSearchQuery('')
284
304
  cy.get('[data-cy=content-search-result]').should('not.exist')
285
305
  })
306
+
307
+ it('shows no-results message for an unmatched query', () => {
308
+ // Use a query with no underscores — MiniSearch tokenizes on \p{P} which
309
+ // includes underscores, splitting e.g. "foo_bar" into two tokens that can
310
+ // accidentally match real content via OR semantics.
311
+ cy.visit('/content-search')
312
+ cy.wait('@searchIndex')
313
+ setSearchQuery('zzznomatch')
314
+ // Wait for the empty state — implicitly waits for loading to clear first
315
+ cy.get('[data-cy=content-search-empty]', { timeout: 8000 }).should('exist')
316
+ cy.get('[data-cy=content-search-result]').should('not.exist')
317
+ cy.get('[data-cy=content-search-loading]').should('not.exist')
318
+ })
319
+
320
+ it('sequential search: search → clear → search again returns correct results', () => {
321
+ cy.visit('/content-search')
322
+ cy.wait('@searchIndex')
323
+
324
+ // First search
325
+ setSearchQuery('Hello')
326
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Hello World')
327
+
328
+ // Clear — results gone
329
+ setSearchQuery('')
330
+ cy.get('[data-cy=content-search-result]').should('not.exist')
331
+ cy.get('[data-cy=content-search-empty]').should('not.exist')
332
+
333
+ // Second search with a different term
334
+ setSearchQuery('Getting')
335
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Getting Started')
336
+ // First search result must not bleed through
337
+ cy.get('[data-cy=content-search-result]').each(($el) => {
338
+ expect($el.text()).not.to.include('Hello World')
339
+ })
340
+ })
341
+
342
+ it('rapid typing triggers only one search request (debounce)', () => {
343
+ cy.visit('/content-search')
344
+ cy.wait('@searchIndex')
345
+
346
+ // Type characters in quick succession — each triggers a new watch callback
347
+ // but only the last one should produce a network request after 200 ms.
348
+ cy.intercept('GET', '/_content/search-index.json').as('secondIndex')
349
+ setSearchQuery('G')
350
+ setSearchQuery('Ge')
351
+ setSearchQuery('Get')
352
+ setSearchQuery('Gett')
353
+ setSearchQuery('Getti')
354
+ setSearchQuery('Getting')
355
+
356
+ // Results arrive for the final query
357
+ cy.get('[data-cy=content-search-result]', { timeout: 8000 }).should('contain', 'Getting Started')
358
+
359
+ // The search index is cached after the first pre-warm fetch (singleton), so
360
+ // no additional network request should occur for these follow-on searches.
361
+ cy.get('@secondIndex.all').should('have.length', 0)
362
+ })
286
363
  })
287
364
 
288
365
  // ─── /content-fallback ────────────────────────────────────────────────────────
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Content search page — exercises `useContentSearch()`.
3
- * Typing 2+ characters into the search box triggers MiniSearch results.
3
+ * Typing into the search box triggers MiniSearch results (debounced).
4
4
  * Route: /content-search
5
5
  */
6
6
 
7
7
  component('page-content-search', () => {
8
8
  useHead({ title: 'Content Search — Kitchen Sink' })
9
9
 
10
- const { query, results } = useContentSearch()
10
+ const { query, results, loading } = useContentSearch()
11
11
 
12
12
  return html`
13
13
  <div>
@@ -19,6 +19,7 @@ component('page-content-search', () => {
19
19
  .value="${query.value}"
20
20
  @input="${(e: Event) => { query.value = (e.target as HTMLInputElement).value }}"
21
21
  />
22
+ ${loading.value ? html`<p data-cy="content-search-loading">Searching…</p>` : ''}
22
23
  <ul data-cy="content-search-results">
23
24
  ${results.value.map(r => html`
24
25
  <li data-cy="content-search-result" data-path="${r._path}">
@@ -27,7 +28,7 @@ component('page-content-search', () => {
27
28
  </li>
28
29
  `)}
29
30
  </ul>
30
- ${results.value.length === 0 && query.value.length >= 2 ? html`
31
+ ${results.value.length === 0 && query.value.length > 0 && !loading.value ? html`
31
32
  <p data-cy="content-search-empty">No results for "${query.value}".</p>
32
33
  ` : ''}
33
34
  </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jasonshimmy/vite-plugin-cer-app",
3
- "version": "0.22.0",
3
+ "version": "0.23.1",
4
4
  "description": "Nuxt-style meta-framework for @jasonshimmy/custom-elements-runtime",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -0,0 +1,405 @@
1
+ /**
2
+ * Tests for the useContentSearch() composable.
3
+ *
4
+ * Covers:
5
+ * - Initial state — empty query, empty results, loading=false
6
+ * - loading state — true immediately on typing, false after results arrive
7
+ * - Debounce timing — results are withheld until 200 ms after the last keystroke
8
+ * - Timer cancellation — a new query before 200 ms resets the debounce clock
9
+ * - Empty-query path — clears loading + results immediately, cancels pending timer
10
+ * - Disconnect cleanup — pending timer is cancelled and loading reset on unmount
11
+ * - Results content — correct items returned for each query
12
+ *
13
+ * @jasonshimmy/custom-elements-runtime is mocked so these tests run without a
14
+ * real DOM or component context. The watch() callback is available immediately
15
+ * after useContentSearch() is called (it is registered during the render-body
16
+ * call, not inside useOnConnected), so no triggerConnected() is needed before
17
+ * simulateType().
18
+ *
19
+ * Note: verifying that debounced input triggers exactly one loadIndex() call
20
+ * is an end-to-end concern exercised in content.cy.ts — the seq-stale guard
21
+ * discards duplicate search results even when debounce is absent, making the
22
+ * count indistinguishable at the unit level.
23
+ */
24
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
25
+ import { buildSearchIndex } from '../../plugin/content/search.js'
26
+ import type { ContentItem } from '../../types/content.js'
27
+
28
+ // ─── Sample content ───────────────────────────────────────────────────────────
29
+
30
+ const ITEMS: ContentItem[] = [
31
+ {
32
+ _path: '/blog/hello',
33
+ _file: 'blog/hello.md',
34
+ _type: 'markdown',
35
+ title: 'Hello World',
36
+ description: 'A post about hello',
37
+ body: '<p>Hello</p>',
38
+ toc: [],
39
+ },
40
+ {
41
+ _path: '/docs/start',
42
+ _file: 'docs/start.md',
43
+ _type: 'markdown',
44
+ title: 'Getting Started',
45
+ description: 'How to get started',
46
+ body: '<p>Start</p>',
47
+ toc: [],
48
+ },
49
+ ]
50
+
51
+ // ─── Runtime mock ─────────────────────────────────────────────────────────────
52
+ //
53
+ // The mock simulates just enough of the runtime for these unit tests:
54
+ // _currentMockContext — fresh plain object each test; receives _cerSearchDebounce
55
+ // _connectedCallbacks — useOnConnected callbacks (pre-warm only in new implementation)
56
+ // _disconnectedCallbacks — useOnDisconnected callbacks (timer cleanup)
57
+ // _watchCallback — the single watch(query, cb) handler registered during render
58
+
59
+ let _currentMockContext: Record<string, unknown> = {}
60
+ let _connectedCallbacks: Array<() => void> = []
61
+ let _disconnectedCallbacks: Array<() => void> = []
62
+ let _watchCallback: ((q: string) => void) | null = null
63
+
64
+ vi.mock('@jasonshimmy/custom-elements-runtime', () => ({
65
+ // Run the factory immediately; getCurrentComponentContext() returns the mock context.
66
+ createComposable: (fn: () => unknown) => () => fn(),
67
+ // Minimal reactive ref: plain object with getter/setter.
68
+ ref: (initial: unknown) => {
69
+ let _val = initial
70
+ return {
71
+ get value() { return _val },
72
+ set value(v: unknown) { _val = v },
73
+ }
74
+ },
75
+ // Capture the single watch() callback registered by the composable.
76
+ watch: (_state: unknown, cb: (val: string) => void) => {
77
+ _watchCallback = cb
78
+ },
79
+ // Capture useOnConnected callbacks for manual triggering (pre-warm only).
80
+ useOnConnected: (cb: () => void) => {
81
+ _connectedCallbacks.push(cb)
82
+ },
83
+ // Capture useOnDisconnected callbacks for manual triggering.
84
+ useOnDisconnected: (cb: () => void) => {
85
+ _disconnectedCallbacks.push(cb)
86
+ },
87
+ // Return the fresh mock context so getDebounceState() can attach _cerSearchDebounce.
88
+ getCurrentComponentContext: () => _currentMockContext,
89
+ }))
90
+
91
+ // Static imports resolve AFTER vi.mock hoisting, so the mock is in place when
92
+ // use-content-search.js's module-level createComposable() call executes.
93
+ import { useContentSearch, resetIndexSingleton } from '../../runtime/composables/use-content-search.js'
94
+ import type { UseContentSearchReturn } from '../../runtime/composables/use-content-search.js'
95
+
96
+ // ─── Helpers ──────────────────────────────────────────────────────────────────
97
+
98
+ /** Flush all useOnConnected callbacks (simulates component mount). */
99
+ function triggerConnected(): void {
100
+ for (const cb of _connectedCallbacks) cb()
101
+ }
102
+
103
+ /** Flush all useOnDisconnected callbacks (simulates component unmount). */
104
+ function triggerDisconnected(): void {
105
+ for (const cb of _disconnectedCallbacks) cb()
106
+ }
107
+
108
+ /** Simulate the user typing a value into the search input. */
109
+ function simulateType(q: string): void {
110
+ if (!_watchCallback) throw new Error('watch callback not registered — did you call useContentSearch()?')
111
+ _watchCallback(q)
112
+ }
113
+
114
+ // Convenience typed accessor
115
+ type Ref<T> = { value: T }
116
+
117
+ // ─── Tests ────────────────────────────────────────────────────────────────────
118
+
119
+ describe('useContentSearch() composable', () => {
120
+ let result: UseContentSearchReturn
121
+ let originalFetch: typeof globalThis.fetch
122
+
123
+ beforeEach(async () => {
124
+ // Fresh context object for each test so _cerSearchDebounce doesn't bleed
125
+ _currentMockContext = {}
126
+ _connectedCallbacks = []
127
+ _disconnectedCallbacks = []
128
+ _watchCallback = null
129
+
130
+ // Each test gets a fresh index singleton so loadIndex() re-fetches.
131
+ resetIndexSingleton()
132
+
133
+ originalFetch = globalThis.fetch
134
+ const indexJson = buildSearchIndex(ITEMS)
135
+ globalThis.fetch = vi.fn().mockResolvedValue({
136
+ ok: true,
137
+ text: () => Promise.resolve(indexJson),
138
+ } as unknown as Response)
139
+
140
+ // Calling useContentSearch() runs the factory, which calls watch() and
141
+ // registers useOnConnected/useOnDisconnected callbacks synchronously.
142
+ result = useContentSearch()
143
+ })
144
+
145
+ afterEach(() => {
146
+ globalThis.fetch = originalFetch
147
+ vi.useRealTimers()
148
+ })
149
+
150
+ // ─── Shape ─────────────────────────────────────────────────────────────────
151
+
152
+ it('returns query, results, and loading refs', () => {
153
+ expect(result).toHaveProperty('query')
154
+ expect(result).toHaveProperty('results')
155
+ expect(result).toHaveProperty('loading')
156
+ })
157
+
158
+ it('initialises with empty query, empty results, and loading=false', () => {
159
+ expect((result.query as Ref<string>).value).toBe('')
160
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
161
+ expect((result.loading as Ref<boolean>).value).toBe(false)
162
+ })
163
+
164
+ it('registers a watch callback during the render call (not after mount)', () => {
165
+ // The watch() is in the render body: available immediately after useContentSearch()
166
+ expect(_watchCallback).not.toBeNull()
167
+ })
168
+
169
+ it('attaches _cerSearchDebounce to the component context', () => {
170
+ expect(_currentMockContext).toHaveProperty('_cerSearchDebounce')
171
+ const state = _currentMockContext['_cerSearchDebounce'] as { seq: number; timer: unknown }
172
+ expect(state.seq).toBe(0)
173
+ expect(state.timer).toBeNull()
174
+ })
175
+
176
+ it('registers a useOnDisconnected callback for timer cleanup', () => {
177
+ expect(_disconnectedCallbacks).toHaveLength(1)
178
+ })
179
+
180
+ // ─── loading state ─────────────────────────────────────────────────────────
181
+
182
+ it('sets loading=true immediately when a non-empty query is set', () => {
183
+ vi.useFakeTimers()
184
+ simulateType('Hello')
185
+ expect((result.loading as Ref<boolean>).value).toBe(true)
186
+ })
187
+
188
+ it('keeps loading=true while the debounce timer is pending', () => {
189
+ vi.useFakeTimers()
190
+ simulateType('Hello')
191
+ vi.advanceTimersByTime(100) // 100 ms < 200 ms debounce
192
+ expect((result.loading as Ref<boolean>).value).toBe(true)
193
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
194
+ })
195
+
196
+ it('sets loading=false once results arrive after the debounce window', async () => {
197
+ vi.useFakeTimers()
198
+ simulateType('Hello')
199
+ await vi.runAllTimersAsync()
200
+ expect((result.loading as Ref<boolean>).value).toBe(false)
201
+ })
202
+
203
+ it('clears loading immediately when query is reset to empty string', () => {
204
+ vi.useFakeTimers()
205
+ simulateType('Hello')
206
+ expect((result.loading as Ref<boolean>).value).toBe(true)
207
+ simulateType('')
208
+ expect((result.loading as Ref<boolean>).value).toBe(false)
209
+ })
210
+
211
+ // ─── Debounce timing ───────────────────────────────────────────────────────
212
+
213
+ it('withholds results until 200 ms after the last keystroke', () => {
214
+ vi.useFakeTimers()
215
+ simulateType('Hello')
216
+ vi.advanceTimersByTime(199) // 1 ms before debounce fires
217
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
218
+ })
219
+
220
+ it('delivers results after the 200 ms debounce window elapses', async () => {
221
+ vi.useFakeTimers()
222
+ simulateType('Hello')
223
+ await vi.runAllTimersAsync()
224
+ expect((result.results as Ref<unknown[]>).value.length).toBeGreaterThan(0)
225
+ })
226
+
227
+ it('resets the debounce clock when a new query arrives before 200 ms', async () => {
228
+ vi.useFakeTimers()
229
+
230
+ simulateType('Getting') // timer-A starts at t=0
231
+ vi.advanceTimersByTime(100) // t=100 — timer-A still pending (100 < 200)
232
+ simulateType('Hello') // cancels timer-A, timer-B starts at t=100
233
+ vi.advanceTimersByTime(100) // t=200 — only 100 ms since timer-B started; still pending
234
+
235
+ // No results yet — timer-B hasn't fired
236
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
237
+
238
+ await vi.runAllTimersAsync() // timer-B fires at t=300; search runs with 'Hello'
239
+
240
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
241
+ expect(paths).toContain('/blog/hello') // 'Hello' prefix matched Hello World
242
+ expect(paths).not.toContain('/docs/start') // 'Getting' timer was cancelled
243
+ })
244
+
245
+ it('cancels the pending timer and prevents results when query is cleared mid-debounce', async () => {
246
+ vi.useFakeTimers()
247
+ simulateType('Hello') // debounce timer starts
248
+ vi.advanceTimersByTime(100) // partway through window
249
+ simulateType('') // clears timer; loading + results reset immediately
250
+
251
+ expect((result.loading as Ref<boolean>).value).toBe(false)
252
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
253
+
254
+ await vi.runAllTimersAsync() // advance remaining time — no timer should fire
255
+ expect((result.results as Ref<unknown[]>).value).toEqual([]) // still empty
256
+ })
257
+
258
+ // ─── Disconnect cleanup ────────────────────────────────────────────────────
259
+
260
+ it('cancels the pending timer on disconnect', async () => {
261
+ vi.useFakeTimers()
262
+ simulateType('Hello')
263
+ vi.advanceTimersByTime(100) // timer is pending
264
+
265
+ triggerDisconnected()
266
+
267
+ // Timer should be cancelled — advancing past the debounce window produces no results
268
+ await vi.runAllTimersAsync()
269
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
270
+ })
271
+
272
+ it('resets loading to false on disconnect', () => {
273
+ vi.useFakeTimers()
274
+ simulateType('Hello')
275
+ expect((result.loading as Ref<boolean>).value).toBe(true)
276
+
277
+ triggerDisconnected()
278
+ expect((result.loading as Ref<boolean>).value).toBe(false)
279
+ })
280
+
281
+ // ─── Result shape and content ──────────────────────────────────────────────
282
+
283
+ it('result items include _path and title', async () => {
284
+ vi.useFakeTimers()
285
+ simulateType('Hello')
286
+ await vi.runAllTimersAsync()
287
+ const first = (result.results as Ref<Record<string, unknown>[]>).value[0]
288
+ expect(first).toHaveProperty('_path')
289
+ expect(first).toHaveProperty('title')
290
+ })
291
+
292
+ it('searching "Hello" returns Hello World and not Getting Started', async () => {
293
+ vi.useFakeTimers()
294
+ simulateType('Hello')
295
+ await vi.runAllTimersAsync()
296
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
297
+ expect(paths).toContain('/blog/hello')
298
+ expect(paths).not.toContain('/docs/start')
299
+ })
300
+
301
+ it('searching "Getting" returns Getting Started and not Hello World', async () => {
302
+ vi.useFakeTimers()
303
+ simulateType('Getting')
304
+ await vi.runAllTimersAsync()
305
+ const paths = (result.results as Ref<{ _path: string }[]>).value.map(r => r._path)
306
+ expect(paths).toContain('/docs/start')
307
+ expect(paths).not.toContain('/blog/hello')
308
+ })
309
+
310
+ it('clears results immediately when query is reset from non-empty to empty', async () => {
311
+ vi.useFakeTimers()
312
+ simulateType('Hello')
313
+ await vi.runAllTimersAsync()
314
+ expect((result.results as Ref<unknown[]>).value.length).toBeGreaterThan(0)
315
+
316
+ simulateType('')
317
+ expect((result.results as Ref<unknown[]>).value).toEqual([])
318
+ })
319
+
320
+ // ─── Pre-warm ──────────────────────────────────────────────────────────────
321
+
322
+ it('registers a useOnConnected callback for index pre-warming', () => {
323
+ expect(_connectedCallbacks).toHaveLength(1)
324
+ })
325
+
326
+ it('pre-warms the index on mount (triggers a fetch)', async () => {
327
+ triggerConnected()
328
+ // fetch is called by the pre-warm (loadIndex inside useOnConnected)
329
+ expect(globalThis.fetch).toHaveBeenCalledTimes(1)
330
+ })
331
+
332
+ // ─── Re-render stability ───────────────────────────────────────────────────
333
+ //
334
+ // The core fix: _seq and _timer live on the component context, not in local
335
+ // factory-body variables. These tests confirm that calling the factory a second
336
+ // time (simulating a component re-render) reuses the same state object rather
337
+ // than resetting it, so a timer set during one render can be cancelled by the
338
+ // new watcher registered on the next render.
339
+
340
+ it('reuses the same debounce state object when the factory runs again with the same context', () => {
341
+ // First render already called useContentSearch() in beforeEach.
342
+ const state1 = _currentMockContext['_cerSearchDebounce']
343
+ expect(state1).toBeDefined()
344
+
345
+ // Simulate re-render: reset captured callbacks, call factory again.
346
+ _connectedCallbacks = []
347
+ _disconnectedCallbacks = []
348
+ _watchCallback = null
349
+ useContentSearch()
350
+
351
+ const state2 = _currentMockContext['_cerSearchDebounce']
352
+
353
+ // Must be the identical object — not re-created.
354
+ expect(state2).toBe(state1)
355
+ })
356
+
357
+ it('a new keystroke after re-render cancels the timer that was set in the previous render', () => {
358
+ vi.useFakeTimers()
359
+
360
+ // First render (done in beforeEach). Type something → timer-A starts.
361
+ simulateType('Getting')
362
+ const state = _currentMockContext['_cerSearchDebounce'] as {
363
+ timer: ReturnType<typeof setTimeout> | null
364
+ seq: number
365
+ }
366
+ const timerA = state.timer
367
+ expect(timerA).not.toBeNull()
368
+
369
+ // Simulate re-render: new watch callback registered, same context state.
370
+ _watchCallback = null
371
+ useContentSearch()
372
+
373
+ // The new watcher fires. It reads state.timer (still timerA) and cancels it,
374
+ // then starts timer-B.
375
+ simulateType('Hello')
376
+
377
+ expect(state.timer).not.toBeNull()
378
+ expect(state.timer).not.toBe(timerA) // timer-A was replaced by timer-B
379
+ })
380
+
381
+ it('seq counter is not reset to 0 when the factory runs again (shared state)', async () => {
382
+ vi.useFakeTimers()
383
+
384
+ // First render: type something, let the debounce fire.
385
+ simulateType('Getting')
386
+ await vi.runAllTimersAsync() // timer fires → seq incremented to 1
387
+
388
+ const state = _currentMockContext['_cerSearchDebounce'] as { seq: number }
389
+ expect(state.seq).toBe(1)
390
+
391
+ // Simulate re-render: fresh callbacks, same context.
392
+ _connectedCallbacks = []
393
+ _disconnectedCallbacks = []
394
+ _watchCallback = null
395
+ useContentSearch()
396
+
397
+ // Type again on the new watcher and let it fire.
398
+ simulateType('Hello')
399
+ await vi.runAllTimersAsync() // seq incremented to 2
400
+
401
+ // If state were re-initialised on re-render, seq would be 1 again.
402
+ // Shared state means it continues from where it left off.
403
+ expect(state.seq).toBe(2)
404
+ })
405
+ })
@@ -7,9 +7,10 @@
7
7
  * - loadIndex() error path — fetch failure rejects cleanly; singleton reset allows retry
8
8
  * - loadIndex() returns a searchable MiniSearch instance
9
9
  *
10
- * Note: The full useContentSearch() composable (query reactive state, stale-seq
11
- * guard, useOnConnected pre-warm) requires a component context provided by the
12
- * custom-elements-runtime and is exercised by the e2e suite in content.cy.ts.
10
+ * The full useContentSearch() composable (debounce, loading state, stale-seq
11
+ * guard) is tested in use-content-search-composable.test.ts, which mocks the
12
+ * runtime to exercise the watch callback and fake-timer debounce logic directly.
13
+ * End-to-end behaviour is covered by content.cy.ts.
13
14
  */
14
15
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
15
16
  import MiniSearch from 'minisearch'
@@ -99,7 +100,7 @@ describe('loadIndex', () => {
99
100
  resetIndexSingleton()
100
101
 
101
102
  const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
102
- const results = index.search('Hello', { prefix: true }) as Array<{ _path: string }>
103
+ const results = index.search('Hello', { prefix: true }) as unknown as Array<{ _path: string }>
103
104
  expect(results.some((r) => r._path === '/blog/hello')).toBe(true)
104
105
  })
105
106
 
@@ -136,4 +137,27 @@ describe('loadIndex', () => {
136
137
  const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
137
138
  expect(index).toBeDefined()
138
139
  })
140
+
141
+ it('automatically retries after a fetch failure without manual singleton reset', async () => {
142
+ const { loadIndex, resetIndexSingleton } = await import('../../runtime/composables/use-content-search.js')
143
+ resetIndexSingleton()
144
+
145
+ // First call fails — singleton should be cleared automatically
146
+ globalThis.fetch = vi.fn().mockResolvedValue({
147
+ ok: false,
148
+ status: 503,
149
+ } as unknown as Response)
150
+ await expect(loadIndex()).rejects.toThrow()
151
+
152
+ // Second call without resetIndexSingleton — should retry and succeed
153
+ const indexJson = buildIndex()
154
+ globalThis.fetch = vi.fn().mockResolvedValue({
155
+ ok: true,
156
+ text: () => Promise.resolve(indexJson),
157
+ } as unknown as Response)
158
+ const index = await loadIndex() as ReturnType<typeof MiniSearch.loadJSON>
159
+ expect(index).toBeDefined()
160
+ // Confirm the new singleton is cached (second call reuses it)
161
+ expect(await loadIndex()).toBe(index)
162
+ })
139
163
  })
@@ -1,7 +1,9 @@
1
1
  import {
2
2
  createComposable,
3
+ getCurrentComponentContext,
3
4
  ref,
4
5
  useOnConnected,
6
+ useOnDisconnected,
5
7
  watch,
6
8
  } from '@jasonshimmy/custom-elements-runtime'
7
9
  import type { ReactiveState } from '@jasonshimmy/custom-elements-runtime'
@@ -19,11 +21,14 @@ let _indexPromise: Promise<unknown> | null = null
19
21
  * Returns the same Promise on repeated calls — the index is built at most once
20
22
  * per session regardless of how many search components are mounted.
21
23
  *
24
+ * If the fetch fails the singleton is cleared so the next search attempt
25
+ * retries automatically (no page reload required after a transient error).
26
+ *
22
27
  * @internal Exported for unit testing only.
23
28
  */
24
- export async function loadIndex(): Promise<unknown> {
29
+ export function loadIndex(): Promise<unknown> {
25
30
  if (_indexPromise) return _indexPromise
26
- _indexPromise = (async () => {
31
+ const attempt = (async () => {
27
32
  const [{ default: MiniSearch }, raw] = await Promise.all([
28
33
  import('minisearch'),
29
34
  fetch(contentSearchIndexUrl()).then((r) => {
@@ -37,7 +42,13 @@ export async function loadIndex(): Promise<unknown> {
37
42
  idField: '_path',
38
43
  })
39
44
  })()
40
- return _indexPromise
45
+ _indexPromise = attempt
46
+ // Clear the singleton on failure so the next call retries the fetch.
47
+ // The === guard ensures a newer concurrent attempt is not accidentally cleared.
48
+ attempt.catch(() => {
49
+ if (_indexPromise === attempt) _indexPromise = null
50
+ })
51
+ return attempt
41
52
  }
42
53
 
43
54
  /** Resets the module-level singleton. Used in tests only. @internal */
@@ -45,44 +56,114 @@ export function resetIndexSingleton(): void {
45
56
  _indexPromise = null
46
57
  }
47
58
 
59
+ // ─── Per-component debounce state ────────────────────────────────────────────
60
+
61
+ // Stores the debounce timer handle and stale-seq counter directly on the
62
+ // component context object (non-enumerable, same pattern the runtime uses for
63
+ // _hookCallbacks). This makes the values stable across re-renders — the context
64
+ // object is fixed for the lifetime of the element instance — without leaking
65
+ // into the reactive proxy or triggering spurious updates.
66
+
67
+ interface DebounceState {
68
+ seq: number
69
+ timer: ReturnType<typeof setTimeout> | null
70
+ }
71
+
72
+ const _STATE_KEY = '_cerSearchDebounce'
73
+
74
+ function getDebounceState(ctx: Record<string, unknown>): DebounceState {
75
+ if (!Object.prototype.hasOwnProperty.call(ctx, _STATE_KEY)) {
76
+ Object.defineProperty(ctx, _STATE_KEY, {
77
+ value: { seq: 0, timer: null } as DebounceState,
78
+ writable: false, // object ref is fixed; its properties are still mutable
79
+ enumerable: false, // invisible to the reactive proxy set-trap
80
+ configurable: false,
81
+ })
82
+ }
83
+ return (ctx as Record<string, DebounceState>)[_STATE_KEY]
84
+ }
85
+
48
86
  // ─── Composable ───────────────────────────────────────────────────────────────
49
87
 
50
88
  export interface UseContentSearchReturn {
51
89
  query: ReactiveState<string>
52
90
  results: ReactiveState<ContentSearchResult[]>
91
+ /** `true` from the moment the user starts typing until results (or an error) arrive. `false` when the query is empty or the search is complete. */
92
+ loading: ReactiveState<boolean>
53
93
  }
54
94
 
55
95
  const _factory = createComposable((): UseContentSearchReturn => {
56
96
  const query = ref('')
57
97
  const results = ref<ContentSearchResult[]>([])
98
+ const loading = ref(false)
99
+
100
+ // Debounce state lives on the component context, not in local render-body variables.
101
+ // Local variables are re-created on every re-render; the context object is stable
102
+ // for the lifetime of the element. Storing state here lets the render-body
103
+ // watch() (see below) pick up the same timer and sequence counter across re-renders
104
+ // without the watcher accumulation that occurs when watch() is placed inside
105
+ // useOnConnected() (which runs once per mount but is not registered for cleanup
106
+ // by the reactive system, leaking watchers on every disconnect + reconnect cycle).
107
+ const state = getDebounceState(getCurrentComponentContext()! as Record<string, unknown>)
58
108
 
59
- // Pre-warm index on mount
109
+ // Pre-warm the index on first mount so the first real search is faster.
60
110
  useOnConnected(() => {
61
111
  loadIndex().catch(() => {/* silently ignore pre-warm errors */})
62
112
  })
63
113
 
64
- // Monotonic counter to discard stale async results
65
- let _seq = 0
114
+ // Cancel any in-flight debounce on unmount so stale async work doesn't land
115
+ // after the component is gone.
116
+ useOnDisconnected(() => {
117
+ if (state.timer !== null) {
118
+ clearTimeout(state.timer)
119
+ state.timer = null
120
+ }
121
+ state.seq++ // discard any in-flight async search
122
+ loading.value = false
123
+ })
66
124
 
67
- watch(query, async (q: string) => {
68
- const seq = ++_seq
125
+ // watch() is in the render body so the reactive system registers it under the
126
+ // current component and tears it down automatically on re-render and disconnect.
127
+ // The mutable state (seq / timer) lives on the context (above) and persists
128
+ // across re-renders — new watcher instances see the same timer and counter,
129
+ // which is what makes debounce cancellation correct even after a re-render.
130
+ watch(query, (q: string) => {
131
+ if (state.timer !== null) {
132
+ clearTimeout(state.timer)
133
+ state.timer = null
134
+ }
69
135
 
70
- if (!q || q.length < 2) {
136
+ if (!q) {
137
+ // Increment seq so any in-flight async search is discarded when it resolves
138
+ state.seq++
139
+ loading.value = false
71
140
  results.value = []
72
141
  return
73
142
  }
74
143
 
75
- try {
76
- const index = await loadIndex() as { search(q: string, opts?: { prefix?: boolean }): ContentSearchResult[] }
77
- if (seq !== _seq) return // stale — a newer query is in flight
78
- results.value = index.search(q, { prefix: true }) as ContentSearchResult[]
79
- } catch {
80
- if (seq !== _seq) return
81
- results.value = []
82
- }
144
+ // Signal loading immediately so the UI can respond before the debounce fires
145
+ loading.value = true
146
+
147
+ state.timer = setTimeout(async () => {
148
+ state.timer = null
149
+ const seq = ++state.seq
150
+
151
+ try {
152
+ const index = await loadIndex() as { search(q: string, opts?: { prefix?: boolean }): ContentSearchResult[] }
153
+ if (seq !== state.seq) return // stale — a newer query is in flight
154
+ results.value = index.search(q, { prefix: true }) as ContentSearchResult[]
155
+ } catch {
156
+ if (seq !== state.seq) return
157
+ results.value = []
158
+ } finally {
159
+ // Only clear loading for the most recent search; a newer in-flight search
160
+ // keeps loading=true until it settles.
161
+ if (seq === state.seq) loading.value = false
162
+ }
163
+ }, 200)
83
164
  })
84
165
 
85
- return { query, results }
166
+ return { query, results, loading }
86
167
  })
87
168
 
88
169
  /**
@@ -92,8 +173,10 @@ const _factory = createComposable((): UseContentSearchReturn => {
92
173
  * `/_content/search-index.json`. Both MiniSearch and the index are loaded via
93
174
  * dynamic import — neither is in the app bundle.
94
175
  *
95
- * Searches `title` and `description` fields. Results are empty until at least
96
- * 2 characters are entered.
176
+ * Searches `title` and `description` fields. Input is debounced (200 ms) so
177
+ * the index is not queried on every keystroke. `loading` becomes `true` as soon
178
+ * as the user starts typing and returns to `false` once results arrive. Results
179
+ * are empty when the query is empty.
97
180
  *
98
181
  * **SSR note**: search is always client-side. In SSR mode the component renders
99
182
  * with empty results and hydrates on mount.
@@ -101,10 +184,11 @@ const _factory = createComposable((): UseContentSearchReturn => {
101
184
  * @example
102
185
  * ```ts
103
186
  * component('site-search', () => {
104
- * const { query, results } = useContentSearch()
187
+ * const { query, results, loading } = useContentSearch()
105
188
  *
106
189
  * return html`
107
190
  * <input type="search" :model="${query}" placeholder="Search…" />
191
+ * ${loading.value ? html`<p>Searching…</p>` : ''}
108
192
  * ${when(results.value.length > 0, () => html`
109
193
  * <ul>
110
194
  * ${each(results.value, r => html`