0

我在我的 Next JS 应用程序中使用默认组件来优化图像,但发现如果使用默认图像优化组件,除了 Vercel 之外,该站点无法托管在其他任何地方。除此之外,该网站也不能导出以托管在其他网络托管平台上。

内置图像组件的缺点使我想到了next-optimized-images包含图像优化和其他好处的包作为内置的 NextImage组件。

它需要我创建一个babel.rc具有以下配置的:

{
  "presets": ["next/babel"],
  "plugins": ["react-optimized-image/plugin"]
}

我还将我next.config.js的修改为以下内容:

const withOptimizedImages = require('next-optimized-images');

module.exports = withOptimizedImages({
  /* config for next-optimized-images */

  // your config for other plugins or the general next.js here...
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ["@svgr/webpack"]
    });

    return config;
  },
  reactStrictMode: true,
});

next-optimized-image内置Img组件可以这样使用:

import Img from 'react-optimized-image';
// react-optimized-image is specified in the docs explicitly
import HeroImage from '../../../public/images/dashboard-on-mobile-and-desktop-screen.png'

export default function Hero(){
  return (
    <>
      <Img  src={HeroImage} sizes={[614]}/>
    </>
  )
}

完成所有这些并运行后npm run dev,我的控制台中出现以下错误:

(node:22516) UnhandledPromiseRejectionWarning: Error: Input buffer contains unsupported image format
(Use `node --trace-warnings ...` to show where the warning was created)
(node:22516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by 
rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:22516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我该如何解决这个问题?提前致谢

4

0 回答 0