【发布时间】:2022-01-02 16:38:07
【问题描述】:
只是对任何有助于减轻 Gatsby 网站内图像查询压力的解决方案感到好奇。 Gatsby 已经给出了图片查询时间过长的警告(我们网站内有大约 600 张图片,而且增长速度非常快)。
理想情况下,我希望每次只获取一张特定的图片,但不想为每个图片创建静态查询。
到目前为止,我在Image 组件中的查询是这样的:
query={graphql`
{
images: allFile(filter: { sourceInstanceName: { eq: "images" } }) {
edges {
node {
relativePath
name
childImageSharp {
gatsbyImageData(
quality: 100
layout: CONSTRAINED
placeholder: TRACED_SVG
)
}
}
}
}
}
`}
render={data => {
const image = data.images.edges.find(n => {
return n.node.relativePath.includes(filename);
});
if (!image) {
return null;
}
const imageAlt = filename.split('.')[0];
return (
<GatsbyImage
image={image.node.childImageSharp.gatsbyImageData}
className={className}
style={styles}
alt={alt ? alt : imageAlt}
fadeIn={false}
/>
);
}}
/>
我只是想听听有人为 Gatsby 上更大的图像数据库提出的任何解决方案 :)
【问题讨论】:
-
简单地使用像 Sanity 这样的 CMS 更具可扩展性,它具有良好的图像托管和动态转换 API。然后,您可以在 GraphQL 中获取图像 ID 并将它们传递给 gatsby-plugin-sanity-image,并动态生成完整的 srcSet,每个 URL 都指向一个不会影响构建时间的服务器渲染图像。
标签: reactjs graphql gatsby gatsby-image