feat: update OptimizedImage for WebP originals and add getOriginalWebpUrl
This commit is contained in:
@@ -35,6 +35,14 @@ function buildFallbackSrc(src: string, width: number): string {
|
|||||||
return `/uploads-resized/${pathPrefix}${parsed.uuid}.webp?w=${width}`
|
return `/uploads-resized/${pathPrefix}${parsed.uuid}.webp?w=${width}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get the original WebP URL for full-screen display (no resize) */
|
||||||
|
export function getOriginalWebpUrl(src: string): string {
|
||||||
|
const parsed = parseUploadUrl(src)
|
||||||
|
if (!parsed) return src
|
||||||
|
const pathPrefix = parsed.subdir ? `${parsed.subdir}/` : ''
|
||||||
|
return `/uploads/${pathPrefix}${parsed.uuid}.webp`
|
||||||
|
}
|
||||||
|
|
||||||
export function OptimizedImage({
|
export function OptimizedImage({
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { render, screen } from '@testing-library/react'
|
import { render, screen } from '@testing-library/react'
|
||||||
import { describe, it, expect } from 'vitest'
|
import { describe, it, expect } from 'vitest'
|
||||||
import { OptimizedImage } from '@/shared/ui/OptimizedImage'
|
import { OptimizedImage, getOriginalWebpUrl } from '@/shared/ui/OptimizedImage'
|
||||||
|
|
||||||
describe('OptimizedImage', () => {
|
describe('OptimizedImage', () => {
|
||||||
it('renders a plain img for non-upload URLs', () => {
|
it('renders a plain img for non-upload URLs', () => {
|
||||||
@@ -52,3 +52,19 @@ describe('OptimizedImage', () => {
|
|||||||
expect(srcSet).not.toContain('?w=1024')
|
expect(srcSet).not.toContain('?w=1024')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('getOriginalWebpUrl', () => {
|
||||||
|
it('returns original WebP URL for upload URLs', () => {
|
||||||
|
expect(getOriginalWebpUrl('/uploads/abc123.png')).toBe('/uploads/abc123.webp')
|
||||||
|
expect(getOriginalWebpUrl('/uploads/abc123.webp')).toBe('/uploads/abc123.webp')
|
||||||
|
expect(getOriginalWebpUrl('/uploads/abc123.jpeg')).toBe('/uploads/abc123.webp')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles review subdir correctly', () => {
|
||||||
|
expect(getOriginalWebpUrl('/uploads/reviews/def456.jpg')).toBe('/uploads/reviews/def456.webp')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns original src for non-upload URLs', () => {
|
||||||
|
expect(getOriginalWebpUrl('https://example.com/photo.jpg')).toBe('https://example.com/photo.jpg')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user