-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use lightweight og image generator
- Loading branch information
Showing
11 changed files
with
194 additions
and
103 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit' | ||
|
||
export default defineNuxtModule({ | ||
setup(_, nuxt) { | ||
if (nuxt.options._prepare) { | ||
return | ||
} | ||
const resolver = createResolver(import.meta.url) | ||
|
||
addServerHandler({ | ||
route: '/_og/**', | ||
handler: resolver.resolve('./runtime/handler'), | ||
}) | ||
|
||
nuxt.options.nitro.serverAssets ??= [] | ||
nuxt.options.nitro.serverAssets.push({ | ||
baseName: 'og-image', | ||
dir: resolver.resolve('./runtime/assets'), | ||
}) | ||
|
||
nuxt.hook('nitro:init', (nitro) => { | ||
nitro.hooks.hook('prerender:generate', (route) => { | ||
if (route.route.startsWith('/_og/')) { | ||
// temp patch for nitro prerenderer | ||
route.route = route.route.split('?')[0] | ||
route.fileName = route.fileName.split('?')[0] | ||
} | ||
}) | ||
}) | ||
}, | ||
}) |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { defineLazyEventHandler, setHeader, getQuery } from 'h3' | ||
export default defineLazyEventHandler(async () => { | ||
const { Resvg } = await import('@resvg/resvg-js') | ||
|
||
// Read server assets | ||
const nunito = await useStorage().getItemRaw('assets:og-image:nunito.ttf') | ||
const svgTemplate = (await useStorage().getItem('assets:og-image:template.svg')) as string | ||
|
||
return defineEventHandler((event) => { | ||
// Use this for HMR | ||
// const svgTemplate = (await useStorage().getItem('assets:og-image:template.svg')) as string | ||
|
||
const { name = '', title = '', description = '' } = getQuery(event) as Record<string, string> | ||
|
||
const descriptionLines = _wrapLine(decodeURIComponent(description), 60) | ||
const svg = svgTemplate | ||
.replace('!name', decodeURIComponent(name)) | ||
.replace('!title', decodeURIComponent(title)) | ||
.replace('!description1', descriptionLines[0] || '') | ||
.replace('!description2', descriptionLines[1] || '') | ||
.replace('!description3', descriptionLines[2] || '') | ||
|
||
// https://github.com/yisibl/resvg-js | ||
const resvg = new Resvg(svg, { | ||
background: '', | ||
dpi: 600, | ||
fitTo: { | ||
mode: 'width', | ||
value: 1200, | ||
}, | ||
font: { | ||
// @ts-expect-error missing types | ||
fontBuffers: [nunito], | ||
}, | ||
}) | ||
const pngData = resvg.render() | ||
const pngBuffer = pngData.asPng() | ||
|
||
setHeader(event, 'Content-Type', 'image/png') | ||
return pngBuffer | ||
}) | ||
}) | ||
|
||
function _wrapLine(input: string, width: number) { | ||
const lines: string[] = [] | ||
let line: string = '' | ||
for (const word of input.split(' ')) { | ||
if (line.length + word.length >= width) { | ||
lines.push(line) | ||
line = '' | ||
} | ||
line += word + ' ' | ||
} | ||
lines.push(line) | ||
return lines | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export interface PageMeta { | ||
title: string | ||
ogTitle: string | ||
description: string | ||
} | ||
|
||
export function usePageSEO(page: PageMeta) { | ||
if (!process.server && !import.meta.dev) { | ||
return | ||
} | ||
|
||
const route = useRoute() | ||
const appConfig = useAppConfig() | ||
|
||
const path = route.path === '/' ? '/_index' : route.path | ||
const canonicalURL = import.meta.dev ? useRequestURL() : appConfig.site.url | ||
const ogURL = new URL(`/_og${path}.png`, canonicalURL) | ||
|
||
ogURL.searchParams.set('name', appConfig.site.name) | ||
ogURL.searchParams.set('title', page.ogTitle || page.title || appConfig.site.name) | ||
ogURL.searchParams.set('description', page.description || appConfig.site.description || '') | ||
|
||
if (import.meta.prerender) { | ||
prerenderRoutes(ogURL.pathname + ogURL.search) | ||
ogURL.searchParams.delete('name') | ||
ogURL.searchParams.delete('title') | ||
ogURL.searchParams.delete('description') | ||
} | ||
|
||
useSeoMeta({ | ||
title: page.title, | ||
description: page.description, | ||
ogImage: { | ||
url: ogURL.href, | ||
width: 1200, | ||
height: 600, | ||
type: 'image/png', | ||
alt: page.description || appConfig.site.description, | ||
}, | ||
twitterImage: { | ||
url: ogURL.href, | ||
width: 1200, | ||
height: 600, | ||
alt: page.description || appConfig.site.description, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters