为外部域的图像配置下一个JS

发布于 2025-01-22 01:15:27 字数 1387 浏览 0 评论 0原文

我对next.config.js有问题。我目前正在运行一个带有打字稿的Next JS的项目。在这个项目中,我正在与TrixJS合作, @react-three/fiber& @react-three/drei。但是我也想包括一些特定公共YouTube URL的图像。 在较旧的项目中,我已经实施了这样的内部三个J的实现:

module.exports = {
reactStrictMode: true,
images: {
    domains: ['i3.ytimg.com', 'img.youtube.com'],
    formats: ['image/webp'],
},

}

在我的next.config.js上,它仍然像魅力一样起作用。但是,当我将其放入当前项目并尝试加载图像时,我会发现错误说:

错误:next/image上的无效src prop([url]),hostName“ img.youtube.com”未在next.config.js

当前的 图像中配置Next.config.js文件

module.exports = {
reactStrictMode: true,
images: {
    domains: ['i3.ytimg.com', 'img.youtube.com'],
    formats: ['image/webp'],
},
}


const withTM = require('next-transpile-modules')(['three', '@react-three/fiber', '@react-three/drei']);

module.exports = withTM();

现在在我的组件上:

export default function ProjectCard({ song }: { song: Lyrics }) {
const img = hqDefault(song.thumbnailURL);

return (
    <div>
        {song.singer}
        <Image src={img} alt={`${song.singer} ${song.title}`} layout="fill" />
    </div>
)
}

HQDEFAULT函数:

export const hqDefault = (url: string): string => {
    return `https://img.youtube.com/vi/${url}/hqdefault.jpg`;
}

任何帮助都将被应用!

I'm having an issue with next.config.js. I'm currently running a project with next js on typescript. In this project i am working with ThreeJs, @react-three/fiber & @react-three/drei. But I also want to include some images from a specific public YouTube url.
In older projects i have implemented this withought having ThreeJs inside like this:

module.exports = {
reactStrictMode: true,
images: {
    domains: ['i3.ytimg.com', 'img.youtube.com'],
    formats: ['image/webp'],
},

}

That being on my next.config.js and it still works like a charm. But when I put it in my current project and try to load an image I get error saying:

Error: Invalid src prop ([url]) on next/image, hostname "img.youtube.com" is not configured under images in your next.config.js

Current next.config.js file

module.exports = {
reactStrictMode: true,
images: {
    domains: ['i3.ytimg.com', 'img.youtube.com'],
    formats: ['image/webp'],
},
}


const withTM = require('next-transpile-modules')(['three', '@react-three/fiber', '@react-three/drei']);

module.exports = withTM();

On my component now:

export default function ProjectCard({ song }: { song: Lyrics }) {
const img = hqDefault(song.thumbnailURL);

return (
    <div>
        {song.singer}
        <Image src={img} alt={`${song.singer} ${song.title}`} layout="fill" />
    </div>
)
}

hqDefault Function:

export const hqDefault = (url: string): string => {
    return `https://img.youtube.com/vi/${url}/hqdefault.jpg`;
}

Any help will be appriciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2025-01-29 01:15:27

您应该导出单个配置对象。

首先,安装“下一代组合 - plugins”:

npm install --save next-compose-plugins

导入它:

const withPlugins = require('next-compose-plugins');

然后像这样导出配置:

module.exports = withPlugins([
    [withTM]
], nextConfig);

You are supposed to export a single configuration object.

First, Install 'next-compose-plugins':

npm install --save next-compose-plugins

Import it:

const withPlugins = require('next-compose-plugins');

And then export your config like this:

module.exports = withPlugins([
    [withTM]
], nextConfig);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文