examples/lit/prefetching-lit-labs-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Lit Query Prefetching Example (@lit-labs/router)#
Prefetches a character when you hover its link, so the detail view opens without a loading state.
Built with @lit-labs/router.
How it works#
src/services/characters/queries.tsdefinescharactersQueries.list()andcharactersQueries.detail(id)withqueryOptions.detailusesskipTokenwhenidisundefined.src/character-item.tscallsuseQueryClient().query({ ...charactersQueries.detail(this.characterId), staleTime: 10 * 1000 })on mouse enter or focus. It also subscribes withcreateQueryControllerandenabled: false, so the name turns bold once the character is cached.src/home-view.tsloads the list and the selected character withcreateQueryController.src/character-view.tsis the/character/:idroute fromsrc/main.ts(@lit-labs/router). ItscreateQueryControllerforcharactersQueries.detail(...)renders without a loading state when the character was prefetched.
To run this example:
npm installnpm run dev