Skip to content

Commit 91c684d

Browse files
committed
fix: jpg image src with jpeg format prop, and vice versa
1 parent 24e7bdb commit 91c684d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

playground/app.vue

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<figcaption>Avif format:</figcaption>
6363
<NuxtPicture format="avif" src="rhino.jpg" />
6464
</figure>
65+
66+
<figure>
67+
<figcaption>Jpeg format prop with jpg image:</figcaption>
68+
<NuxtPicture format="jpeg" src="rhino.jpg" />
69+
</figure>
6570
</div>
6671
</template>
6772

src/runtime/index.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ import { joinURL } from 'ufo'
22
import type { ImageModifiers, ImageOptions, ResolvedImage } from '@nuxt/image'
33
import { useRuntimeConfig } from '#imports'
44

5+
const JPEG_REGEX = /jpe?g/
6+
7+
function addFormatToSrc(src: string, format: string): string {
8+
const extension = src.split('.').pop()
9+
10+
if (
11+
extension === format ||
12+
(extension?.match(JPEG_REGEX) && format.match(JPEG_REGEX)) // Avoid add `.jpg` to `.jpeg`, and vice versa
13+
) {
14+
return src
15+
}
16+
17+
return src + '.' + format
18+
}
19+
520
export interface InterventionRequestImageModifiers extends ImageModifiers {
621
contrast?: number
722
sharpen?: number
@@ -79,9 +94,7 @@ export function getImage(
7994
// process modifiers
8095
const operationsString = operations.join('-')
8196

82-
if (format && !src.endsWith('.' + format)) {
83-
src += '.' + format
84-
}
97+
if (format) src = addFormatToSrc(src, format)
8598

8699
const getUrl = function (): string {
87100
if (src.match('^https?')) {

0 commit comments

Comments
 (0)