Unverified Commit 28f65ff4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix image-optimizer requires in next-server (#34141)

parent 2e0598d0
...@@ -95,7 +95,7 @@ export interface NodeRequestHandler { ...@@ -95,7 +95,7 @@ export interface NodeRequestHandler {
} }
export default class NextNodeServer extends BaseServer { export default class NextNodeServer extends BaseServer {
private imageResponseCache: ResponseCache private imageResponseCache?: ResponseCache
constructor(options: Options) { constructor(options: Options) {
// Initialize super class // Initialize super class
...@@ -117,6 +117,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -117,6 +117,7 @@ export default class NextNodeServer extends BaseServer {
process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true) process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true)
} }
if (!this.minimalMode) {
const { ImageOptimizerCache } = const { ImageOptimizerCache } =
require('./image-optimizer') as typeof import('./image-optimizer') require('./image-optimizer') as typeof import('./image-optimizer')
...@@ -127,6 +128,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -127,6 +128,7 @@ export default class NextNodeServer extends BaseServer {
}) })
) )
} }
}
private compression = private compression =
this.nextConfig.compress && this.nextConfig.target === 'server' this.nextConfig.compress && this.nextConfig.target === 'server'
...@@ -170,8 +172,6 @@ export default class NextNodeServer extends BaseServer { ...@@ -170,8 +172,6 @@ export default class NextNodeServer extends BaseServer {
} }
protected generateImageRoutes(): Route[] { protected generateImageRoutes(): Route[] {
const { getHash, ImageOptimizerCache, sendResponse, ImageError } =
require('./image-optimizer') as typeof import('./image-optimizer')
return [ return [
{ {
match: route('/_next/image'), match: route('/_next/image'),
...@@ -185,6 +185,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -185,6 +185,15 @@ export default class NextNodeServer extends BaseServer {
finished: true, finished: true,
} }
} }
const { getHash, ImageOptimizerCache, sendResponse, ImageError } =
require('./image-optimizer') as typeof import('./image-optimizer')
if (!this.imageResponseCache) {
throw new Error(
'invariant image optimizer cache was not initialized'
)
}
const imagesConfig = this.nextConfig.images const imagesConfig = this.nextConfig.images
if (imagesConfig.loader !== 'default') { if (imagesConfig.loader !== 'default') {
......
...@@ -21,6 +21,9 @@ describe('should set-up next', () => { ...@@ -21,6 +21,9 @@ describe('should set-up next', () => {
let requiredFilesManifest let requiredFilesManifest
beforeAll(async () => { beforeAll(async () => {
// test build against environment with next support
process.env.NOW_BUILDER = '1'
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(join(__dirname, 'required-server-files/pages')), pages: new FileRef(join(__dirname, 'required-server-files/pages')),
......
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