fix: optimize og image url in metadata

This commit is contained in:
javayhu 2025-06-10 22:52:16 +08:00
parent 5b50e62774
commit 56df0bed46
2 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import { websiteConfig } from '@/config/website';
import { defaultMessages } from '@/i18n/messages';
import type { Metadata } from 'next';
import { getBaseUrl } from './urls/urls';
import { getBaseUrl, getImageUrl } from './urls/urls';
/**
* Construct the metadata object for the current page (in docs/guides)
@ -22,7 +22,7 @@ export function constructMetadata({
title = title || defaultMessages.Metadata.title;
description = description || defaultMessages.Metadata.description;
image = image || websiteConfig.metadata.images?.ogImage;
const ogImageUrl = new URL(`${getBaseUrl()}${image}`);
const ogImageUrl = getImageUrl(image || '');
return {
title,
description,

View File

@ -79,6 +79,21 @@ export function getUrlWithLocaleInCallbackUrl(
}
}
/**
* Get the URL of the image, if the image is a relative path, it will be prefixed with the base URL
* @param image - The image URL
* @returns The URL of the image
*/
export function getImageUrl(image: string): string {
if (image.startsWith('http://') || image.startsWith('https://')) {
return image;
}
if (image.startsWith('/')) {
return `${getBaseUrl()}${image}`;
}
return `${getBaseUrl()}/${image}`;
}
/**
* Get the Stripe dashboard customer URL
* @param customerId - The Stripe customer ID