未验证 提交 3f07e554 编写于 作者: S Steven 提交者: GitHub

Fix Image component defaults & remove autoOptimize (#18101)

上级 1a8cb7e1
...@@ -994,7 +994,6 @@ export default async function getBaseWebpackConfig( ...@@ -994,7 +994,6 @@ export default async function getBaseWebpackConfig(
sizes: config.images.sizes, sizes: config.images.sizes,
path: config.images.path, path: config.images.path,
loader: config.images.loader, loader: config.images.loader,
autoOptimize: config.images.autoOptimize,
}), }),
'process.env.__NEXT_ROUTER_BASEPATH': JSON.stringify(config.basePath), 'process.env.__NEXT_ROUTER_BASEPATH': JSON.stringify(config.basePath),
'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites), 'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites),
......
import React, { ReactElement, useEffect, useRef } from 'react' import React, { ReactElement, useEffect, useRef } from 'react'
import Head from '../next-server/lib/head' import Head from '../next-server/lib/head'
const loaders: { [key: string]: (props: LoaderProps) => string } = { const loaders = new Map<LoaderKey, (props: LoaderProps) => string>([
imgix: imgixLoader, ['imgix', imgixLoader],
cloudinary: cloudinaryLoader, ['cloudinary', cloudinaryLoader],
akamai: akamaiLoader, ['akamai', akamaiLoader]
default: defaultLoader, ['default', defaultLoader],
} ])
type LoaderKey = 'imgix' | 'cloudinary' | 'akamai' | 'default'
type ImageData = { type ImageData = {
sizes?: number[] sizes: number[]
loader?: string loader: LoaderKey
path?: string path: string
autoOptimize?: boolean
} }
type ImageProps = Omit< type ImageProps = Omit<
...@@ -30,11 +31,7 @@ type ImageProps = Omit< ...@@ -30,11 +31,7 @@ type ImageProps = Omit<
) )
const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any
const breakpoints = imageData.sizes || [640, 1024, 1600] const { sizes: configSizes, loader: configLoader, path: configPath } = imageData
// Auto optimize defaults to on if not specified
if (imageData.autoOptimize === undefined) {
imageData.autoOptimize = true
}
let cachedObserver: IntersectionObserver let cachedObserver: IntersectionObserver
const IntersectionObserver = const IntersectionObserver =
...@@ -89,8 +86,8 @@ type CallLoaderProps = { ...@@ -89,8 +86,8 @@ type CallLoaderProps = {
} }
function callLoader(loaderProps: CallLoaderProps) { function callLoader(loaderProps: CallLoaderProps) {
let loader = loaders[imageData.loader || 'default'] let load = loaders.get(configLoader) || defaultLoader
return loader({ root: imageData.path || '/_next/image', ...loaderProps }) return load({ root: configPath, ...loaderProps })
} }
type SrcSetData = { type SrcSetData = {
...@@ -187,7 +184,7 @@ export default function Image({ ...@@ -187,7 +184,7 @@ export default function Image({
const imgSrcSet = !unoptimized const imgSrcSet = !unoptimized
? generateSrcSet({ ? generateSrcSet({
src, src,
widths: breakpoints, widths: configSizes,
quality, quality,
}) })
: undefined : undefined
...@@ -266,7 +263,7 @@ export default function Image({ ...@@ -266,7 +263,7 @@ export default function Image({
{shouldPreload {shouldPreload
? generatePreload({ ? generatePreload({
src, src,
widths: breakpoints, widths: configSizes,
unoptimized, unoptimized,
sizes, sizes,
}) })
...@@ -292,7 +289,7 @@ function normalizeSrc(src: string) { ...@@ -292,7 +289,7 @@ function normalizeSrc(src: string) {
} }
function imgixLoader({ root, src, width, quality }: LoaderProps): string { function imgixLoader({ root, src, width, quality }: LoaderProps): string {
const params = [] const params = ['auto=format']
let paramsString = '' let paramsString = ''
if (width) { if (width) {
params.push('w=' + width) params.push('w=' + width)
...@@ -300,9 +297,7 @@ function imgixLoader({ root, src, width, quality }: LoaderProps): string { ...@@ -300,9 +297,7 @@ function imgixLoader({ root, src, width, quality }: LoaderProps): string {
if (quality) { if (quality) {
params.push('q=' + quality) params.push('q=' + quality)
} }
if (imageData.autoOptimize) {
params.push('auto=compress')
}
if (params.length) { if (params.length) {
paramsString = '?' + params.join('&') paramsString = '?' + params.join('&')
} }
...@@ -314,11 +309,8 @@ function akamaiLoader({ root, src, width }: LoaderProps): string { ...@@ -314,11 +309,8 @@ function akamaiLoader({ root, src, width }: LoaderProps): string {
} }
function cloudinaryLoader({ root, src, width, quality }: LoaderProps): string { function cloudinaryLoader({ root, src, width, quality }: LoaderProps): string {
const params = [] const params = ['f_auto']
let paramsString = '' let paramsString = ''
if (!quality && imageData.autoOptimize) {
quality = 'auto'
}
if (width) { if (width) {
params.push('w_' + width) params.push('w_' + width)
} }
......
...@@ -26,7 +26,8 @@ const defaultConfig: { [key: string]: any } = { ...@@ -26,7 +26,8 @@ const defaultConfig: { [key: string]: any } = {
images: { images: {
sizes: [320, 420, 768, 1024, 1200], sizes: [320, 420, 768, 1024, 1200],
domains: [], domains: [],
hosts: { default: { path: '/_next/image' } }, path: '/_next/image',
loader: 'default',
}, },
devIndicators: { devIndicators: {
buildActivity: true, buildActivity: true,
......
module.exports = { module.exports = {
images: { images: {
sizes: [480, 1024, 1600], sizes: [480, 1024, 1600],
autoOptimize: false,
path: 'https://example.com/myaccount/', path: 'https://example.com/myaccount/',
loader: 'imgix', loader: 'imgix',
}, },
......
...@@ -33,26 +33,26 @@ function runTests() { ...@@ -33,26 +33,26 @@ function runTests() {
}) })
it('should modify src with the loader', async () => { it('should modify src with the loader', async () => {
expect(await browser.elementById('basic-image').getAttribute('src')).toBe( expect(await browser.elementById('basic-image').getAttribute('src')).toBe(
'https://example.com/myaccount/foo.jpg?q=60' 'https://example.com/myaccount/foo.jpg?auto=format&q=60'
) )
}) })
it('should correctly generate src even if preceding slash is included in prop', async () => { it('should correctly generate src even if preceding slash is included in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('src') await browser.elementById('preceding-slash-image').getAttribute('src')
).toBe('https://example.com/myaccount/fooslash.jpg') ).toBe('https://example.com/myaccount/fooslash.jpg?auto=format')
}) })
it('should add a srcset based on the loader', async () => { it('should add a srcset based on the loader', async () => {
expect( expect(
await browser.elementById('basic-image').getAttribute('srcset') await browser.elementById('basic-image').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foo.jpg?w=480&q=60 480w, https://example.com/myaccount/foo.jpg?w=1024&q=60 1024w, https://example.com/myaccount/foo.jpg?w=1600&q=60 1600w' 'https://example.com/myaccount/foo.jpg?auto=format&w=480&q=60 480w, https://example.com/myaccount/foo.jpg?auto=format&w=1024&q=60 1024w, https://example.com/myaccount/foo.jpg?auto=format&w=1600&q=60 1600w'
) )
}) })
it('should add a srcset even with preceding slash in prop', async () => { it('should add a srcset even with preceding slash in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('srcset') await browser.elementById('preceding-slash-image').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/fooslash.jpg?w=480 480w, https://example.com/myaccount/fooslash.jpg?w=1024 1024w, https://example.com/myaccount/fooslash.jpg?w=1600 1600w' 'https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w, https://example.com/myaccount/fooslash.jpg?auto=format&w=1024 1024w, https://example.com/myaccount/fooslash.jpg?auto=format&w=1600 1600w'
) )
}) })
it('should support the unoptimized attribute', async () => { it('should support the unoptimized attribute', async () => {
...@@ -70,10 +70,10 @@ function runTests() { ...@@ -70,10 +70,10 @@ function runTests() {
function lazyLoadingTests() { function lazyLoadingTests() {
it('should have loaded the first image immediately', async () => { it('should have loaded the first image immediately', async () => {
expect(await browser.elementById('lazy-top').getAttribute('src')).toBe( expect(await browser.elementById('lazy-top').getAttribute('src')).toBe(
'https://example.com/myaccount/foo1.jpg' 'https://example.com/myaccount/foo1.jpg?auto=format'
) )
expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe( expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe(
'https://example.com/myaccount/foo1.jpg?w=480 480w, https://example.com/myaccount/foo1.jpg?w=1024 1024w, https://example.com/myaccount/foo1.jpg?w=1600 1600w' 'https://example.com/myaccount/foo1.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo1.jpg?auto=format&w=1024 1024w, https://example.com/myaccount/foo1.jpg?auto=format&w=1600 1600w'
) )
}) })
it('should not have loaded the second image immediately', async () => { it('should not have loaded the second image immediately', async () => {
...@@ -101,11 +101,11 @@ function lazyLoadingTests() { ...@@ -101,11 +101,11 @@ function lazyLoadingTests() {
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('src') return browser.elementById('lazy-mid').getAttribute('src')
}, 'https://example.com/myaccount/foo2.jpg') }, 'https://example.com/myaccount/foo2.jpg?auto=format')
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('srcset') return browser.elementById('lazy-mid').getAttribute('srcset')
}, 'https://example.com/myaccount/foo2.jpg?w=480 480w, https://example.com/myaccount/foo2.jpg?w=1024 1024w, https://example.com/myaccount/foo2.jpg?w=1600 1600w') }, 'https://example.com/myaccount/foo2.jpg?auto=format&w=480 480w, https://example.com/myaccount/foo2.jpg?auto=format&w=1024 1024w, https://example.com/myaccount/foo2.jpg?auto=format&w=1600 1600w')
}) })
it('should not have loaded the third image after scrolling down', async () => { it('should not have loaded the third image after scrolling down', async () => {
expect( expect(
...@@ -166,14 +166,14 @@ describe('Image Component Tests', () => { ...@@ -166,14 +166,14 @@ describe('Image Component Tests', () => {
it('should add a preload tag for a priority image', async () => { it('should add a preload tag for a priority image', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriority.png' 'https://example.com/myaccount/withpriority.png?auto=format'
) )
).toBe(true) ).toBe(true)
}) })
it('should add a preload tag for a priority image with preceding slash', async () => { it('should add a preload tag for a priority image with preceding slash', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/fooslash.jpg' 'https://example.com/myaccount/fooslash.jpg?auto=format'
) )
).toBe(true) ).toBe(true)
}) })
...@@ -197,7 +197,7 @@ describe('Image Component Tests', () => { ...@@ -197,7 +197,7 @@ describe('Image Component Tests', () => {
it('should NOT add a preload tag for a priority image', async () => { it('should NOT add a preload tag for a priority image', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriorityclient.png' 'https://example.com/myaccount/withpriorityclient.png?auto=format'
) )
).toBe(false) ).toBe(false)
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册