Next.js图片宽度适配Bug:文章关联图片无法缩至300px
Next.js 图片宽度缩放问题修复方案
问题现象
加载默认图片时可正常调整至300px宽度,但文章关联的特色图片始终保持900px,无法按预期缩放,图片通过pages/blog/index.js中的<FeaturedImage>组件展示。
核心原因
components/FeaturedImage.js组件内的判断条件错误:使用了大写开头的post.FeaturedImage进行判断,但实际API返回的字段是小写开头的post.featuredImage,导致判断逻辑始终不成立,一直使用默认图片的尺寸配置;而打印post.featuredImage时能正常获取到数据,说明字段大小写不匹配是问题根源。
修复后的FeaturedImage.js代码
import Image from "next/image"; import Link from "next/link"; export default function FeaturedImage({ post }) { let img = ""; const defaultFeaturedImage = "http://nextjstest1.local/wp-content/uploads/2022/12/travel_icy-polar_022K.jpg"; const defaultWidth = "300"; const defaultHeight = "200"; // 修正字段大小写,使用post.featuredImage判断 if (post.featuredImage) { let size = post.featuredImage.node.mediaDetails.sizes[0]; img = { src: size.sourceUrl, width: size.width, height: size.height, }; } else { img = { src: defaultFeaturedImage, width: defaultWidth, height: defaultHeight, }; } return ( <Link href={`/blog/${post.slug}`}> <Image src={img.src} width={img.width} height={img.height} alt={post.title} /> </Link> ); }
相关参考代码
pages/blog/index.js
import FeaturedImage from "@/components/FeaturedImage"; import Head from "next/head"; import Image from "next/image"; import Link from "next/link"; import SiteHeader from "@/components/SiteHeader"; import getAllPosts from "@/lib/posts"; export async function getStaticProps() { const allPosts = await getAllPosts(); return { props: { allPosts: allPosts, }, }; } export default function BlogHome({ allPosts }) { return ( <> <Head> <title>Blog</title> </Head> <div className="h-[50vh] min-h-[20rem] bg-[url('/home.jpg')] relative"> <div className="absolute bg-slate-900 inset-0 z-0 opacity-40"></div> <SiteHeader className="header-blog-home z-10 relative" /> <h1 className="text-6xl text-center text-slate-100 relative z-10 py-8"> BLOG </h1> <p className="relative z-10 text-center text-slate-200 text-2xl"> Read our latest articles </p> </div> <main> <section className="post-list mt-4"> <ul> {allPosts.nodes.map((post) => ( <li key={post.slug} className="grid grid-col-5 gap-4 mb-4"> <div className="col-span-2"> <FeaturedImage post={post} /> </div> <div className="col-span-3"> <h2 className="py-4"> <Link href={`/blog/${post.slug}`} className="text-blue-400 text-2xl" > {post.title} </Link> </h2> <div className="text-lg" dangerouslySetInnerHTML={{ __html: post.excerpt }} ></div> <div> Posted under {post.categories.nodes.map((category) => ( <Link href={`/category/${category.slug}`} key={category.slug} className="text-blue-400 hover:text-blue-500" > {category.name} </Link> ))} </div> </div> </li> ))} </ul> </section> </main> </> ); }
lib/graphqlRequest.js
export default async function graphqlRequest(query) { const url = "http://nextjstest1.local/graphql"; const headers = { "Content-Type": "application/json" }; const res = await fetch(url, { headers, method: "POST", body: JSON.stringify(query), }); const resJson = await res.json(); return resJson; }
next.config.js
/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, images: { remotePatterns: [ { protocol: "http", hostname: "nextjstest1.local", port: "", pathname: "/**", }, ], }, }; module.exports = nextConfig;
内容的提问来源于stack exchange,提问作者WilOnWeb
相关产品推荐
相关产品推荐

