Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/src/compiled/image-detector/detector.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 30 additions & 5 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IncomingMessage, ServerResponse } from 'http'
import { mediaType } from 'next/dist/compiled/@hapi/accept'
import contentDisposition from 'next/dist/compiled/content-disposition'
import imageSizeOf from 'next/dist/compiled/image-size'
import { detector } from 'next/dist/compiled/image-detector/detector.js'
import isAnimated from 'next/dist/compiled/is-animated'
import { join } from 'path'
import nodeUrl, { type UrlWithParsedQuery } from 'url'
Expand Down Expand Up @@ -232,11 +233,21 @@ export async function detectContentType(
return JP2
}

const sharp = getSharp(null)
const meta = await sharp(buffer)
.metadata()
.catch((_) => null)
switch (meta?.format) {
let format:
| import('sharp').Metadata['format']
| ReturnType<typeof detector>
| undefined
format = detector(buffer)

if (!format) {
const sharp = getSharp(null)
const meta = await sharp(buffer)
.metadata()
.catch((_) => null)
format = meta?.format
}

switch (format) {
case 'avif':
return AVIF
case 'webp':
Expand All @@ -251,6 +262,7 @@ export async function detectContentType(
case 'svg':
return SVG
case 'jxl':
case 'jxl-stream':
return JXL
case 'jp2':
return JP2
Expand All @@ -259,6 +271,12 @@ export async function detectContentType(
return TIFF
case 'pdf':
return PDF
case 'bmp':
return BMP
case 'ico':
return ICO
case 'icns':
return ICNS
case 'dcraw':
case 'dz':
case 'exr':
Expand All @@ -271,6 +289,13 @@ export async function detectContentType(
case 'rad':
case 'raw':
case 'v':
case 'cur':
case 'dds':
case 'j2c':
case 'ktx':
case 'pnm':
case 'psd':
case 'tga':
case undefined:
default:
return null
Expand Down
12 changes: 12 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,17 @@ export async function ncc_image_size(task, opts) {
.target('src/compiled/image-size')
}

// eslint-disable-next-line camelcase
externals['image-detector'] = 'next/dist/compiled/image-detector'
export async function ncc_image_detector(task, opts) {
// NOTE: remove this special compile step if the upstream PR lands
// https://github.com/image-size/image-size/pull/451
await task
.source(relative(__dirname, require.resolve('image-size/dist/detector.js')))
.ncc({ packageName: 'image-size', externals })
.target('src/compiled/image-detector')
}

// eslint-disable-next-line camelcase
externals['@hapi/accept'] = 'next/dist/compiled/@hapi/accept'
export async function ncc_hapi_accept(task, opts) {
Expand Down Expand Up @@ -2281,6 +2292,7 @@ export async function ncc(task, opts) {
'ncc_p_queue',
'ncc_raw_body',
'ncc_image_size',
'ncc_image_detector',
'ncc_hapi_accept',
'ncc_commander',
'ncc_node_anser',
Expand Down
27 changes: 27 additions & 0 deletions packages/next/types/$$compiled.internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,33 @@ declare module 'next/dist/compiled/image-size' {
export = m
}

declare module 'next/dist/compiled/image-detector/detector.js' {
export function detector(
arr: Uint8Array
):
| 'bmp'
| 'cur'
| 'dds'
| 'gif'
| 'heif'
| 'icns'
| 'ico'
| 'j2c'
| 'jp2'
| 'jpg'
| 'jxl'
| 'jxl-stream'
| 'ktx'
| 'png'
| 'pnm'
| 'psd'
| 'svg'
| 'tga'
| 'tiff'
| 'webp'
| undefined
}

declare module 'next/dist/compiled/@hapi/accept' {
import m from '@hapi/accept'
export = m
Expand Down
Loading