fix: canonical and social URLs answered as paths become absolute on the request origin
This commit is contained in:
@@ -83,13 +83,26 @@ interface SeoHeadInput {
|
||||
link: Array<{ rel: string; href: string }>
|
||||
}
|
||||
|
||||
/**
|
||||
* `value` made absolute on `origin` when it is a path ("/x"); anything else
|
||||
* is returned as it is. The plugin answers paths when the backend has no
|
||||
* site URL (golem15.seo.site_url, app.url), and a canonical or social URL
|
||||
* must be absolute.
|
||||
*/
|
||||
export function seoAbsolute(value: string, origin: string | undefined): string {
|
||||
if (!origin || !value.startsWith('/') || value.startsWith('//')) return value
|
||||
return origin.replace(/\/+$/, '') + value
|
||||
}
|
||||
|
||||
/**
|
||||
* The head of an answer. Only non-empty values become tags, so the page's
|
||||
* own tag stays wherever the answer has none. The title is final (the
|
||||
* plugin applies its templates), hence the plain titleTemplate. A not_found
|
||||
* answer, or no answer, contributes only the staging robots.
|
||||
* answer, or no answer, contributes only the staging robots. A canonical,
|
||||
* og:url or og:image that is a path is made absolute on `origin` (the
|
||||
* request's origin).
|
||||
*/
|
||||
export function seoHeadInput(answer: SeoResolveAnswer | null): SeoHeadInput {
|
||||
export function seoHeadInput(answer: SeoResolveAnswer | null, origin?: string): SeoHeadInput {
|
||||
const head: SeoHeadInput = { meta: [], link: [] }
|
||||
const robots = seoRobots(answer)
|
||||
if (robots) head.meta.push({ name: 'robots', content: robots })
|
||||
@@ -108,8 +121,8 @@ export function seoHeadInput(answer: SeoResolveAnswer | null): SeoHeadInput {
|
||||
name('description', seo.description)
|
||||
property('og:title', seo.og?.title)
|
||||
property('og:description', seo.og?.description)
|
||||
property('og:image', seo.og?.image)
|
||||
property('og:url', seo.og?.url)
|
||||
property('og:image', seo.og?.image && seoAbsolute(seo.og.image, origin))
|
||||
property('og:url', seo.og?.url && seoAbsolute(seo.og.url, origin))
|
||||
name('twitter:card', seo.twitter?.card)
|
||||
name('twitter:site', seo.twitter?.site)
|
||||
for (const tag of Array.isArray(seo.verification) ? seo.verification : []) {
|
||||
@@ -117,7 +130,7 @@ export function seoHeadInput(answer: SeoResolveAnswer | null): SeoHeadInput {
|
||||
head.meta.push({ name: tag.name, content: tag.content })
|
||||
}
|
||||
}
|
||||
if (seo.canonical) head.link.push({ rel: 'canonical', href: seo.canonical })
|
||||
if (seo.canonical) head.link.push({ rel: 'canonical', href: seoAbsolute(seo.canonical, origin) })
|
||||
return head
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user