Nextjs图像未显示在vercel中

oknwwptz  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(114)

我有一个无头的WordPress网站在Nextjs实现和Vercel托管,但当我打开网页的一些图像需要大约10秒加载,我没有线索为什么,你可以尝试在https://blog.ultatel.com/page/2检查

import { useInView } from "react-intersection-observer";
const PostCard = ({
  post,
  variant = "primary",
  className,
  showAuthor = false,
}: PostCardProps) => {
  const [loading, setLoading] = useState(true);
  const [ref, inView] = useInView({
    triggerOnce: true,
    fallbackInView: true,
  });
  const [ref2, inView2] = useInView({
    triggerOnce: true,
    fallbackInView: true,
    threshold: 0.15,
  });
  const rootClassName = cn(
    s.card,
    {
      [s.primary]: variant === "primary",
      [s.secondary]: variant === "secondary",
    },
    className
  );
  return (
    <div className={rootClassName} ref={ref}>
      <Link
        className={s.coverWrapper}
        href={`/${post?.slug}`}
        aria-label={post?.title}
      >
        {inView && (
          <>
            {loading && <div className={s.loading}></div>}
            <Image
              src={post?.featuredImage?.node?.sourceUrl ?? "/default.webp"}
              alt={post?.featuredImage?.node?.altText ?? ""}
              ref={ref2}
              quality={90}
              className={`${s.cover} ${inView2 && s.show}`}
              width={700}
              height={700}
              onLoadingComplete={() => setLoading(false)}
              priority
              
            />
          </>
        )}
      </Link>
      <div className={s.post__content}>
        <div className={s.categoryTime}>
          {post?.categories?.edges?.length >= 1 && (
            <div className="flex items-center gap-2 flex-wrap">
              <Tag
                category={post?.categories?.edges[0]?.node}
                variant={variant}
                className={s.tag}
              />
            </div>
          )}

          {post?.readingTime && (
            <div className={s.time}>{post?.readingTime} min read</div>
          )}
        </div>
        <h3 className={s.post__title}>
          <Link href={`/${post?.slug}`}>{post?.title}</Link>
        </h3>
        {showAuthor && (
          <div>
            <Link
              href={`/author/${post?.author?.node?.slug}`}
              className={s.author}
            >
              {post?.author?.node?.name}
            </Link>
          </div>
        )}
        <LineButton
          className={s.buttonLine}
          variant={variant}
          href={`/${post?.slug}`}
          aria={`Read ${post?.title}`}
        >
          Read post{" "}
          <svg
            className={s.icon}
            width="12"
            height="11"
            viewBox="0 0 12 11"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M7.20904 1.21197L10.711 5.64783L7.20904 10.0837"
              stroke={variant === "primary" ? "white" : "#004C66"}
              strokeWidth="2"
            />
            <line
              x1="0.205078"
              y1="5.38708"
              x2="10.7111"
              y2="5.38708"
              stroke={variant === "primary" ? "white" : "#004C66"}
              strokeWidth="2"
            />
          </svg>
        </LineButton>
      </div>
    </div>
  );
};
export default PostCard;

这是我当前的postCard组件,用于呈现图像。
我使用react-intersection-observer来渲染图像,只有当用户滚动到明信片时,我试着删除这个Inview,只延迟加载图像,我试着使用fill属性,size属性,但所有版本都不工作,有人能解释给我听吗,为什么渲染图像需要这么长时间?(看到问题后,我添加了一个'占位符'加载,给用户一些反馈)

{loading && <div className={s.loading}></div>}
gpnt7bae

gpnt7bae1#

你的代码看起来很好,但可能有几个原因导致你的Next.js网站中的图像加载时间很长。以下是一些潜在的问题和提高图像加载性能的相应建议:

  • 您的镜像大小可能很大,这会显著影响加载时间。考虑使用Photoshop、TinyPNG、Cloudinary或ImageOptim压缩和调整图像大小。
  • 缓慢的网络延迟也可能导致图像加载缓慢。您可以通过优化图像托管环境或使用内容交付网络(CDN)来提供图像来改善这一点。您的托管服务提供商Vercel已经使用全球CDN,但如果您愿意,您也可以使用其他在线平台,如Cloudinary for global CDN,以确保快速可靠地交付您的图像。这样,您的图像将被缓存在分布在全球的CDN服务器上,从而减少延迟并缩短加载时间。
  • 另一个原因可能是服务器响应时间。检查获取图像时WordPress服务器的响应时间是否很慢。这可能会延迟图像加载过程。因此,请尝试缓存映像或优化服务器环境以获得更好的性能。您也可以尝试延迟加载以减少初始加载时间。Cloudinary支持延迟加载技术,只有当图像进入设备视口时才加载图像。可以使用Cloudinary的JS SDK或Next.js插件来实现延迟加载。
  • 最后,尝试在Next.js中使用图像优化。考虑使用next/image中的组件,根据设备和视口自动优化图片,这样可以缩短加载时间。

通过调查这些因素,您应该能够确定图像加载缓慢的根本原因,并采取适当的措施来提高Next.js网站的性能。希望这能帮上忙。

相关问题