-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(landing): serve pre-encoded footer artwork #7667
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** @vitest-environment jsdom */ | ||
| import { createHash } from 'node:crypto' | ||
| import { readFile } from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import { renderToStaticMarkup } from 'react-dom/server' | ||
| import sharp from 'sharp' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { Cta } from '@/app/(landing)/components/cta/cta' | ||
| import { FOOTER_ARTWORK } from '@/app/(landing)/components/cta/footer-artwork.generated' | ||
|
|
||
| vi.mock('@/app/(landing)/components/hero-cta', () => ({ HeroCta: () => null })) | ||
|
|
||
| describe('Footer artwork delivery', () => { | ||
| it('renders responsive static sources with lazy, decorative fallbacks and no optimizer requests', () => { | ||
| const host = document.createElement('div') | ||
| host.innerHTML = renderToStaticMarkup(<Cta />) | ||
| expect(host.innerHTML).not.toContain('/_next/image') | ||
| expect(host.querySelectorAll('picture')).toHaveLength(2) | ||
| expect(host.querySelector('picture')?.className).toContain('dark:hidden') | ||
| expect(host.querySelectorAll('picture')[1].className).toContain('dark:block') | ||
| for (const picture of host.querySelectorAll('picture')) { | ||
| expect(picture.closest('[aria-hidden="true"]')).not.toBeNull() | ||
| expect([...picture.querySelectorAll('source')].map((source) => source.type)).toEqual([ | ||
| 'image/avif', | ||
| 'image/webp', | ||
| ]) | ||
| const image = picture.querySelector('img')! | ||
| expect(image.getAttribute('loading')).toBe('lazy') | ||
| expect(image.alt).toBe('') | ||
| expect(image.src).toContain('/landing/footer-artwork/') | ||
| expect(image.getAttribute('style')).toContain('data:image/svg+xml') | ||
| } | ||
| }) | ||
|
|
||
| it('ships every advertised size with the matching dimensions and immutable content hash', async () => { | ||
| for (const artwork of Object.values(FOOTER_ARTWORK)) { | ||
| for (const srcSet of [artwork.avifSrcSet, artwork.webpSrcSet]) { | ||
| for (const entry of srcSet.split(', ')) { | ||
| const [src, descriptor] = entry.split(' ') | ||
| const width = Number.parseInt(descriptor, 10) | ||
| const buffer = await readFile(path.join(process.cwd(), 'public', src)) | ||
| const metadata = await sharp(buffer).metadata() | ||
| expect(metadata.width).toBe(width) | ||
| expect(metadata.height).toBe((width * 9) / 16) | ||
| expect(src).toContain(createHash('sha256').update(buffer).digest('hex').slice(0, 12)) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| }) |
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
21 changes: 21 additions & 0 deletions
21
apps/sim/app/(landing)/components/cta/footer-artwork.generated.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,21 @@ | ||
| /** Generated by scripts/generate-footer-artwork.ts. */ | ||
| export const FOOTER_ARTWORK = { | ||
| light: { | ||
| avifSrcSet: | ||
| '/landing/footer-artwork/light-1200-f29e84fa39b5.avif 1200w, /landing/footer-artwork/light-1920-352cf7c570ad.avif 1920w, /landing/footer-artwork/light-3840-e889bb4bc039.avif 3840w', | ||
| webpSrcSet: | ||
| '/landing/footer-artwork/light-1200-8764ab359e07.webp 1200w, /landing/footer-artwork/light-1920-c9ec24660024.webp 1920w, /landing/footer-artwork/light-3840-6a6c3eab4b02.webp 3840w', | ||
| src: '/landing/footer-artwork/light-3840-6a6c3eab4b02.webp', | ||
| blurDataURL: | ||
| 'data:image/webp;base64,UklGRjYAAABXRUJQVlA4ICoAAACwAQCdASoIAAQAAkA4JaQAAxaZftQAAP75igYWrDK4ciyu9bneK5BAAAA=', | ||
| }, | ||
| dark: { | ||
| avifSrcSet: | ||
| '/landing/footer-artwork/dark-1200-a1f7345fe7f6.avif 1200w, /landing/footer-artwork/dark-1920-638fa2f584cb.avif 1920w, /landing/footer-artwork/dark-3840-c0bf8a67335d.avif 3840w', | ||
| webpSrcSet: | ||
| '/landing/footer-artwork/dark-1200-117c8741a843.webp 1200w, /landing/footer-artwork/dark-1920-2c586b41810a.webp 1920w, /landing/footer-artwork/dark-3840-3140a296c362.webp 3840w', | ||
| src: '/landing/footer-artwork/dark-3840-3140a296c362.webp', | ||
| blurDataURL: | ||
| 'data:image/webp;base64,UklGRiwAAABXRUJQVlA4ICAAAABwAQCdASoIAAQAAkA4JaRtgAAIQAD+9MlFFCUE/BGgAA==', | ||
| }, | ||
| } as const |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
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.
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.
Binary file not shown.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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 @@ | ||
| /** | ||
| * Pre-encodes the footer paintings so visitors never wait for image optimization. | ||
| * Run `bun run scripts/generate-footer-artwork.ts` after changing either master. | ||
| * Generated filenames include the encoded content hash for immutable caching. | ||
| */ | ||
| import { createHash } from 'node:crypto' | ||
| import { mkdir, readdir, unlink, writeFile } from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import { createLogger } from '@sim/logger' | ||
| import sharp from 'sharp' | ||
|
|
||
| const logger = createLogger('FooterArtwork') | ||
| const ROOT = path.resolve(import.meta.dirname, '..') | ||
| const PUBLIC_DIR = path.join(ROOT, 'apps/sim/public/landing') | ||
| const OUTPUT_DIR = path.join(PUBLIC_DIR, 'footer-artwork') | ||
| const MANIFEST = path.join( | ||
| ROOT, | ||
| 'apps/sim/app/(landing)/components/cta/footer-artwork.generated.ts' | ||
| ) | ||
| const WIDTHS = [1200, 1920, 3840] as const | ||
| const THEMES = ['light', 'dark'] as const | ||
| /** Match the existing Next.js quality=90 WebP and AVIF encodes. */ | ||
| const QUALITY = 90 | ||
|
|
||
| interface FooterArtwork { | ||
| avifSrcSet: string | ||
| webpSrcSet: string | ||
| src: string | ||
| blurDataURL: string | ||
| } | ||
|
|
||
| sharp.concurrency(2) | ||
| await mkdir(OUTPUT_DIR, { recursive: true }) | ||
| const generatedFiles = new Set<string>() | ||
| const artwork: Record<string, FooterArtwork> = {} | ||
|
|
||
| for (const theme of THEMES) { | ||
| const master = path.join(PUBLIC_DIR, `cta-san-francisco-painted-${theme}.webp`) | ||
| const sources: Record<'avif' | 'webp', string[]> = { avif: [], webp: [] } | ||
| let fallback = '' | ||
| for (const format of ['avif', 'webp'] as const) { | ||
| for (const width of WIDTHS) { | ||
| const image = sharp(master).rotate().resize(width, undefined, { withoutEnlargement: true }) | ||
| const buffer = await (format === 'avif' | ||
| ? image.avif({ quality: Math.round(QUALITY * (50 / 80)), effort: 3 }) | ||
| : image.webp({ quality: QUALITY }) | ||
| ).toBuffer() | ||
| const hash = createHash('sha256').update(buffer).digest('hex').slice(0, 12) | ||
| const filename = `${theme}-${width}-${hash}.${format}` | ||
| await writeFile(path.join(OUTPUT_DIR, filename), buffer) | ||
| generatedFiles.add(filename) | ||
| const url = `/landing/footer-artwork/${filename}` | ||
| sources[format].push(`${url} ${width}w`) | ||
| if (format === 'webp') fallback = url | ||
| logger.info('Encoded footer artwork', { theme, width, format, bytes: buffer.length }) | ||
| } | ||
| } | ||
| const preview = await sharp(master).resize(8).webp({ quality: 70 }).toBuffer() | ||
| artwork[theme] = { | ||
| avifSrcSet: sources.avif.join(', '), | ||
| webpSrcSet: sources.webp.join(', '), | ||
| src: fallback, | ||
| blurDataURL: `data:image/webp;base64,${preview.toString('base64')}`, | ||
| } | ||
| } | ||
|
|
||
| await writeFile( | ||
| MANIFEST, | ||
| `/** Generated by scripts/generate-footer-artwork.ts. */\nexport const FOOTER_ARTWORK = ${JSON.stringify(artwork, null, 2)} as const\n` | ||
| ) | ||
| const format = Bun.spawn(['bunx', 'biome', 'format', '--write', MANIFEST], { cwd: ROOT }) | ||
| if ((await format.exited) !== 0) throw new Error('Could not format the footer artwork manifest') | ||
|
|
||
| for (const filename of await readdir(OUTPUT_DIR)) { | ||
| if ( | ||
| /^(light|dark)-\d+-[a-f0-9]{12}\.(avif|webp)$/.test(filename) && | ||
| !generatedFiles.has(filename) | ||
| ) { | ||
| await unlink(path.join(OUTPUT_DIR, filename)) | ||
| } | ||
| } |
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.