Skip to content

Commit e40b75b

Browse files
committed
refactor: move check for env var into setupImageFunction
ensure basePath redirect is still added
1 parent f426c98 commit e40b75b

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

packages/runtime/src/helpers/functions.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { join, relative, resolve } from 'pathe'
77
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
88
import { getHandler } from '../templates/getHandler'
99
import { getPageResolver } from '../templates/getPageResolver'
10+
import { isEnvSet } from './utils'
1011

1112
export const generateFunctions = async (
1213
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
@@ -69,35 +70,39 @@ export const setupImageFunction = async ({
6970
remotePatterns: RemotePattern[]
7071
responseHeaders?: Record<string, string>
7172
}): Promise<void> => {
72-
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
73-
const functionName = `${IMAGE_FUNCTION_NAME}.js`
74-
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME)
75-
76-
await ensureDir(functionDirectory)
77-
await writeJSON(join(functionDirectory, 'imageconfig.json'), {
78-
...imageconfig,
79-
basePath: [basePath, IMAGE_FUNCTION_NAME].join('/'),
80-
remotePatterns,
81-
responseHeaders,
82-
})
83-
await copyFile(join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), join(functionDirectory, functionName))
73+
74+
if(!isEnvSet('DISABLE_IPX')) {
75+
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
76+
const functionName = `${IMAGE_FUNCTION_NAME}.js`
77+
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME)
8478

85-
const imagePath = imageconfig.path || '/_next/image'
79+
await ensureDir(functionDirectory)
80+
await writeJSON(join(functionDirectory, 'imageconfig.json'), {
81+
...imageconfig,
82+
basePath: [basePath, IMAGE_FUNCTION_NAME].join('/'),
83+
remotePatterns,
84+
responseHeaders,
85+
})
86+
87+
await copyFile(join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), join(functionDirectory, functionName))
8688

87-
// If we have edge functions then the request will have already been rewritten
88-
// so this won't match. This is matched if edge is disabled or unavailable.
89-
netlifyConfig.redirects.push({
90-
from: `${imagePath}*`,
91-
query: { url: ':url', w: ':width', q: ':quality' },
92-
to: `${basePath}/${IMAGE_FUNCTION_NAME}/w_:width,q_:quality/:url`,
93-
status: 301,
94-
})
89+
const imagePath = imageconfig.path || '/_next/image'
9590

96-
netlifyConfig.redirects.push({
97-
from: `${basePath}/${IMAGE_FUNCTION_NAME}/*`,
98-
to: `/.netlify/builders/${IMAGE_FUNCTION_NAME}`,
99-
status: 200,
100-
})
91+
// If we have edge functions then the request will have already been rewritten
92+
// so this won't match. This is matched if edge is disabled or unavailable.
93+
netlifyConfig.redirects.push({
94+
from: `${imagePath}*`,
95+
query: { url: ':url', w: ':width', q: ':quality' },
96+
to: `${basePath}/${IMAGE_FUNCTION_NAME}/w_:width,q_:quality/:url`,
97+
status: 301,
98+
})
99+
100+
netlifyConfig.redirects.push({
101+
from: `${basePath}/${IMAGE_FUNCTION_NAME}/*`,
102+
to: `/.netlify/builders/${IMAGE_FUNCTION_NAME}`,
103+
status: 200,
104+
})
105+
}
101106

102107
if (basePath) {
103108
// next/image generates image static URLs that still point at the site root

packages/runtime/src/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
isNextAuthInstalled,
3131
getCustomImageResponseHeaders,
3232
getRemotePatterns,
33-
isEnvSet,
3433
} from './helpers/utils'
3534
import {
3635
verifyNetlifyBuildVersion,
@@ -170,16 +169,14 @@ const plugin: NetlifyPlugin = {
170169
nextConfig: { basePath, i18n },
171170
})
172171

173-
if (!isEnvSet('DISABLE_IPX')) {
174-
await setupImageFunction({
175-
constants,
176-
imageconfig: images,
177-
netlifyConfig,
178-
basePath,
179-
remotePatterns: getRemotePatterns(experimental, images),
180-
responseHeaders: getCustomImageResponseHeaders(netlifyConfig.headers),
181-
})
182-
}
172+
await setupImageFunction({
173+
constants,
174+
imageconfig: images,
175+
netlifyConfig,
176+
basePath,
177+
remotePatterns: getRemotePatterns(experimental, images),
178+
responseHeaders: getCustomImageResponseHeaders(netlifyConfig.headers),
179+
})
183180

184181
await generateRedirects({
185182
netlifyConfig,

0 commit comments

Comments
 (0)