当我使用plaiceholder插件在NextJ中模糊图像时,为什么有更多图像时会崩溃?

发布于 2025-02-08 07:46:42 字数 974 浏览 2 评论 0原文

我使用以下代码模糊远程图像,当图像数量很小时,它可以执行,但是现在我有50张图像,并且在项目运行后浏览器被卡住了。

优化它的最佳方法是什么?是由Plaiceholder插件还是由NextJS本身引起的?

import fs from 'fs';
import path from 'path';
import { getPlaiceholder } from 'plaiceholder';
import { PhotoData } from '../types';

const directory = path.join(process.cwd(), 'data');

export async function getPhotos() {
  const filePath = path.join(directory, 'photos.json');
  const jsonData = fs.readFileSync(filePath, 'utf8');
  const photosData = JSON.parse(jsonData).sort((a: PhotoData, b: PhotoData) =>
    new Date(b.date) > new Date(a.date) ? 1 : -1
  );

  const photosProps = await Promise.all(
    photosData.map(async (photoData: PhotoData) => {
      const { thumbnail } = photoData;
      const { base64, img } = await getPlaiceholder(thumbnail);

      return {
        ...img,
        blurDataURL: base64,
      };
    })
  ).then((values) => values);

  return {
    photosProps,
    photosData,
  };
}

I use the following code to blur the remote images, when the number of images is small it can be executed, but now I have 50 images and the browser gets stuck after the project runs.

what is the best way to optimize it? Is it caused by the plaiceholder plugin or by nextjs itself?

import fs from 'fs';
import path from 'path';
import { getPlaiceholder } from 'plaiceholder';
import { PhotoData } from '../types';

const directory = path.join(process.cwd(), 'data');

export async function getPhotos() {
  const filePath = path.join(directory, 'photos.json');
  const jsonData = fs.readFileSync(filePath, 'utf8');
  const photosData = JSON.parse(jsonData).sort((a: PhotoData, b: PhotoData) =>
    new Date(b.date) > new Date(a.date) ? 1 : -1
  );

  const photosProps = await Promise.all(
    photosData.map(async (photoData: PhotoData) => {
      const { thumbnail } = photoData;
      const { base64, img } = await getPlaiceholder(thumbnail);

      return {
        ...img,
        blurDataURL: base64,
      };
    })
  ).then((values) => values);

  return {
    photosProps,
    photosData,
  };
}

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

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

发布评论

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

评论(1

缺⑴份安定 2025-02-15 07:46:42

我正在尝试实施类似的事情,但预测一个类似的问题,前端会变得凌乱。因此,在我的情况下,我将尝试在后端存储哈希图像。

如果您仅使用NextJ,则可以尝试在NextJS Backend中执行此操作。
我认为您可以从杰克·赫林顿(Jack Herrington) https://www.youtube.com/watch?v=ffhsiio4acu& t = 803S

I am trying to implement a similar thing, but predict a similar issue and frontend will get messy. So, I will try to store the hash images in the backend, in my case, using my cms.

If you are using only nextjs, you can try to do that in nextjs backend.
I think you can get the inspiration on how to do that from the below video by Jack Herrington https://www.youtube.com/watch?v=FfHsIio4aCU&t=803s

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