-
Notifications
You must be signed in to change notification settings - Fork 37
perf(share): cuts share card generation cost #3148
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
projects/client/src/routes/api/shareable-image/_internal/loadShareFonts.spec.ts
This file contains hidden or 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,81 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| type LoadShareFonts = typeof import('./loadShareFonts.ts')['loadShareFonts']; | ||
| type Bucket = Parameters<LoadShareFonts>[0]['bucket']; | ||
|
|
||
| const REGULAR = 'assets/fonts/NotoSans-Regular.ttf'; | ||
| const BOLD = 'assets/fonts/NotoSans-Bold.ttf'; | ||
|
|
||
| async function freshLoader(): Promise<LoadShareFonts> { | ||
| vi.resetModules(); | ||
| const { loadShareFonts } = await import('./loadShareFonts.ts'); | ||
|
|
||
| return loadShareFonts; | ||
| } | ||
|
|
||
| function bucketWith(paths: ReadonlyArray<string>) { | ||
| const get = vi.fn((path: string) => { | ||
| if (!paths.includes(path)) { | ||
| return Promise.resolve(null); | ||
| } | ||
|
|
||
| return Promise.resolve({ | ||
| arrayBuffer: () => Promise.resolve(new ArrayBuffer(8)), | ||
| }); | ||
| }); | ||
|
|
||
| return { get } as unknown as Bucket & { get: typeof get }; | ||
| } | ||
|
|
||
| describe('util: loadShareFonts', () => { | ||
| describe('when the bucket has both weights', () => { | ||
| it('should return a regular and a bold face', async () => { | ||
| const loadShareFonts = await freshLoader(); | ||
|
|
||
| const fonts = await loadShareFonts({ | ||
| bucket: bucketWith([REGULAR, BOLD]), | ||
| }); | ||
|
|
||
| expect(fonts?.map((font) => font.weight)).toEqual([400, 700]); | ||
| }); | ||
|
|
||
| it('should read each face only once across calls', async () => { | ||
| const loadShareFonts = await freshLoader(); | ||
| const bucket = bucketWith([REGULAR, BOLD]); | ||
|
|
||
| await loadShareFonts({ bucket }); | ||
| await loadShareFonts({ bucket }); | ||
|
|
||
| expect(bucket.get).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when the fonts are unavailable', () => { | ||
| it('should fall back when there is no bucket', async () => { | ||
| const loadShareFonts = await freshLoader(); | ||
|
|
||
| const fonts = await loadShareFonts({ bucket: undefined }); | ||
|
|
||
| expect(fonts).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should fall back when a face is missing', async () => { | ||
| const loadShareFonts = await freshLoader(); | ||
|
|
||
| const fonts = await loadShareFonts({ bucket: bucketWith([REGULAR]) }); | ||
|
|
||
| expect(fonts).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should retry on the next call rather than caching the failure', async () => { | ||
| const loadShareFonts = await freshLoader(); | ||
|
|
||
| await loadShareFonts({ bucket: bucketWith([]) }); | ||
| const fonts = await loadShareFonts({ | ||
| bucket: bucketWith([REGULAR, BOLD]), | ||
| }); | ||
|
|
||
| expect(fonts?.map((font) => font.weight)).toEqual([400, 700]); | ||
| }); | ||
| }); | ||
| }); |
54 changes: 54 additions & 0 deletions
54
projects/client/src/routes/api/shareable-image/_internal/loadShareFonts.ts
This file contains hidden or 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,54 @@ | ||
| import { error } from '$lib/utils/console/print.ts'; | ||
| import type { R2Bucket } from '@cloudflare/workers-types'; | ||
| import type { ImageResponseOptions } from '@ethercorps/sveltekit-og'; | ||
|
|
||
| type ShareFonts = NonNullable<ImageResponseOptions['fonts']>; | ||
|
|
||
| type LoadShareFontsProps = { | ||
| bucket: Pick<R2Bucket, 'get'> | Nil; | ||
| }; | ||
|
|
||
| const FONT_FAMILY = 'Inter'; | ||
|
|
||
| const FONT_SOURCES = [ | ||
| { path: 'assets/fonts/NotoSans-Regular.ttf', weight: 400 }, | ||
| { path: 'assets/fonts/NotoSans-Bold.ttf', weight: 700 }, | ||
| ] as const; | ||
|
|
||
| let cachedFonts: ShareFonts | undefined; | ||
|
|
||
| export async function loadShareFonts( | ||
| { bucket }: LoadShareFontsProps, | ||
| ): Promise<ShareFonts | undefined> { | ||
| if (cachedFonts) { | ||
| return cachedFonts; | ||
| } | ||
|
|
||
| if (!bucket) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| cachedFonts = await Promise.all( | ||
| FONT_SOURCES.map(async ({ path, weight }) => { | ||
| const object = await bucket.get(path); | ||
|
|
||
| if (!object) { | ||
| throw new Error(`Missing font in R2: ${path}`); | ||
| } | ||
|
|
||
| return { | ||
| name: FONT_FAMILY, | ||
| data: await object.arrayBuffer(), | ||
| weight, | ||
| style: 'normal', | ||
| } as const; | ||
| }), | ||
| ); | ||
|
|
||
| return cachedFonts; | ||
| } catch (e) { | ||
| error('Failed to load share fonts, falling back to the bundled set:', e); | ||
| return; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.