-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
images.loaderFile
config (#41585)
This PR adds a new configure property, `images.loaderFile` that allow you to define a path to a file with an exported image loader function. This is useful when migrating from `next/legacy/image` to `next/image` because it lets you configure the loader for every instance of `next/image` once, similar to the legacy "built-in loaders".
- Loading branch information
Showing
20 changed files
with
549 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// TODO: change "any" to actual type | ||
function defaultLoader({ config, src, width, quality }: any): string { | ||
if (process.env.NODE_ENV !== 'production') { | ||
const missingValues = [] | ||
|
||
// these should always be provided but make sure they are | ||
if (!src) missingValues.push('src') | ||
if (!width) missingValues.push('width') | ||
|
||
if (missingValues.length > 0) { | ||
throw new Error( | ||
`Next Image Optimization requires ${missingValues.join( | ||
', ' | ||
)} to be provided. Make sure you pass them as props to the \`next/image\` component. Received: ${JSON.stringify( | ||
{ src, width, quality } | ||
)}` | ||
) | ||
} | ||
|
||
if (src.startsWith('//')) { | ||
throw new Error( | ||
`Failed to parse src "${src}" on \`next/image\`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)` | ||
) | ||
} | ||
|
||
if (!src.startsWith('/') && (config.domains || config.remotePatterns)) { | ||
let parsedSrc: URL | ||
try { | ||
parsedSrc = new URL(src) | ||
} catch (err) { | ||
console.error(err) | ||
throw new Error( | ||
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)` | ||
) | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
// We use dynamic require because this should only error in development | ||
const { hasMatch } = require('./match-remote-pattern') | ||
if (!hasMatch(config.domains, config.remotePatterns, parsedSrc)) { | ||
throw new Error( | ||
`Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` + | ||
`See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host` | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (src.endsWith('.svg') && !config.dangerouslyAllowSVG) { | ||
// Special case to make svg serve as-is to avoid proxying | ||
// through the built-in Image Optimization API. | ||
return src | ||
} | ||
|
||
return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${ | ||
quality || 75 | ||
}` | ||
} | ||
|
||
// We use this to determine if the import is the default loader | ||
// or a custom loader defined by the user in next.config.js | ||
defaultLoader.__next_img_default = true | ||
|
||
export default defaultLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// prettier-ignore | ||
module.exports = { /* replaceme */ } |
Oops, something went wrong.