Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ OGP 設定する #51

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions src/pages/api/og.page.ts

This file was deleted.

86 changes: 86 additions & 0 deletions src/pages/api/og.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ImageResponse } from '@vercel/og'
import { NextRequest } from 'next/server'
import { SmallPreview } from '@/components/preview'
import { themeSchema } from '@/model/theme'
import { resolveTheme } from '@/utils/theme'
import { lightTheme } from '@/utils/theme/default'

export const config = {
runtime: 'experimental-edge',
}

const res = async (req: NextRequest) => {
const author = req.nextUrl.searchParams.get('author') ?? 'traP'
const rawTheme = req.nextUrl.searchParams.get('theme')
if (rawTheme === null) {
const dom = Fallback({})
if (dom === null) {
return new Response('Internal Server Error', { status: 500 })
}
return new ImageResponse(dom, {
width: 1200,
height: 675,
})
}
let theme
try {
theme = themeSchema.parse(JSON.parse(rawTheme))
} catch (err) {
// fallback 考える
theme = lightTheme
}
const resolvedTheme = resolveTheme(theme)

const dom = SmallPreview({
author,
theme: resolvedTheme,
})
if (dom === null) {
return new Response('Not Found', { status: 404 })
}
return new ImageResponse(dom, {
width: 1200,
height: 675,
})
}
export default res

const Fallback: React.FC = () => {
return (
<div style={{ display: 'flex', position: 'relative' }}>
<SmallPreview author={'traP'} theme={resolveTheme(lightTheme)} />
<div
style={{
display: 'flex',
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
alignItems: 'center',
justifyContent: 'center',
}}
>
<span
style={{
backgroundColor: 'rgba(255, 255, 255, 0.9)',
borderRadius: '32px',
}}
>
<span
style={{
backgroundColor: 'rgba(0, 91, 172, 0.3)',
color: 'black',
borderRadius: '32px',
fontSize: '180px',
padding: '32px 64px',
fontWeight: 'bold',
}}
>
QTheme
</span>
</span>
</div>
</div>
)
}