From b80ce7d7c33b6b87ae840b1a8ddaad76b2d40ecf Mon Sep 17 00:00:00 2001 From: FreePoix <17864213831@163.com> Date: Wed, 20 Mar 2024 14:08:31 +0800 Subject: [PATCH] fix: regenerate og and twitter meta --- configs/app/meta.ts | 18 ++++-------------- lib/metadata/generate.ts | 9 +++------ lib/metadata/types.ts | 4 ++-- nextjs/PageNextJs.tsx | 15 +++++++++++---- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/configs/app/meta.ts b/configs/app/meta.ts index 4ac777e5cc..ee33d154c5 100644 --- a/configs/app/meta.ts +++ b/configs/app/meta.ts @@ -1,32 +1,22 @@ import app from './app'; import { getEnvValue, getExternalAssetFilePath, parseEnvJson } from './utils'; -const defaultImageUrl = app.baseUrl + '/static/og_placeholder.png'; - -const protocol = getEnvValue('NEXT_PUBLIC_APP_PROTOCOL'); -const host = getEnvValue('NEXT_PUBLIC_APP_HOST'); -const port = getEnvValue('NEXT_PUBLIC_APP_PORT'); const path = getExternalAssetFilePath('NEXT_PUBLIC_OG_IMAGE_URL'); -let imageUrl: string; +let ogImageUrl: URL; // Hanlde Nextjs to collect page data try { - if (path) { - const ogImageUrl = new URL(path || '', `${ protocol }://${ host }:${ port }`); - imageUrl = ogImageUrl.href; - } else { - imageUrl = defaultImageUrl; - } + ogImageUrl = new URL(path || '', app.baseUrl); } catch (e) { - imageUrl = defaultImageUrl; + ogImageUrl = new URL('', 'https://placehodler'); } const meta = Object.freeze({ promoteBlockscoutInTitle: parseEnvJson(getEnvValue('NEXT_PUBLIC_PROMOTE_BLOCKSCOUT_IN_TITLE')) ?? true, og: { + url: ogImageUrl, title: getEnvValue('NEXT_PUBLIC_OG_TITLE') || '', description: getEnvValue('NEXT_PUBLIC_OG_DESCRIPTION') || '', - imageUrl, }, }); diff --git a/lib/metadata/generate.ts b/lib/metadata/generate.ts index a56894b0d1..1671ad2028 100644 --- a/lib/metadata/generate.ts +++ b/lib/metadata/generate.ts @@ -6,7 +6,6 @@ import config from 'configs/app'; import getNetworkTitle from 'lib/networks/getNetworkTitle'; import compileValue from './compileValue'; -import getPageOgType from './getPageOgType'; import * as templates from './templates'; export default function generate(route: R, apiData?: ApiData): Metadata { @@ -21,15 +20,13 @@ export default function generate(route: R, apiData?: ApiData const title = compiledTitle ? compiledTitle + (config.meta.promoteBlockscoutInTitle ? ' | Blockscout' : '') : ''; const description = compileValue(templates.description.make(route.pathname), params); - const pageOgType = getPageOgType(route.pathname); - return { title: title, description, opengraph: { - title: pageOgType !== 'Regular page' ? config.meta.og.title : '', - description: pageOgType !== 'Regular page' ? config.meta.og.description : '', - imageUrl: pageOgType !== 'Regular page' ? config.meta.og.imageUrl : '', + url: config.meta.og.url, + title: config.meta.og.title || title, + description: config.meta.og.description || description, }, }; } diff --git a/lib/metadata/types.ts b/lib/metadata/types.ts index 252dbc29cf..a15b9085d9 100644 --- a/lib/metadata/types.ts +++ b/lib/metadata/types.ts @@ -11,8 +11,8 @@ export interface Metadata { title: string; description: string; opengraph: { + url: URL; title: string; - description?: string; - imageUrl?: string; + description: string; }; } diff --git a/nextjs/PageNextJs.tsx b/nextjs/PageNextJs.tsx index 1c15c9f419..3b35778a8a 100644 --- a/nextjs/PageNextJs.tsx +++ b/nextjs/PageNextJs.tsx @@ -32,11 +32,18 @@ const PageNextJs = (props: Props) => { { /* OG TAGS */ } - { opengraph.description && } - - - + + + + + { /* Twitter Meta Tags */ } + + + + + + { props.children }