简体   繁体   English

如何在 Nextjs 上导出 static 图片?

[英]How to export static images on Nextjs?

when I want to export my nextjs app, it says that I cannot export my images on static websites.当我想导出我的 nextjs 应用程序时,它说我无法导出我在 static 网站上的图像。

Error: Image Optimization using Next.js' default loader is not compatible with next export .错误:使用 Next.js 的默认加载器的图像优化与next export不兼容。 Possible solutions: - Use next start to run a server, which includes the Image Optimization API. - Use any provider which supports Image Optimization (like Vercel).可能的解决方案: - 使用next start运行服务器,其中包括图像优化 API。 - 使用任何支持图像优化的提供程序(如 Vercel)。 - Configure a third-party loader in next.config.js . - 在next.config.js中配置第三方加载器。 - Use the loader prop for next/image . - 为next/image使用loader属性。

How can I make it so that it does?我怎样才能做到这一点?

Is there a way for me to simply tell it to render images statically?有没有办法让我简单地告诉它静态渲染图像? I dont want to go throught other onlines images loaders..我不想通过其他在线图像加载器 go..

I created a npm module so that we can use the Next.js <Image/> component with optimized images while using the static export functionality.我创建了一个 npm 模块,这样我们就可以在使用 static 导出功能的同时使用 Next.js <Image/> 组件和优化图像。

https://www.npmjs.com/package/next-image-export-optimizer https://www.npmjs.com/package/next-image-export-optimizer

The library wraps the <Image /> component of Next.js and automatically creates optimized images using sharp.js at export time.该库包装了 Next.js 的 <Image /> 组件,并在导出时使用 sharp.js 自动创建优化图像。

It uses a custom loader to create a srcset for the <img /> that the <Image /> component of Next.js creates.它使用自定义加载器为 Next.js 的 <Image /> 组件创建的 <img /> 创建 srcset。 Then at build/export time, the images inside the public/images folder (as an example) are optimized with sharp.js and copied into the build folder.然后在构建/导出时,public/images 文件夹中的图像(作为示例)使用 sharp.js 进行优化并复制到构建文件夹中。

I'm going to add onto skara9's answer because it didn't quite work for me.我要添加到 skara9 的答案中,因为它对我来说不太管用。 I found a thread on github discussing it and the answer there worked for me.我在 github 上找到了一个讨论它的线程,那里的答案对我有用。 It just wraps around the NextJS image component and works pretty flawlessly for me.它只是包裹了 NextJS 图像组件,对我来说非常完美。

// components/Image.js
import NextImage from "next/image";

// opt-out of image optimization, no-op
const customLoader = ({ src }) => {
  return src
}

export default function Image(props) {
  return (
    <NextImage
      {...props}
      loader={customLoader}
    />
  );
}

Make sure you change your imports and update your next.config.js确保更改导入并更新next.config.js

import Image from '../components/Image.js'

You need to set up a custom image loader in Next.js您需要在 Next.js 中设置自定义图像加载器

In your next.config.js file, add this property to the export:next.config.js文件中,将此属性添加到导出:

images: {
  loader: "custom"
}

And make a script called loader.js that exports this:并制作一个名为loader.js的脚本来导出:

function imageLoader({ src }) {
  return `/images/${src}`; // REPLACE WITH YOUR IMAGE DIRECTORY
}

For each Image component, set the loader prop manually:对于每个Image组件,手动设置loader属性:

<Image loader={imageLoader} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM