Skip to content

Commit

Permalink
fix: prerender using crawled query params
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 15, 2024
1 parent 091c685 commit a988a74
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/runtime/app/composables/defineOgImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ export function defineOgImage(_options: DefineOgImageInput = {}) {
createOgImageMeta(null, options, resolvedOptions, nuxtApp.ssrContext!)
}
else {
const path = getOgImagePath(basePath, resolvedOptions)
if (import.meta.prerender)
appendHeader(useRequestEvent(), 'x-nitro-prerender', path)
const path = getOgImagePath(basePath, defu(resolvedOptions, { _query: options._query }))
if (import.meta.prerender) {
appendHeader(useRequestEvent(nuxtApp)!, 'x-nitro-prerender', path)
}
createOgImageMeta(path, options, resolvedOptions, nuxtApp.ssrContext!)
}
}
4 changes: 2 additions & 2 deletions src/runtime/server/og-image/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { logger } from '../util/logger'
import { normaliseOptions } from '../util/options'
import { useChromiumRenderer, useSatoriRenderer } from './instances'

export function resolvePathCacheKey(e: H3Event, path?: string) {
export function resolvePathCacheKey(e: H3Event, path: string) {
const siteConfig = e.context.siteConfig.get()
const basePath = withoutTrailingSlash(withoutLeadingSlash(normalizeKey(path || e.path)))
const basePath = withoutTrailingSlash(withoutLeadingSlash(normalizeKey(path)))
return [
(!basePath || basePath === '/') ? 'index' : basePath,
hash([
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Head } from '@unhead/schema'
import type { OgImageOptions, OgImageRuntimeConfig } from './types'
import { useRuntimeConfig } from '#imports'
import { defu } from 'defu'
import { joinURL } from 'ufo'
import { joinURL, withQuery } from 'ufo'
import { getExtension } from './pure'

// must work in both Nuxt an
Expand Down Expand Up @@ -39,7 +39,11 @@ export function generateMeta(url: string, resolvedOptions: OgImageOptions) {
export function getOgImagePath(pagePath: string, _options?: Partial<OgImageOptions>) {
const baseURL = useRuntimeConfig().app.baseURL
const options = defu(_options, useOgImageRuntimeConfig().defaults)
return joinURL('/', baseURL, `__og-image__/${import.meta.prerender ? 'static' : 'image'}`, pagePath, `og.${options.extension}`)
const path = joinURL('/', baseURL, `__og-image__/${import.meta.prerender ? 'static' : 'image'}`, pagePath, `og.${options.extension}`)
if (Object.keys(options._query || {}).length) {
return withQuery(path, options._query)
}
return path
}

export function useOgImageRuntimeConfig() {
Expand Down

0 comments on commit a988a74

Please sign in to comment.