|
|
@@ -2,7 +2,8 @@ import { QueryClient } from '@tanstack/react-query' |
|
|
import { match } from 'path-to-regexp' |
|
|
import { match } from 'path-to-regexp' |
|
|
|
|
|
|
|
|
import { fetchPost, fetchPosts, fetchPostChanges } from '@/lib/posts' |
|
|
import { fetchPost, fetchPosts, fetchPostChanges } from '@/lib/posts' |
|
|
import { postsKeys, wikiKeys } from '@/lib/queryKeys' |
|
|
|
|
|
|
|
|
import { postsKeys, tagsKeys, wikiKeys } from '@/lib/queryKeys' |
|
|
|
|
|
import { fetchTagByName } from '@/lib/tags' |
|
|
import { fetchWikiPageByTitle, fetchWikiPages } from '@/lib/wiki' |
|
|
import { fetchWikiPageByTitle, fetchWikiPages } from '@/lib/wiki' |
|
|
|
|
|
|
|
|
type Prefetcher = (qc: QueryClient, url: URL) => Promise<void> |
|
|
type Prefetcher = (qc: QueryClient, url: URL) => Promise<void> |
|
|
@@ -13,6 +14,7 @@ const mWiki = match<{ title: string }> ('/wiki/:title') |
|
|
|
|
|
|
|
|
const prefetchWikiPagesIndex: Prefetcher = async (qc, url) => { |
|
|
const prefetchWikiPagesIndex: Prefetcher = async (qc, url) => { |
|
|
const title = url.searchParams.get ('title') ?? '' |
|
|
const title = url.searchParams.get ('title') ?? '' |
|
|
|
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
await qc.prefetchQuery ({ |
|
|
queryKey: wikiKeys.index ({ title }), |
|
|
queryKey: wikiKeys.index ({ title }), |
|
|
queryFn: () => fetchWikiPages ({ title }) }) |
|
|
queryFn: () => fetchWikiPages ({ title }) }) |
|
|
@@ -24,11 +26,26 @@ const prefetchWikiPageShow: Prefetcher = async (qc, url) => { |
|
|
if (!(m)) |
|
|
if (!(m)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
const version = url.searchParams.get ('version') |
|
|
|
|
|
const { title } = m.params |
|
|
|
|
|
|
|
|
const title = decodeURIComponent (m.params.title) |
|
|
|
|
|
const version = url.searchParams.get ('version') || undefined |
|
|
|
|
|
|
|
|
|
|
|
const wikiPage = await qc.fetchQuery ({ |
|
|
|
|
|
queryKey: wikiKeys.show (title, { version }), |
|
|
|
|
|
queryFn: () => fetchWikiPageByTitle (title, { version }) }) |
|
|
|
|
|
|
|
|
|
|
|
const effectiveTitle = wikiPage.title |
|
|
|
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
await qc.prefetchQuery ({ |
|
|
queryKey: wikiKeys.show (title, version ? { version } : { }), |
|
|
|
|
|
queryFn: () => fetchWikiPageByTitle (title, version ? { version } : { }) }) |
|
|
|
|
|
|
|
|
queryKey: tagsKeys.show (effectiveTitle), |
|
|
|
|
|
queryFn: () => fetchTagByName (effectiveTitle) }) |
|
|
|
|
|
|
|
|
|
|
|
if (version) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
const p = { tags: effectiveTitle, match: 'all', page: 1, limit: 8 } as const |
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
|
|
|
queryKey: postsKeys.index (p), |
|
|
|
|
|
queryFn: () => fetchPosts (p) }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -37,6 +54,7 @@ const prefetchPostsIndex: Prefetcher = async (qc, url) => { |
|
|
const m = url.searchParams.get ('match') === 'any' ? 'any' : 'all' |
|
|
const m = url.searchParams.get ('match') === 'any' ? 'any' : 'all' |
|
|
const page = Number (url.searchParams.get ('page') || 1) |
|
|
const page = Number (url.searchParams.get ('page') || 1) |
|
|
const limit = Number (url.searchParams.get ('limit') || 20) |
|
|
const limit = Number (url.searchParams.get ('limit') || 20) |
|
|
|
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
await qc.prefetchQuery ({ |
|
|
queryKey: postsKeys.index ({ tags, match: m, page, limit }), |
|
|
queryKey: postsKeys.index ({ tags, match: m, page, limit }), |
|
|
queryFn: () => fetchPosts ({ tags, match: m, page, limit }) }) |
|
|
queryFn: () => fetchPosts ({ tags, match: m, page, limit }) }) |
|
|
@@ -49,6 +67,7 @@ const prefetchPostShow: Prefetcher = async (qc, url) => { |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
const { id } = m.params |
|
|
const { id } = m.params |
|
|
|
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
await qc.prefetchQuery ({ |
|
|
queryKey: postsKeys.show (id), |
|
|
queryKey: postsKeys.show (id), |
|
|
queryFn: () => fetchPost (id) }) |
|
|
queryFn: () => fetchPost (id) }) |
|
|
@@ -59,6 +78,7 @@ const prefetchPostChanges: Prefetcher = async (qc, url) => { |
|
|
const id = url.searchParams.get ('id') |
|
|
const id = url.searchParams.get ('id') |
|
|
const page = Number (url.searchParams.get ('page') || 1) |
|
|
const page = Number (url.searchParams.get ('page') || 1) |
|
|
const limit = Number (url.searchParams.get ('limit') || 20) |
|
|
const limit = Number (url.searchParams.get ('limit') || 20) |
|
|
|
|
|
|
|
|
await qc.prefetchQuery ({ |
|
|
await qc.prefetchQuery ({ |
|
|
queryKey: postsKeys.changes ({ ...(id && { id }), page, limit }), |
|
|
queryKey: postsKeys.changes ({ ...(id && { id }), page, limit }), |
|
|
queryFn: () => fetchPostChanges ({ ...(id && { id }), page, limit }) }) |
|
|
queryFn: () => fetchPostChanges ({ ...(id && { id }), page, limit }) }) |
|
|
|