Unverified Commit 226e81c7 authored by Darsh Patel's avatar Darsh Patel Committed by GitHub

Add `dangerously-unoptimized` loader for next/image (#26847)

* add support for static loader

* update existing test

* add test

* add prettier-ignore

* update docs

* apply suggestions from review

* remove un-needed normalize

* Apply suggestions from code review
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* update test
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 23a47b9f
...@@ -121,6 +121,7 @@ The following Image Optimization cloud providers are included: ...@@ -121,6 +121,7 @@ The following Image Optimization cloud providers are included:
- [Imgix](https://www.imgix.com): `loader: 'imgix'` - [Imgix](https://www.imgix.com): `loader: 'imgix'`
- [Cloudinary](https://cloudinary.com): `loader: 'cloudinary'` - [Cloudinary](https://cloudinary.com): `loader: 'cloudinary'`
- [Akamai](https://www.akamai.com): `loader: 'akamai'` - [Akamai](https://www.akamai.com): `loader: 'akamai'`
- dangerously-unoptimized: uses image directly from `src` prop without optimization (only needed with `next export` or when the built-in `_next/image` optimizer can not be leveraged) `loader: 'dangerously-unoptimized'`
- Default: Works automatically with `next dev`, `next start`, or a custom server - Default: Works automatically with `next dev`, `next start`, or a custom server
If you need a different provider, you can use the [`loader`](/docs/api-reference/next/image.md#loader) prop with `next/image`. If you need a different provider, you can use the [`loader`](/docs/api-reference/next/image.md#loader) prop with `next/image`.
......
...@@ -36,6 +36,7 @@ const loaders = new Map< ...@@ -36,6 +36,7 @@ const loaders = new Map<
['cloudinary', cloudinaryLoader], ['cloudinary', cloudinaryLoader],
['akamai', akamaiLoader], ['akamai', akamaiLoader],
['default', defaultLoader], ['default', defaultLoader],
['dangerously-unoptimized', unoptimizedLoader],
]) ])
const VALID_LAYOUT_VALUES = [ const VALID_LAYOUT_VALUES = [
...@@ -684,6 +685,10 @@ function cloudinaryLoader({ ...@@ -684,6 +685,10 @@ function cloudinaryLoader({
return `${root}${paramsString}${normalizeSrc(src)}` return `${root}${paramsString}${normalizeSrc(src)}`
} }
function unoptimizedLoader({ src }: DefaultImageLoaderProps): string {
return src
}
function defaultLoader({ function defaultLoader({
root, root,
src, src,
......
...@@ -3,6 +3,7 @@ export const VALID_LOADERS = [ ...@@ -3,6 +3,7 @@ export const VALID_LOADERS = [
'imgix', 'imgix',
'cloudinary', 'cloudinary',
'akamai', 'akamai',
'dangerously-unoptimized',
] as const ] as const
export type LoaderValue = typeof VALID_LOADERS[number] export type LoaderValue = typeof VALID_LOADERS[number]
......
module.exports = { // prettier-ignore
images: { module.exports = { /* replaceme */ }
loader: 'cloudinary',
path: 'https://example.com/',
},
}
...@@ -3,13 +3,25 @@ ...@@ -3,13 +3,25 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { nextBuild, nextExport } from 'next-test-utils' import { nextBuild, nextExport, File } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 5) jest.setTimeout(1000 * 60 * 5)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out') const outdir = join(appDir, 'out')
const nextConfig = new File(join(appDir, 'next.config.js'))
describe('Export with cloudinary loader next/image component', () => { describe('Export with cloudinary loader next/image component', () => {
beforeAll(async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
loader: 'cloudinary',
path: 'https://example.com/',
},
})
)
})
it('should build successfully', async () => { it('should build successfully', async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
const { code } = await nextBuild(appDir) const { code } = await nextBuild(appDir)
...@@ -26,4 +38,41 @@ describe('Export with cloudinary loader next/image component', () => { ...@@ -26,4 +38,41 @@ describe('Export with cloudinary loader next/image component', () => {
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('img[alt="icon"]').attr('alt')).toBe('icon') expect($('img[alt="icon"]').attr('alt')).toBe('icon')
}) })
afterAll(async () => {
await nextConfig.restore()
})
})
describe('Export with dangerously-unoptimized loader next/image component', () => {
beforeAll(async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
loader: 'dangerously-unoptimized',
},
})
)
})
it('should build successfully', async () => {
await fs.remove(join(appDir, '.next'))
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build failed with status ${code}`)
})
it('should export successfully', async () => {
const { code } = await nextExport(appDir, { outdir })
if (code !== 0) throw new Error(`export failed with status ${code}`)
})
it('should contain img element with same src in html output', async () => {
const html = await fs.readFile(join(outdir, 'index.html'))
const $ = cheerio.load(html)
expect($('img[alt="icon"]').attr('src')).toBe('/i.png')
})
afterAll(async () => {
await nextConfig.restore()
})
}) })
...@@ -730,7 +730,7 @@ describe('Image Optimizer', () => { ...@@ -730,7 +730,7 @@ describe('Image Optimizer', () => {
await nextConfig.restore() await nextConfig.restore()
expect(stderr).toContain( expect(stderr).toContain(
'Specified images.loader should be one of (default, imgix, cloudinary, akamai), received invalid value (notreal)' 'Specified images.loader should be one of (default, imgix, cloudinary, akamai, dangerously-unoptimized), received invalid value (notreal)'
) )
}) })
}) })
......
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