question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cant use external next/image with custom server in production

See original GitHub issue

Bug report

Describe the bug

Cant use external next/image with custom server, request:

http://localhost:3000/_next/image?url=https%3A%2F%2Fpicsum.photos%2Fseed%2F9e56cc3f-be43-4d02-a580-9fe6438640e9%2F208%2F124&w=256&q=75

return 400 and message "url" parameter is not allowed

domain is added to next.config.js and it works in development

To Reproduce

custom server:

const { createServer } = require("http");
const next = require("next");
const { parse } = require("url");

const dev = process.env.NODE_ENV !== "production";
const port = process.env.PORT || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true);

    handle(req, res, parsedUrl);
  }).listen(port, (err) => {
    if (err) throw err;
    console.log("> Ready on http://localhost:3000");
  });
});

Image component

<Image
          src="https://picsum.photos/seed/208/124"
          height={124}
          width={208}
        />

Expected behavior

Image should load

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: Windows and linux
  • Version of Next.js: 10.0.3
  • Version of Node.js: 12 and 14
  • Deployment: local server and azure

Additional context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
korbinianfritschcommented, Dec 4, 2020

Thanks for opening this issue, @gust42.

I can confirm this behavior within a similar setup:

2reactions
gust42commented, Sep 15, 2021

You can add a next.config.js to your server folder with your image setup, solved it for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Custom Server - Next.js
A custom Next.js server allows you to start a server 100% programmatically in order to use custom server patterns. Most of the time,...
Read more >
next/image does not load images from external URL after ...
I use the Next.js Image component for image optimization. It works great on dev but it doesn't load images from external URLs in...
Read more >
Next.js automatic image optimization with next/image
Learn about automatic image optimization and how it can benefit developers with the native, game-changing, and powerful next/image API.
Read more >
Best practices to increase the speed for Next.js apps
Using server -side rendering will help your app reduce the time ... Next.js provides an inbuilt next/image component that we can use in...
Read more >
Nextjs image optimization with examples - Refine Dev
Built-in image optimization using next image component. ... This is a custom function that resolves external image URLs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found