ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.5 KiB

  1. import { QueryClient } from '@tanstack/react-query'
  2. import { match } from 'path-to-regexp'
  3. import { fetchPost, fetchPosts } from '@/lib/posts'
  4. import { postsKeys } from '@/lib/queryKeys'
  5. type Prefetcher = (qc: QueryClient, url: URL) => Promise<void>
  6. const mPost = match<{ id: string }> ('/posts/:id')
  7. const prefetchPostsIndex: Prefetcher = async (qc, url) => {
  8. const tags = url.searchParams.get ('tags') ?? ''
  9. const m = url.searchParams.get ('match') === 'any' ? 'any' : 'all'
  10. const page = Number (url.searchParams.get ('page') || 1)
  11. const limit = Number (url.searchParams.get ('limit') || 20)
  12. await qc.prefetchQuery ({
  13. queryKey: postsKeys.index ({ tags, match: m, page, limit }),
  14. queryFn: () => fetchPosts ({ tags, match: m, page, limit }) })
  15. }
  16. const prefetchPostShow: Prefetcher = async (qc, url) => {
  17. const m = mPost (url.pathname)
  18. if (!(m))
  19. return
  20. const { id } = m.params
  21. await qc.prefetchQuery ({
  22. queryKey: postsKeys.show (id),
  23. queryFn: () => fetchPost (id) })
  24. }
  25. export const routePrefetchers: {
  26. test: (u: URL) => boolean
  27. run: Prefetcher }[] = [
  28. { test: u => u.pathname === '/' || u.pathname === '/posts', run: prefetchPostsIndex },
  29. { test: u => Boolean (mPost (u.pathname)), run: prefetchPostShow }]
  30. export const prefetchForURL = async (qc: QueryClient, urlLike: string): Promise<void> => {
  31. const u = new URL (urlLike, location.origin)
  32. const jobs = routePrefetchers.filter (r => r.test (u)).map (r => r.run (qc, u))
  33. if (jobs.length === 0)
  34. return
  35. await Promise.all (jobs)
  36. }