Skip to content

Commit 5905fbc

Browse files
authored
Merge pull request #689 from webstackdev/feature/move-social-avatar-to-public-folder
Move social share avatar to public folder
2 parents 817922b + 924145a commit 5905fbc

3 files changed

Lines changed: 11 additions & 25 deletions

File tree

4.08 KB
Loading

src/pages/api/social-card/__tests__/index.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22
import { GET } from '@pages/api/social-card'
3-
4-
vi.mock('@assets/images/avatars/kevin-brown.webp', () => ({
5-
default: {
6-
src: '/_astro/kevin-brown.test.webp',
7-
fsPath: '/virtual/assets/kevin-brown.webp',
8-
},
9-
}))
3+
import { fileURLToPath } from 'node:url'
104

115
type CollectionFixture = Array<{
126
id: string
@@ -73,6 +67,10 @@ const seedCollections = () => {
7367
}
7468

7569
describe('Social Card API - GET /api/social-card', () => {
70+
const expectedAvatarPath = fileURLToPath(
71+
new URL('../../../../assets/images/avatars/kevin-brown.webp', import.meta.url)
72+
)
73+
7674
beforeEach(() => {
7775
generateOpenGraphImageMock.mockReset()
7876
generateOpenGraphImageMock.mockResolvedValue(Buffer.from('mock-image'))
@@ -133,7 +131,7 @@ describe('Social Card API - GET /api/social-card', () => {
133131
expect(generateOpenGraphImageMock).toHaveBeenCalledWith(
134132
expect.objectContaining({
135133
logo: expect.objectContaining({
136-
path: '/virtual/assets/kevin-brown.webp',
134+
path: expectedAvatarPath,
137135
}),
138136
})
139137
)

src/pages/api/social-card/index.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import type { APIRoute } from 'astro'
22
import { getCollection } from 'astro:content'
33
import { generateOpenGraphImage } from 'astro-og-canvas'
4-
import kevinBrownAvatar from '@assets/images/avatars/kevin-brown.webp'
4+
import { fileURLToPath } from 'node:url'
55
import { buildApiErrorResponse, handleApiFunctionError } from '@pages/api/_utils/errors'
66
import { createApiFunctionContext } from '@pages/api/_utils/requestContext'
77

88
export const prerender = false
99

1010
const ROUTE = '/api/social-card'
11+
const AVATAR_FILE_PATH = fileURLToPath(
12+
new URL('../../../assets/images/avatars/kevin-brown.webp', import.meta.url)
13+
)
1114
const DEFAULT_TITLE = 'Platform Engineering by Kevin Brown'
1215
const DEFAULT_DESCRIPTION =
1316
'Platform engineer helping teams harden delivery, modernize cloud platforms, and improve developer experience.'
1417

15-
type ImageMetadataWithFsPath = typeof kevinBrownAvatar & {
16-
fsPath?: string
17-
}
18-
1918
type CollectionKey = 'articles' | 'caseStudies' | 'services' | 'downloads'
2019
type PaletteKey = 'articles' | 'case-studies' | 'services' | 'downloads' | 'default'
2120

@@ -60,16 +59,6 @@ const gradientPalette: Record<PaletteKey, [number, number, number][]> = {
6059
],
6160
}
6261

63-
const getAvatarFilePath = (): string => {
64-
const avatarFilePath = (kevinBrownAvatar as ImageMetadataWithFsPath).fsPath
65-
66-
if (!avatarFilePath) {
67-
throw new Error('Kevin Brown avatar asset is missing a local file path')
68-
}
69-
70-
return avatarFilePath
71-
}
72-
7362
/** Normalize slug parameters to a consistent format */
7463
const normalizeSlug = (value: string | null): string => {
7564
if (!value) return 'home'
@@ -133,7 +122,6 @@ export const GET: APIRoute = async ({ request, clientAddress, cookies }) => {
133122
const slug = normalizeSlug(url.searchParams.get('slug'))
134123
const titleOverride = url.searchParams.get('title')
135124
const descriptionOverride = url.searchParams.get('description')
136-
const avatarPath = getAvatarFilePath()
137125

138126
const contentIndex = await buildContentIndex()
139127
const matchedEntry = contentIndex[slug]
@@ -153,7 +141,7 @@ export const GET: APIRoute = async ({ request, clientAddress, cookies }) => {
153141
bgGradient: gradientPalette[palette],
154142
padding: 80,
155143
logo: {
156-
path: avatarPath,
144+
path: AVATAR_FILE_PATH,
157145
size: [140, 140],
158146
},
159147
font: {

0 commit comments

Comments
 (0)