Skip to content

Commit

Permalink
add image metadata for svg (#48947)
Browse files Browse the repository at this point in the history
### What?

add .svg to the list of images that that return metadata

### Why?

.svg files should also return width and height

### How?

see vercel/turbo#4741
  • Loading branch information
sokra committed Apr 29, 2023
1 parent b33179c commit f2b920b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Expand Up @@ -95,6 +95,7 @@ pub fn get_next_image_rule() -> ModuleRule {
ModuleRuleCondition::ResourcePathEndsWith(".avif".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".apng".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".gif".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".svg".to_string()),
]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Custom(
StructuredImageModuleTypeVc::new().into(),
Expand Down
@@ -1,6 +1,7 @@
import Image from 'next/image'
import { img } from '../components/img'
import broken from '../public/broken.jpeg'
import svg from '../public/test.svg'
import { useEffect } from 'react'

export default function Home() {
Expand All @@ -16,6 +17,7 @@ export default function Home() {
src={img}
placeholder="blur"
/>,
<Image id="svg" alt="test svg image" src={svg} />,
<Image
id="local"
alt="test src image"
Expand All @@ -33,6 +35,11 @@ function runTests() {
expect(img).toHaveProperty('height', 100)
})

it('should return image size for svg', function () {
expect(svg).toHaveProperty('width', 400)
expect(svg).toHaveProperty('height', 400)
})

it('should not return image size for broken images', function () {
expect(broken).toHaveProperty('width', 0)
expect(broken).toHaveProperty('height', 0)
Expand All @@ -47,6 +54,12 @@ function runTests() {
expect(img).toHaveProperty('blurHeight', 7)
})

it('should not have blur placeholder for svg', function () {
expect(svg).toHaveProperty('blurDataURL', null)
expect(svg).toHaveProperty('blurWidth', 0)
expect(svg).toHaveProperty('blurHeight', 0)
})

it('should not have blur placeholder for broken images', function () {
expect(broken).toHaveProperty('blurDataURL', null)
expect(broken).toHaveProperty('blurWidth', 0)
Expand All @@ -58,6 +71,11 @@ function runTests() {
expect(img.src).toContain(encodeURIComponent('_next/static/assets'))
})

it('should link to imported svg image', function () {
const img = document.querySelector('#svg')
expect(img.src).toContain('_next/static/assets')
})

it('should link to local src image', function () {
const img = document.querySelector('#local')
expect(img.src).toContain('triangle-black')
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f2b920b

Please sign in to comment.