Skip to content

Commit

Permalink
feat: og
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 12, 2023
1 parent 0d62125 commit 5537cfb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
10 changes: 3 additions & 7 deletions src/app/(page-detail)/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
import { CommentAreaRootLazy } from '~/components/widgets/comment'
import { TocFAB } from '~/components/widgets/toc/TocFAB'
import { attachUA } from '~/lib/attach-ua'
import { getOgUrl } from '~/lib/helper.server'
import { getSummaryFromMd } from '~/lib/markdown'
import { getQueryClient } from '~/lib/query-client.server'
import { CurrentPageDataProvider } from '~/providers/page/CurrentPageDataProvider'
Expand All @@ -33,15 +34,10 @@ export const generateMetadata = async ({
try {
attachUA()
const data = await getQueryClient().fetchQuery(queries.page.bySlug(slug))
const { title, images, text } = data
const { title, text } = data
const description = getSummaryFromMd(text ?? '')

const ogImage = images?.length
? {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
url: images[0].src!,
}
: undefined
const ogImage = getOgUrl(title, '关于我')
return {
title,
description,
Expand Down
File renamed without changes.
17 changes: 7 additions & 10 deletions src/app/notes/[id]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NoteMainContainer } from '~/components/widgets/note/NoteMainContainer'
import { TocFAB } from '~/components/widgets/toc/TocFAB'
import { REQUEST_QUERY } from '~/constants/system'
import { attachUA } from '~/lib/attach-ua'
import { getOgUrl } from '~/lib/helper.server'
import { getSummaryFromMd } from '~/lib/markdown'
import { getQueryClient } from '~/lib/query-client.server'
import {
Expand All @@ -18,7 +19,7 @@ import { CurrentNoteIdProvider } from '~/providers/note/CurrentNoteIdProvider'
import { queries } from '~/queries/definition'

import { Paper } from '../../../components/layout/container/Paper'
import { Transition } from './Transtion'
import { Transition } from './Transition'

export const generateMetadata = async ({
params,
Expand All @@ -32,26 +33,22 @@ export const generateMetadata = async ({
const { data } = await getQueryClient().fetchQuery(
queries.note.byNid(params.id),
)
const { title, images, text } = data
const { title, text } = data
const description = getSummaryFromMd(text ?? '')

const ogImage = images?.length
? {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
url: images[0].src!,
}
: undefined
const ogUrl = getOgUrl(title, '生活记录')

return {
title,
description,
openGraph: {
title,
description,
images: ogImage,
images: ogUrl,
type: 'article',
},
twitter: {
images: ogImage,
images: ogUrl,
title,
description,
card: 'summary_large_image',
Expand Down
15 changes: 8 additions & 7 deletions src/app/posts/(post-detail)/[category]/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
import { CommentAreaRootLazy } from '~/components/widgets/comment'
import { TocFAB } from '~/components/widgets/toc/TocFAB'
import { attachUA } from '~/lib/attach-ua'
import { getOgUrl } from '~/lib/helper.server'
import { getSummaryFromMd } from '~/lib/markdown'
import { getQueryClient } from '~/lib/query-client.server'
import { CurrentPostDataProvider } from '~/providers/post/CurrentPostDataProvider'
Expand All @@ -27,15 +28,15 @@ export const generateMetadata = async ({
const data = await getQueryClient().fetchQuery(
queries.post.bySlug(category, slug),
)
const { title, images, text } = data
const {
title,
category: { name },
text,
} = data
const description = getSummaryFromMd(text ?? '')

const ogImage = images?.length
? {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
url: images[0].src!,
}
: undefined
const ogImage = getOgUrl(title, name)

return {
title,
description,
Expand Down
2 changes: 2 additions & 0 deletions src/constants/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const REQUEST_QUERY = 'request_query'

export const REQUEST_GEO = 'request_geo'
export const REQUEST_IP = 'request_ip'

export const REQUEST_HOST = 'request_host'
20 changes: 20 additions & 0 deletions src/lib/helper.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { headers } from 'next/dist/client/components/headers'

import { REQUEST_HOST } from '~/constants/system'

import { isDev } from './env'

export function escapeXml(unsafe: string) {
return unsafe.replace(/[<>&'"]/g, (c) => {
switch (c) {
Expand All @@ -15,3 +21,17 @@ export function escapeXml(unsafe: string) {
return c
})
}

export const getHost = () => {
const header = headers()
const host = header.get(REQUEST_HOST)

return host
}

export const getOgUrl = (title: string, subtitle: string) => {
const ogUrl = new URL(`${isDev ? 'http' : 'https'}://${getHost()}/api/og`)
ogUrl.searchParams.set('title', title)
ogUrl.searchParams.set('subtitle', subtitle)
return ogUrl
}
2 changes: 2 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import countries from '~/data/countries.json'

import {
REQUEST_GEO,
REQUEST_HOST,
REQUEST_IP,
REQUEST_PATHNAME,
REQUEST_QUERY,
Expand Down Expand Up @@ -47,6 +48,7 @@ export default async function middleware(req: NextRequest) {
requestHeaders.set(REQUEST_QUERY, search)
requestHeaders.set(REQUEST_GEO, geo?.country || 'unknown')
requestHeaders.set(REQUEST_IP, ip || '')
requestHeaders.set(REQUEST_HOST, headers.get('host') || '')

const isApi = pathname.startsWith('/api/')

Expand Down
1 change: 0 additions & 1 deletion src/providers/root/script-inject-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const ScriptInjectProvider = () => {
delete nextProps[key]
}

console.log(nextProps)
return <Script key={props.src} {...nextProps} />
})}
</>
Expand Down

1 comment on commit 5537cfb

@vercel
Copy link

@vercel vercel bot commented on 5537cfb Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
springtide.vercel.app
innei.in

Please sign in to comment.