fix: use different local may not load image (#19693)

This commit is contained in:
Joel 2025-05-14 18:41:03 +08:00 committed by GitHub
parent 1c91736a6d
commit 79015bf8d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,7 +14,10 @@ const withMDX = require('@next/mdx')({
}) })
// the default url to prevent parse url error when running jest // 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} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
@ -29,13 +32,13 @@ const nextConfig = {
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'], pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
// https://nextjs.org/docs/messages/next-image-unconfigured-host // https://nextjs.org/docs/messages/next-image-unconfigured-host
images: { images: {
remotePatterns: [{ remotePatterns: remoteImageURLs.map(remoteImageURL => ({
protocol: remoteImageURL.protocol.replace(':', ''), protocol: remoteImageURL.protocol.replace(':', ''),
hostname: remoteImageURL.hostname, hostname: remoteImageURL.hostname,
port: remoteImageURL.port, port: remoteImageURL.port,
pathname: remoteImageURL.pathname, pathname: remoteImageURL.pathname,
search: '', search: '',
}], })),
}, },
experimental: { experimental: {
}, },