fix: warm middleware on server-side (#6636)

This commit is contained in:
Calum H.
2026-07-06 17:39:43 +00:00
committed by GitHub
parent 1cce1eb248
commit 732bdfc79b
+24 -5
View File
@@ -1,6 +1,7 @@
import { useGeneratedState } from '~/composables/generated' import { useGeneratedState } from '~/composables/generated'
import { projectQueryOptions } from '~/composables/queries/project' import { projectQueryOptions } from '~/composables/queries/project'
import { useAppQueryClient } from '~/composables/query-client' import { useAppQueryClient } from '~/composables/query-client'
import { createModrinthClient } from '~/helpers/api.ts'
import { getProjectTypeForUrlShorthand } from '~/helpers/projects.js' import { getProjectTypeForUrlShorthand } from '~/helpers/projects.js'
import { useServerModrinthClient } from '~/server/utils/api-client' import { useServerModrinthClient } from '~/server/utils/api-client'
@@ -18,9 +19,6 @@ const PROJECT_TYPES = [
] ]
export default defineNuxtRouteMiddleware(async (to) => { export default defineNuxtRouteMiddleware(async (to) => {
// Only run this middleware on the server - it relies on server-only runtime config
if (import.meta.client) return
const routeProjectParam = to.params.project const routeProjectParam = to.params.project
const projectId = Array.isArray(routeProjectParam) ? routeProjectParam[0] : routeProjectParam const projectId = Array.isArray(routeProjectParam) ? routeProjectParam[0] : routeProjectParam
const routeType = Array.isArray(to.params.type) ? to.params.type[0] : to.params.type const routeType = Array.isArray(to.params.type) ? to.params.type[0] : to.params.type
@@ -31,10 +29,11 @@ export default defineNuxtRouteMiddleware(async (to) => {
} }
const queryClient = useAppQueryClient() const queryClient = useAppQueryClient()
const authToken = useCookie('auth-token') const client = await getProjectMiddlewareClient()
const client = useServerModrinthClient({ authToken: authToken.value || undefined })
const tags = useGeneratedState() const tags = useGeneratedState()
if (import.meta.client) startLoading()
try { try {
// Fetch v2 and v3 in parallel — cache both for the page's useQuery calls // Fetch v2 and v3 in parallel — cache both for the page's useQuery calls
const [project, projectV3] = await Promise.all([ const [project, projectV3] = await Promise.all([
@@ -48,9 +47,11 @@ export default defineNuxtRouteMiddleware(async (to) => {
// Cache by slug if we looked up by ID (or vice versa) // Cache by slug if we looked up by ID (or vice versa)
if (projectId !== project.slug) { if (projectId !== project.slug) {
queryClient.setQueryData(['project', 'v2', project.slug], project) queryClient.setQueryData(['project', 'v2', project.slug], project)
queryClient.setQueryData(['project', 'v3', project.slug], projectV3)
} }
if (projectId !== project.id) { if (projectId !== project.id) {
queryClient.setQueryData(['project', 'v2', project.id], project) queryClient.setQueryData(['project', 'v2', project.id], project)
queryClient.setQueryData(['project', 'v3', project.id], projectV3)
} }
const projectType = projectV3.minecraft_server != null ? 'server' : project.project_type const projectType = projectV3.minecraft_server != null ? 'server' : project.project_type
@@ -81,5 +82,23 @@ export default defineNuxtRouteMiddleware(async (to) => {
} }
} catch { } catch {
// Let the page handle 404s and other errors // Let the page handle 404s and other errors
} finally {
if (import.meta.client) stopLoading()
} }
}) })
async function getProjectMiddlewareClient() {
if (import.meta.server) {
const authToken = useCookie('auth-token')
return useServerModrinthClient({ authToken: authToken.value || undefined })
}
const auth = await useAuth()
const config = useRuntimeConfig()
return createModrinthClient(auth, {
apiBaseUrl: config.public.apiBaseUrl.replace('/v2/', '/'),
archonBaseUrl: config.public.pyroBaseUrl.replace('/v2/', '/'),
rateLimitKey: config.rateLimitKey,
})
}