Unverified Commit 3102899f authored by Steven's avatar Steven Committed by GitHub

Fix squoosh image optimization for Node.js 18 (#38833)

Starting in Node.js 18, the `fetch` global was made available and that caused the squoosh implementation to assume it was on a web browser and try to fetch the wasm file instead of reading it from the filesystem. This PR deletes the code trying to fetch since we know that squoosh only runs in the Image Optimization API, not the browser.

- Fixes #38020 
- Enables a test that was disabled in #38460 
parent 9416e00b
...@@ -379,22 +379,6 @@ var Module = (function () { ...@@ -379,22 +379,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -426,25 +410,7 @@ var Module = (function () { ...@@ -426,25 +410,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -379,22 +379,6 @@ var Module = (function () { ...@@ -379,22 +379,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -426,25 +410,7 @@ var Module = (function () { ...@@ -426,25 +410,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -386,22 +386,6 @@ var Module = (function () { ...@@ -386,22 +386,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -433,25 +417,7 @@ var Module = (function () { ...@@ -433,25 +417,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -386,22 +386,6 @@ var Module = (function () { ...@@ -386,22 +386,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -433,25 +417,7 @@ var Module = (function () { ...@@ -433,25 +417,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -362,22 +362,6 @@ var Module = (function () { ...@@ -362,22 +362,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -409,25 +393,7 @@ var Module = (function () { ...@@ -409,25 +393,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -362,22 +362,6 @@ var Module = (function () { ...@@ -362,22 +362,6 @@ var Module = (function () {
} }
} }
function getBinaryPromise() { function getBinaryPromise() {
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' })
.then(function (response) {
if (!response['ok']) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
)
}
return response['arrayBuffer']()
})
.catch(function () {
return getBinary(wasmBinaryFile)
})
}
}
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
return getBinary(wasmBinaryFile) return getBinary(wasmBinaryFile)
}) })
...@@ -409,25 +393,7 @@ var Module = (function () { ...@@ -409,25 +393,7 @@ var Module = (function () {
}) })
} }
function instantiateAsync() { function instantiateAsync() {
if ( return instantiateArrayBuffer(receiveInstantiationResult)
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === 'function' &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === 'function'
) {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info)
return result.then(receiveInstantiationResult, function (reason) {
err('wasm streaming compile failed: ' + reason)
err('falling back to ArrayBuffer instantiation')
return instantiateArrayBuffer(receiveInstantiationResult)
})
}
)
} else {
return instantiateArrayBuffer(receiveInstantiationResult)
}
} }
if (Module['instantiateWasm']) { if (Module['instantiateWasm']) {
try { try {
......
...@@ -256,9 +256,9 @@ describe('app dir - react server components', () => { ...@@ -256,9 +256,9 @@ describe('app dir - react server components', () => {
expect(manipulated).toBe(undefined) expect(manipulated).toBe(undefined)
}) })
it.skip('should suspense next/image in server components', async () => { it('should suspense next/image in server components', async () => {
const imageHTML = await renderViaHTTP(next.url, '/next-api/image') const imageHTML = await renderViaHTTP(next.url, '/next-api/image')
const imageTag = getNodeBySelector(imageHTML, 'body > span > span > img') const imageTag = getNodeBySelector(imageHTML, '#myimg')
expect(imageTag.attr('src')).toContain('data:image') expect(imageTag.attr('src')).toContain('data:image')
}) })
......
...@@ -3,7 +3,7 @@ import src from '../../../public/test.jpg' ...@@ -3,7 +3,7 @@ import src from '../../../public/test.jpg'
// Keep arrow function to test rsc loaders // Keep arrow function to test rsc loaders
const Page = () => { const Page = () => {
return <NextImage src={src} /> return <NextImage id="myimg" src={src} />
} }
export default Page export default Page
import Image from 'next/image' import Image from 'next/image'
import FutureImage from 'next/future/image' import FutureImage from 'next/future/image'
import png from '../public/test.png'
export default function Page() { export default function Page() {
return ( return (
...@@ -7,7 +8,7 @@ export default function Page() { ...@@ -7,7 +8,7 @@ export default function Page() {
<h1>Example Image Usage</h1> <h1>Example Image Usage</h1>
<Image src="/test.jpg" width={200} height={200} /> <Image src="/test.jpg" width={200} height={200} />
<hr /> <hr />
<FutureImage src="/test.png" width={200} height={200} /> <FutureImage src={png} />
</> </>
) )
} }
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