| import path from '../../../shared/lib/isomorphic/path' |
| import type { MetadataContext } from '../types/resolvers' |
|
|
| function isStringOrURL(icon: any): icon is string | URL { |
| return typeof icon === 'string' || icon instanceof URL |
| } |
|
|
| function createLocalMetadataBase() { |
| |
| const isExperimentalHttps = Boolean(process.env.__NEXT_EXPERIMENTAL_HTTPS) |
| const protocol = isExperimentalHttps ? 'https' : 'http' |
| return new URL(`${protocol}://localhost:${process.env.PORT || 3000}`) |
| } |
|
|
| function getPreviewDeploymentUrl(): URL | undefined { |
| const origin = process.env.VERCEL_BRANCH_URL || process.env.VERCEL_URL |
| return origin ? new URL(`https://${origin}`) : undefined |
| } |
|
|
| function getProductionDeploymentUrl(): URL | undefined { |
| const origin = process.env.VERCEL_PROJECT_PRODUCTION_URL |
| return origin ? new URL(`https://${origin}`) : undefined |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function getSocialImageMetadataBaseFallback( |
| metadataBase: URL | null |
| ): URL { |
| const defaultMetadataBase = createLocalMetadataBase() |
| const previewDeploymentUrl = getPreviewDeploymentUrl() |
| const productionDeploymentUrl = getProductionDeploymentUrl() |
|
|
| let fallbackMetadataBase |
| if (process.env.NODE_ENV === 'development') { |
| fallbackMetadataBase = defaultMetadataBase |
| } else { |
| fallbackMetadataBase = |
| process.env.NODE_ENV === 'production' && |
| previewDeploymentUrl && |
| process.env.VERCEL_ENV === 'preview' |
| ? previewDeploymentUrl |
| : metadataBase || productionDeploymentUrl || defaultMetadataBase |
| } |
|
|
| return fallbackMetadataBase |
| } |
|
|
| function resolveUrl(url: null | undefined, metadataBase: URL | null): null |
| function resolveUrl(url: string | URL, metadataBase: URL | null): URL |
| function resolveUrl( |
| url: string | URL | null | undefined, |
| metadataBase: URL | null |
| ): URL | null |
| function resolveUrl( |
| url: string | URL | null | undefined, |
| metadataBase: URL | null |
| ): URL | null { |
| if (url instanceof URL) return url |
| if (!url) return null |
|
|
| try { |
| |
| const parsedUrl = new URL(url) |
| return parsedUrl |
| } catch {} |
|
|
| if (!metadataBase) { |
| metadataBase = createLocalMetadataBase() |
| } |
|
|
| |
| const pathname = metadataBase.pathname || '' |
| const joinedPath = path.posix.join(pathname, url) |
|
|
| return new URL(joinedPath, metadataBase) |
| } |
|
|
| |
| function resolveRelativeUrl(url: string | URL, pathname: string): string | URL { |
| if (typeof url === 'string' && url.startsWith('./')) { |
| return path.posix.resolve(pathname, url) |
| } |
| return url |
| } |
|
|
| |
| const FILE_REGEX = |
| /^(?:\/((?!\.well-known(?:\/.*)?)(?:[^/]+\/)*[^/]+\.\w+))(\/?|$)/i |
| function isFilePattern(pathname: string): boolean { |
| return FILE_REGEX.test(pathname) |
| } |
|
|
| |
| function resolveAbsoluteUrlWithPathname( |
| url: string | URL, |
| metadataBase: URL | null, |
| pathname: string, |
| { trailingSlash }: MetadataContext |
| ): string { |
| |
| url = resolveRelativeUrl(url, pathname) |
|
|
| |
| |
| let resolvedUrl = '' |
| const result = metadataBase ? resolveUrl(url, metadataBase) : url |
| if (typeof result === 'string') { |
| resolvedUrl = result |
| } else { |
| resolvedUrl = result.pathname === '/' ? result.origin : result.href |
| } |
|
|
| |
| |
| |
| if (trailingSlash && !resolvedUrl.endsWith('/')) { |
| let isRelative = resolvedUrl.startsWith('/') |
| let hasQuery = resolvedUrl.includes('?') |
| let isExternal = false |
| let isFileUrl = false |
|
|
| if (!isRelative) { |
| try { |
| const parsedUrl = new URL(resolvedUrl) |
| isExternal = |
| metadataBase != null && parsedUrl.origin !== metadataBase.origin |
| isFileUrl = isFilePattern(parsedUrl.pathname) |
| } catch { |
| |
| isExternal = true |
| } |
| if ( |
| |
| !isFileUrl && |
| !isExternal && |
| !hasQuery |
| ) |
| return `${resolvedUrl}/` |
| } |
| } |
|
|
| return resolvedUrl |
| } |
|
|
| export { |
| isStringOrURL, |
| resolveUrl, |
| resolveRelativeUrl, |
| resolveAbsoluteUrlWithPathname, |
| } |
|
|