Skip to content

Commit

Permalink
fix(nuxt3): catch fonts data
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed May 14, 2023
1 parent 3a468b7 commit a03ae42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
4 changes: 2 additions & 2 deletions server/nuxt3/server/api/webcard.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { GithubRepoMeta, TwitterTweetMeta, WebInfoData } from '@starnexus/c
import { errorMessage } from '@starnexus/core'
import type { Component } from 'vue'
import { satori } from '../utils/WebCard/satori'
import { initBaseFonts, loadDynamicAsset } from '../utils/WebCard/font'
import { initBasicFonts, loadDynamicAsset } from '../utils/WebCard/font'
import TweetCard from '../utils/WebCard/TweetCard.vue'
import CommonCard from '../utils/WebCard/CommonCard.vue'

Expand Down Expand Up @@ -94,7 +94,7 @@ export default eventHandler(async (event) => {
if (!card)
throw new Error(`No WebCard template for ${webMeta.siteName}`)

const fonts = await initBaseFonts()
const fonts = await initBasicFonts()
svg = await satori(card, {
props,
width: 1200,
Expand Down
45 changes: 18 additions & 27 deletions server/nuxt3/server/utils/WebCard/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ async function loadAsset(emojiType: keyof typeof apis, code: string, text: strin

export const loadDynamicAsset = withCache(loadAsset)

export async function initBaseFonts() {
const basicFonts: SatoriOptions['fonts'] = []
export async function initBasicFonts() {
if (basicFonts.length)
return basicFonts
// const interRegPath = join(process.cwd(), 'public', 'fonts', 'Inter-Regular.ttf')
// const InterReg = await fs.readFile(resolve(__dirname, '../../../assets/fonts/Inter-Regular.ttf'))
// const interBoldPath = join(process.cwd(), 'public', 'fonts', 'Inter-Bold.ttf')
Expand All @@ -324,30 +327,18 @@ export async function initBaseFonts() {
// https://unpkg.com/browse/@fontsource/inter@4.5.2/files/
const interRegular = await $fetch('https://unpkg.com/@fontsource/inter@4.5.2/files/inter-all-400-normal.woff', { responseType: 'arrayBuffer' })
const InterBold = await $fetch('https://unpkg.com/@fontsource/inter@4.5.2/files/inter-all-700-normal.woff', { responseType: 'arrayBuffer' })
return [
// {
// name: 'Noto Sans SC',
// data: NotoSansSC,
// weight: 400,
// style: 'normal',
// },
{
name: 'Inter',
data: interRegular,
weight: 400,
style: 'normal',
},
{
name: 'Inter',
data: InterBold,
weight: 700,
style: 'normal',
},
// {
// name: 'M Plus 1p',
// data: fontData,
// weight: 400,
// style: 'normal',
// },
] as SatoriOptions['fonts']

basicFonts.push({
name: 'Inter',
data: interRegular,
weight: 400,
style: 'normal',
})
basicFonts.push({
name: 'Inter',
data: InterBold,
weight: 700,
style: 'normal',
})
return basicFonts as SatoriOptions['fonts']
}

1 comment on commit a03ae42

@vercel
Copy link

@vercel vercel bot commented on a03ae42 May 14, 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:

star-nexus – ./

star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app
star-nexus.vercel.app

Please sign in to comment.