Shivan M.
—In Next.js 13, you might need to render multiple images that are located at different URLs.
If your images are not loading when using external URLs, it’s because Next.js requires configuration to be set. By explicitly stating from which URLs the images can be loaded, you can protect your application from malicious third parties.
There are two approaches to setting the configuration to allow external images.
In your next.config.js
file, you can set the remotePatterns
prop to specify from where external images can be sourced.
In the following example, images from any URL starting with https://example.com/account123/
are allowed and any other images will result in a 400 Bad Request
:
module.exports = { images: { remotePatterns: [ { protocol: 'https', hostname: 'example.com', port: '', pathname: '/account123/**', }, ], }, }
You can use wildcard patterns for both pathname
and hostname
as follows:
*
to match a single path segment or subdomain.*
to match any number of path segments at the end, or subdomains at the beginning.You can also use the domains
prop to provide a list of hostnames that are allowed for external images. However, the domains
prop does not support wildcard pattern matching, nor can it restrict protocol, port, and pathname. Thus, any route of a provided hostname can be used for external images.
Below is an example of the domains
prop:
module.exports = { images: { domains: ['example.com'], }, }
In this case, both https://example.com/account123
and http://example.com/account113
are valid URLs for external images.
You can find additional information about the remotePatterns
and domains
configuration in the Next.js documentation.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.