Unverified Commit f2b920b2 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add image metadata for svg (#48947)

### What?

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

### Why?

.svg files should also return width and height

### How?

see https://github.com/vercel/turbo/pull/4741
parent b33179cc
...@@ -95,6 +95,7 @@ pub fn get_next_image_rule() -> ModuleRule { ...@@ -95,6 +95,7 @@ pub fn get_next_image_rule() -> ModuleRule {
ModuleRuleCondition::ResourcePathEndsWith(".avif".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".avif".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".apng".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".apng".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".gif".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".gif".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".svg".to_string()),
]), ]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Custom( vec![ModuleRuleEffect::ModuleType(ModuleType::Custom(
StructuredImageModuleTypeVc::new().into(), StructuredImageModuleTypeVc::new().into(),
......
import Image from 'next/image' import Image from 'next/image'
import { img } from '../components/img' import { img } from '../components/img'
import broken from '../public/broken.jpeg' import broken from '../public/broken.jpeg'
import svg from '../public/test.svg'
import { useEffect } from 'react' import { useEffect } from 'react'
export default function Home() { export default function Home() {
...@@ -16,6 +17,7 @@ export default function Home() { ...@@ -16,6 +17,7 @@ export default function Home() {
src={img} src={img}
placeholder="blur" placeholder="blur"
/>, />,
<Image id="svg" alt="test svg image" src={svg} />,
<Image <Image
id="local" id="local"
alt="test src image" alt="test src image"
...@@ -33,6 +35,11 @@ function runTests() { ...@@ -33,6 +35,11 @@ function runTests() {
expect(img).toHaveProperty('height', 100) 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 () { it('should not return image size for broken images', function () {
expect(broken).toHaveProperty('width', 0) expect(broken).toHaveProperty('width', 0)
expect(broken).toHaveProperty('height', 0) expect(broken).toHaveProperty('height', 0)
...@@ -47,6 +54,12 @@ function runTests() { ...@@ -47,6 +54,12 @@ function runTests() {
expect(img).toHaveProperty('blurHeight', 7) 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 () { it('should not have blur placeholder for broken images', function () {
expect(broken).toHaveProperty('blurDataURL', null) expect(broken).toHaveProperty('blurDataURL', null)
expect(broken).toHaveProperty('blurWidth', 0) expect(broken).toHaveProperty('blurWidth', 0)
...@@ -58,6 +71,11 @@ function runTests() { ...@@ -58,6 +71,11 @@ function runTests() {
expect(img.src).toContain(encodeURIComponent('_next/static/assets')) 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 () { it('should link to local src image', function () {
const img = document.querySelector('#local') const img = document.querySelector('#local')
expect(img.src).toContain('triangle-black') expect(img.src).toContain('triangle-black')
......
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="400" height="400" viewBox="0 0 400 400"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
-2000z m2401 118 l396 -693 -398 -3 c-220 -1 -578 -1 -798 0 l-398 3 396 693
c217 380 398 692 401 692 3 0 184 -312 401 -692z"/>
</g>
</svg>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment