Files
supabase/apps/www/lib/blog-images.test.ts
Alan Daniel 2ab1083b21 fix(www): bust social-media cache for dynamic OG images (#44673)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Bug fix

## What is the current behavior?

Social-media crawlers (X, LinkedIn, etc.) cache the dynamic OG images
generated by the `generate-og` Edge Function. When an OG image is
updated or a crawler caches the wrong image, the stale version persists
indefinitely because the URL doesn't change between deploys.

## What is the new behavior?

`getAbsoluteBlogSocialImage` now appends a `&v={timestamp}` parameter to
OG image URLs that point to the `generate-og` Edge Function. The
timestamp is evaluated once at build time (`Date.now()`), so each deploy
produces unique meta tag URLs that bypass crawler caches. Static image
URLs are left untouched.

## Additional context

Only affects the `og:image` and `twitter:image` meta tags — on-site
thumbnails and other image references are unchanged.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Bug Fixes**
* Fixed cache refresh behavior for dynamically generated OG images to
ensure updated content is served with each deployment.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-08 10:03:59 -04:00

111 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
BLOG_PLACEHOLDER_IMAGE,
getAbsoluteBlogSocialImage,
getBlogSocialImage,
getBlogThumbnailImage,
resolveBlogImagePath,
validateBlogFrontmatterImages,
} from './blog-images'
describe('blog image helpers', () => {
it('prefers imgThumb for on-site blog thumbnails', () => {
expect(
getBlogThumbnailImage({
imgThumb: 'example/thumb.png',
imgSocial: 'example/og.png',
})
).toBe('/images/blog/example/thumb.png')
})
it('falls back to imgSocial for on-site blog thumbnails', () => {
expect(
getBlogThumbnailImage({
imgSocial: 'example/og.png',
})
).toBe('/images/blog/example/og.png')
})
it('prefers imgSocial for social metadata', () => {
expect(
getBlogSocialImage({
imgThumb: 'example/thumb.png',
imgSocial: 'example/og.png',
})
).toBe('/images/blog/example/og.png')
})
it('returns absolute URLs unchanged and prefixes relative paths', () => {
expect(resolveBlogImagePath('https://example.com/og.png')).toBe('https://example.com/og.png')
expect(resolveBlogImagePath('/images/blog/example/og.png')).toBe('/images/blog/example/og.png')
expect(resolveBlogImagePath('example/og.png')).toBe('/images/blog/example/og.png')
})
it('builds absolute social image URLs', () => {
expect(
getAbsoluteBlogSocialImage(
{
imgSocial: 'example/og.png',
},
'https://supabase.com'
)
).toBe('https://supabase.com/images/blog/example/og.png')
})
it('appends a cache-busting param to dynamic OG function URLs', () => {
const url = getAbsoluteBlogSocialImage(
{
imgSocial:
'https://zhfonblqamxferhoguzj.supabase.co/functions/v1/generate-og?template=announcement',
},
'https://supabase.com'
)
expect(url).toMatch(
/^https:\/\/zhfonblqamxferhoguzj\.supabase\.co\/functions\/v1\/generate-og\?template=announcement&v=.+$/
)
})
it('does not append a cache-busting param to static image URLs', () => {
expect(
getAbsoluteBlogSocialImage({ imgSocial: 'example/og.png' }, 'https://supabase.com')
).toBe('https://supabase.com/images/blog/example/og.png')
})
it('uses the placeholder when neither image is present', () => {
expect(getBlogThumbnailImage({})).toBe(BLOG_PLACEHOLDER_IMAGE)
expect(getBlogThumbnailImage({}, { fallbackToPlaceholder: false })).toBeUndefined()
})
it('warns when blog frontmatter is missing one of the image fields', () => {
const warn = vi.fn()
validateBlogFrontmatterImages(
{
imgThumb: 'example/thumb.png',
},
'/tmp/example-post.mdx',
warn
)
expect(warn).toHaveBeenCalledWith(expect.stringContaining('missing "imgSocial"'))
})
it('warns when a blog image uses the prefixed public path', () => {
const warn = vi.fn()
validateBlogFrontmatterImages(
{
imgThumb: '/images/blog/example/thumb.png',
imgSocial: 'example/og.png',
},
'/tmp/example-prefixed-post.mdx',
warn
)
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('should not include the "/images/blog/" prefix')
)
})
})