Skip to content

Commit f062b53

Browse files
committed
refactor: serve 404 when ipx is disabled and no custom loader is specified
1 parent 5ebd2bd commit f062b53

File tree

5 files changed

+21
-71
lines changed

5 files changed

+21
-71
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ by targeting the `/_next/image/*` route:
4444

4545
## Disabling `ipx`
4646

47-
If you wish to disable the use of the `ipx` package, set the `DISABLE_IPX` environment variable to `true`.
47+
If you wish to disable the use of the `ipx` package, set the `DISABLE_IPX` environment variable to `true`. Please note that some requests to `/_next/image/*` may fail unless an image loader, such as Cloudinary or Imgix, is specified as a replacement for `ipx`.
48+
49+
See the [Next.js documentation](https://nextjs.org/docs/api-reference/next/image#built-in-loaders) for options.
4850

4951
## Next.js Middleware on Netlify
5052

demos/default/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"@reach/visually-hidden": "^0.16.0",
2323
"next": "^12.3.0",
2424
"react": "^18.0.0",
25-
"react-dom": "^18.0.0",
26-
"sharp": "^0.31.1"
25+
"react-dom": "^18.0.0"
2726
},
2827
"devDependencies": {
2928
"@netlify/plugin-nextjs": "*",

package-lock.json

Lines changed: 1 addition & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ const resolveModuleRoot = (moduleName) => {
7878
}
7979
}
8080

81-
const configureDefaultExcludedModules = () => (isEnvSet('DISABLE_IPX') ? ['electron'] : ['sharp', 'electron'])
82-
83-
const DEFAULT_EXCLUDED_MODULES = configureDefaultExcludedModules()
81+
const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron']
8482

8583
export const configureHandlerFunctions = async ({ netlifyConfig, publish, ignore = [] }) => {
8684
const config = await getRequiredServerFiles(publish)

packages/runtime/src/helpers/functions.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ export const setupImageFunction = async ({
7171
remotePatterns: RemotePattern[]
7272
responseHeaders?: Record<string, string>
7373
}): Promise<void> => {
74-
if (!isEnvSet('DISABLE_IPX')) {
74+
const imagePath = imageconfig.path || '/_next/image'
75+
76+
if (isEnvSet('DISABLE_IPX')) {
77+
// If no image loader is specified, need to redirect to a 404 page since there's no
78+
// backing loader to serve local site images
79+
if (!imageconfig.loader) {
80+
netlifyConfig.redirects.push({
81+
from: `${imagePath}*`,
82+
query: { url: ':url', w: ':width', q: ':quality' },
83+
to: '/404.html',
84+
status: 404,
85+
force: true
86+
})
87+
}
88+
} else {
7589
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
7690
const functionName = `${IMAGE_FUNCTION_NAME}.js`
7791
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME)
@@ -86,8 +100,6 @@ export const setupImageFunction = async ({
86100

87101
await copyFile(join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), join(functionDirectory, functionName))
88102

89-
const imagePath = imageconfig.path || '/_next/image'
90-
91103
// If we have edge functions then the request will have already been rewritten
92104
// so this won't match. This is matched if edge is disabled or unavailable.
93105
netlifyConfig.redirects.push({

0 commit comments

Comments
 (0)