From 5538d9d0b561466907753189462ee655cd696387 Mon Sep 17 00:00:00 2001 From: Caner Akdas Date: Fri, 10 Apr 2026 14:14:51 +0300 Subject: [PATCH 1/2] refactor: fetch orama-db when search triggered --- src/generators/web/ui/hooks/useOrama.mjs | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/generators/web/ui/hooks/useOrama.mjs b/src/generators/web/ui/hooks/useOrama.mjs index f8861eb4..8b364e80 100644 --- a/src/generators/web/ui/hooks/useOrama.mjs +++ b/src/generators/web/ui/hooks/useOrama.mjs @@ -1,15 +1,17 @@ import { create, search, load } from '@orama/orama'; -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import { relativeOrAbsolute } from '../utils/relativeOrAbsolute.mjs'; /** - * Hook for initializing and managing Orama search database + * Hook for initializing and managing Orama search database. + * The search data is lazily fetched on the first search call. * * @param {string} pathname - The current page's path (e.g., '/api/fs') */ export default pathname => { const [client, setClient] = useState(null); + const loaded = useRef(null); useEffect(() => { // Create the database instance @@ -17,18 +19,30 @@ export default pathname => { schema: {}, }); + /** + * Ensures the search data is loaded. + * @returns {Promise} A promise that resolves when the data is loaded. + */ + const ensureLoaded = () => { + if (!loaded.current) { + loaded.current = fetch(relativeOrAbsolute('/orama-db.json', pathname)) + .then(response => response.ok && response.json()) + .then(data => load(db, data)); + } + + return loaded.current; + }; + // TODO(@avivkeller): Ask Orama to support this functionality natively /** * @param {any} options */ - db.search = options => search(db, options); + db.search = async options => { + await ensureLoaded(); + return search(db, options); + }; setClient(db); - - // Load the search data - fetch(relativeOrAbsolute('/orama-db.json', pathname)) - .then(response => response.ok && response.json()) - .then(data => load(db, data)); }, []); return client; From a855996ea297eef232d36c6be53d8b46ce53841c Mon Sep 17 00:00:00 2001 From: Caner Akdas Date: Fri, 10 Apr 2026 14:41:36 +0300 Subject: [PATCH 2/2] fix: null ref when fetch catch --- src/generators/web/ui/hooks/useOrama.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/generators/web/ui/hooks/useOrama.mjs b/src/generators/web/ui/hooks/useOrama.mjs index 8b364e80..30a5903c 100644 --- a/src/generators/web/ui/hooks/useOrama.mjs +++ b/src/generators/web/ui/hooks/useOrama.mjs @@ -27,7 +27,10 @@ export default pathname => { if (!loaded.current) { loaded.current = fetch(relativeOrAbsolute('/orama-db.json', pathname)) .then(response => response.ok && response.json()) - .then(data => load(db, data)); + .then(data => load(db, data)) + .catch(() => { + loaded.current = null; + }); } return loaded.current;