Write an ember-modifier that starts the book request on hover or focus and caches the promise. The route's model hook then reads the same cached promise. <LinkTo> passes the modifier through to its <a>.
// app/lib/book/load-catalog.js
const bookCache = new Map();
export function prefetchBook(id, searchParams) {
const key = `${id}?${stringifySearchParams(searchParams)}`;
if (!bookCache.has(key)) bookCache.set(key, loadBook(id, searchParams));
return bookCache.get(key);
}
// app/modifiers/prefetch-book.js
import { modifier } from 'ember-modifier';
import { prefetchBook } from '../lib/book/load-catalog';
export default modifier((element, [id, searchParams]) => {
const warm = () => prefetchBook(id, searchParams);
element.addEventListener('pointerenter', warm, { once: true });
element.addEventListener('focus', warm, { once: true });
return () => {
element.removeEventListener('pointerenter', warm);
element.removeEventListener('focus', warm);
};
});
// app/routes/book.js: model(params) { return prefetchBook(params.id, parseSearchParams(params)); }
// app/components/book/book-card.gjs
<LinkTo @route="book" @model={{this.bookId}} @query={{this.query}}
{{prefetchBook this.bookId @searchParams}}>
{{! … }}
</LinkTo>