Skip to content

Commit 2ba98ff

Browse files
committedJan 14, 2025·
fix: broken public image resolving
Maybe fixes #297
1 parent fcdd0cc commit 2ba98ff

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed
 

‎playground/components/OgImage/WithImage.vue

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const containerStyles = {
2424
<div :style="{ position: 'absolute', top: '170px', left: '250px', fontSize: '25px', display: 'flex', alignItems: 'center' }">
2525
<img src="/harlan-wilton.jpeg" width="100" height="100" :style="{ borderRadius: '12px', marginRight: '8px' }">
2626
</div>
27+
<img src="harlan-wilton.jpeg" width="100" height="100" :style="{ borderRadius: '12px', marginRight: '8px' }">
28+
<img src="assets/static/images/picture.jpg" width="100" height="100" :style="{ borderRadius: '12px', marginRight: '8px' }">
2729
<img src="https://avatars.githubusercontent.com/u/5326365?v=4" width="200" height="200" alt="absolute test">
2830
</div>
2931
</template>
Loading

‎playground/tailwind.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

‎src/runtime/pure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ function detectBase64MimeType(data: string) {
1212
}
1313

1414
for (const s in signatures) {
15-
if (data.indexOf(s) === 0)
15+
if (data.startsWith(s)) {
1616
return signatures[s as keyof typeof signatures]
17+
}
1718
}
1819
return 'image/svg+xml'
1920
}
2021

2122
export function toBase64Image(data: string | ArrayBuffer) {
2223
const base64 = typeof data === 'string' ? data : Buffer.from(data).toString('base64')
2324
const type = detectBase64MimeType(base64)
24-
2525
return `data:${type};base64,${base64}`
2626
}
2727

‎src/runtime/server/og-image/satori/plugins/imageSrc.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OgImageRenderEventContext, VNode } from '../../../../types'
22
import { useNitroOrigin, useStorage } from '#imports'
33
import sizeOf from 'image-size'
4-
import { withBase } from 'ufo'
4+
import { withBase, withoutLeadingSlash } from 'ufo'
55
import { toBase64Image } from '../../../../pure'
66
import { logger } from '../../../util/logger'
77
import { defineSatoriTransformer } from '../utils'
@@ -10,7 +10,12 @@ async function resolveLocalFilePathImage(publicStoragePath: string, src: string)
1010
// try hydrating from storage
1111
// we need to read the file using unstorage
1212
// because we can't fetch public files using $fetch when prerendering
13-
const key = `${publicStoragePath}${src.replace('./', ':').replace('/', ':')}`
13+
const normalizedSrc = withoutLeadingSlash(src
14+
.replace('_nuxt/@fs/', '')
15+
.replace('_nuxt/', '')
16+
.replace('./', ''),
17+
)
18+
const key = `${publicStoragePath}:${normalizedSrc}`
1419
if (await useStorage().hasItem(key))
1520
return await useStorage().getItemRaw(key)
1621
}

‎src/runtime/server/og-image/satori/renderer.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OgImageRenderEventContext, Renderer, ResolvedFontConfig } from '..
33
import { fontCache } from '#og-image-cache'
44
import { theme } from '#og-image-virtual/unocss-config.mjs'
55
import { defu } from 'defu'
6+
import { sendError } from 'h3'
67
import { normaliseFontInput, useOgImageRuntimeConfig } from '../../../shared'
78
import { loadFont } from './font'
89
import { useResvg, useSatori, useSharp } from './instances'
@@ -58,7 +59,9 @@ export async function createSvg(event: OgImageRenderEventContext) {
5859
width: options.width!,
5960
height: options.height!,
6061
}) as SatoriOptions
61-
return satori(vnodes, satoriOptions)
62+
return satori(vnodes, satoriOptions).catch((err) => {
63+
return sendError(event.e, err, import.meta.dev)
64+
})
6265
}
6366

6467
async function createPng(event: OgImageRenderEventContext) {
@@ -93,9 +96,13 @@ const SatoriRenderer: Renderer = {
9396
}
9497
},
9598
async debug(e) {
99+
const [vnodes, svg] = await Promise.all([
100+
createVNodes(e),
101+
createSvg(e),
102+
])
96103
return {
97-
vnodes: await createVNodes(e),
98-
svg: await createSvg(e),
104+
vnodes,
105+
svg,
99106
}
100107
},
101108
}

0 commit comments

Comments
 (0)
Please sign in to comment.