From 79015bf8d9b60f2c39895b0190d78c050a996f1b Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 14 May 2025 18:41:03 +0800 Subject: [PATCH] fix: use different local may not load image (#19693) --- web/next.config.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/web/next.config.js b/web/next.config.js index 2d18dddaf0..9ce1b35644 100644 --- a/web/next.config.js +++ b/web/next.config.js @@ -14,7 +14,10 @@ const withMDX = require('@next/mdx')({ }) // the default url to prevent parse url error when running jest -const remoteImageURL = new URL(`${process.env.NEXT_PUBLIC_WEB_PREFIX || 'http://localhost:3000'}/**`) +const hasSetWebPrefix = process.env.NEXT_PUBLIC_WEB_PREFIX +const port = process.env.PORT || 3000 +const locImageURLs = !hasSetWebPrefix ? [new URL(`http://localhost:${port}/**`), new URL(`http://127.0.0.1:${port}/**`)] : [] +const remoteImageURLs = [hasSetWebPrefix ? new URL(`${process.env.NEXT_PUBLIC_WEB_PREFIX}/**`) : '', ...locImageURLs].filter(item => !!item) /** @type {import('next').NextConfig} */ const nextConfig = { @@ -29,13 +32,13 @@ const nextConfig = { pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'], // https://nextjs.org/docs/messages/next-image-unconfigured-host images: { - remotePatterns: [{ + remotePatterns: remoteImageURLs.map(remoteImageURL => ({ protocol: remoteImageURL.protocol.replace(':', ''), hostname: remoteImageURL.hostname, port: remoteImageURL.port, pathname: remoteImageURL.pathname, search: '', - }], + })), }, experimental: { },