~/javascript/nextjs-fixing-load-external-images-url-parameter-not-allowed.md

Nextjs - Fixing Loading External Images and Issue of Url Paramater Not Allowed

2 min readJanuary 30, 2022by Gorav Singal#javascript#nextjs
// TL;DR

Configure the images.domains array in next.config.js to whitelist external hostnames and resolve the 'URL parameter not allowed' error when using the Next.js Image component.

Nextjs - Fixing Loading External Images and Issue of Url Paramater Not Allowed

Introduction

In this post, we will see

  • How to load external images to your next.js website
  • Fixing issue of Url Parameter not allowed

Using Image component in Next.js

A general code to load image in next.js is

<Image
    src={this.props.article.image.url}
    height={400}
    width={700}
    alt={this.props.article.title}
    />

If this image url is same as your domain name, then its fine and you do not need to do anything else. But, if you are loading images from any other url like unsplash or s3.amazonaws.com, then it will not load it.

By default, Next.js does not allow you to load images from any other url. This is due to security implications. You should know from where you are loading images.

How to Load External Images

You need to configure this in next.config.js configuration file, often placed at the root folder of your project.

A sample file:

module.exports = {
  env: {
    NEXT_PUBLIC_API_URL:
      process.env.NEXT_PUBLIC_API_URL,
    ...some other properties
  },
  images: {
    domains: [
      'images.unsplash.com',
      's3.amazonaws.com'
    ],
  }
};

In this way, you are telling Next.js to load images from above mentioned two domains.

Your image source will become something like below:

/_next/image?url=https%3A%2F%2Fs3.amazonaws.com%2Ffiles.<my_bucket>.com%2Ffiles%2Farticles%2Fasian_garden_1a6667449c.jpg&w=1920&q=75

There is a catch here too. read next line.

Issue: Url Paramater Not Allowed

If you are deploying your application using Docker containers, then you will most probably face this issue again, even if you have used next.config.js.

When you load, you will see this error:

"url" parameter is not allowed

We generally take Dockerfile from the official sources, and there is this line commented:

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./

Just uncomment above COPY step, and re-build your container image. This should work.

Hope this helps.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Authenticating Strapi backend with Next.js and next-auth using credentials and jwt

Authenticating Strapi backend with Next.js and next-auth using credentials and jwt

Introduction Strapi is a backend system provides basic crud operations with…

Request Entity Too Large(413) - Uploading File with Formdata with Axios and Custom header

Request Entity Too Large(413) - Uploading File with Formdata with Axios and Custom header

Introduction I was trying to upload images to my backend using rest APIs. I was…

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Introduction In this post we will see: How to prepare a docker image for your…

Nextjs - How to Redirect to Another Page and Pass State

Nextjs - How to Redirect to Another Page and Pass State

Introduction We have a page, for example a . And, upon submission we would want…

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

Introduction This post is in contuation of our previous post: How to use Draft…

Create Articles with Image Upload Using Draft.js, Next.js, and Strapi

Create Articles with Image Upload Using Draft.js, Next.js, and Strapi

Introduction In this post, we will use in Next.js with strapi. And, we will…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…