我有一个使用 Gatsby (带有gatsby-plugin-image)的网站,我正在尝试重构为 Typescript。这是我第一次接触 ts
我有一个布局页面,它使用页面查询来提取 Mdx 数据,但是我缺少一些类型,并且想知道是否有人可以为我指出正确的方向。
我正在努力寻找应该导入和使用的类型:
childImageSharp: {fixed:???}
MdxQuery = {body: ???}`
Run Code Online (Sandbox Code Playgroud)
在处理图像的旧gatsby-image方法中,它使用 的类型,FixedObject但这不再有效。
对于这些缺失的类型,有人能为我指出正确的方向吗?
type Frontmatter = {
title: string;
titlestrap: string | null;
category: string;
slug: string;
metaDescription: string | null;
author?: string | null;
featuredImage: {
childImageSharp: {
fixed: any; // what here?
gatsbyImageData: IGatsbyImageData;
};
};
};
type MdxQuery = {
id: string;
body: any; // what here?
frontmatter: Frontmatter;
excerpt: string;
};
Run Code Online (Sandbox Code Playgroud) 我正在将 Strapi 与 Gatsby 一起使用,并且在动态区域内渲染图像有困难。我在用:
直接在集合类型中找到的顶级图像字段可以使用 GraphQL 轻松查询以返回gatsbyImageData,以缩略图字段为例:
query MyQuery {
allStrapiPage {
nodes {
Title
Body
thumbnail {
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在上面的查询中Body是一个动态区域字段,其中包含数十个可选组件,其中许多包含图像字段。为 Body 返回的数据是标准 JSON 数据,这意味着图像字段没有 required gatsbyImageData,请参阅示例响应:
"Body": [
{
"PrimaryImage": {
"id": 12,
"name": "Test Image",
"alternativeText": "",
"caption": "",
"width": 338,
"height": 260,
"formats": {
"thumbnail": {
"name": "thumbnail_Test Image",
"hash": …Run Code Online (Sandbox Code Playgroud) 我正在使用 Netlify CMS(并托管在 Netlify 上)开发 Gatsby 应用程序。尝试使元数据正常工作,以便 Twitter 卡片与图像一起正确显示。
元数据通常没问题,但图像没有显示在 Twitter 验证器上,或者如果我尝试发布到 Twitter。问题显然是图像本身,这些图像托管在网站上,使用 Gatsby 和 Gatsby Image Sharp 进行渲染。
事实上,验证器似乎没有显示出根本问题。简单地说,图像不显示:
相关元数据示例:
<meta name="twitter:url" content="https://example.com/" data-react-helmet="true">
<meta name="twitter:image" content="https://example.com/static/12345/c5b20/blah.jpg" data-react-helmet="true">
<meta data-react-helmet="true" name="twitter:title" content="Site title">
<meta data-react-helmet="true" name="twitter:card" content="summary_large_image">
Run Code Online (Sandbox Code Playgroud)
我知道图像的问题,因为如果我用外部 URL 替换我的图像 URL(这是完整的图像 URL),它工作正常,显示带有图像的完整卡片。
知道是什么原因造成的吗?我正在缩小图像的大小,以便它可以快速加载,而且它似乎可以直接加载(例如)。(我的意思是,那张图片有什么奇怪的地方吗?)
注意:在此问题的先前版本中,我引用了 Cloudinary 和 Uploadcare,但此后在一个分支中删除了这两个以简化问题。(他们似乎已经从我使用的起始应用不必要的遗物。)现在,您可以看到一个示例页面该分支在这里和相关图像中的twitter:image标签在这里。我使用 React Helmet(和 Gatsby React Helmet)将此预处理/缩小的图像提供到标题中,并在我的 GraphQL 调用中使用以下代码以该特定的较小格式获取与博客文章关联的图像:
featuredimage {
childImageSharp {
fixed(width: 480, quality: 75) {
src
}
}
Run Code Online (Sandbox Code Playgroud)
第二个注意事项/想法:我是否应该担心生产中的页面似乎在每次重新加载时都重新渲染?SSR …
我想优化我的 Gatsby 网站上的所有图像,并实现我已经安装了gatsby-image-plugin.
对于动态图像,我使用 GatsbyImage 组件,一切正常,没有任何问题。
问题是当我想使用StaticImage Component渲染静态图像时。
这是一个例子:
import laptop from '@images/laptop.png';
Run Code Online (Sandbox Code Playgroud)
如果我导入图像并以这种方式使用它:
<img src={laptop} alt='laptop' />
Run Code Online (Sandbox Code Playgroud)
图像在浏览器上正确显示,但如果我尝试这样做:
import { StaticImage } from 'gatsby-plugin-image';
<StaticImage src={laptop} alt='laptop' />;
Run Code Online (Sandbox Code Playgroud)
图像未显示在浏览器上,我在控制台中收到以下消息:
图像未加载 /static/laptop-5f8db7cd28b221fc1a42d3ecd6baa636.png
终端中出现此错误:
在构建时找不到以下属性的值: src 图像未加载 /static/laptop-5f8db7cd28b221fc1a42d3ecd6baa636.png
我尝试作为src来自互联网的随机图像的链接传递,并且该图像显示在浏览器上!为什么当我使用资源文件夹中的图像时它不起作用?
附注;阅读插件文档我发现有一些限制,例如您不能传递来自 props 的图像,但事实并非如此!我直接从资产文件夹导入图像。
请问有什么线索吗?先感谢您。
我gatsby build在Gatsby 站点上运行时遇到问题,该问题仅在 Github Actions 容器中运行时发生。它也不总是发生。作为一个很好的例子,今天 Dependabot 启动了 12 个 PR,其中两个因此错误而失败。错误显示如下:
$ gatsby build
??????????????????????????????????????????????????????????????????????????
? ?
? Gatsby collects anonymous usage analytics ?
? to help improve Gatsby for all users. ?
? ?
? If you'd like to opt-out, you can use `gatsby telemetry --disable` ?
? To learn more, checkout https://gatsby.dev/telemetry ?
? ?
??????????????????????????????????????????????????????????????????????????
success open and validate gatsby-configs - 0.035s
success load plugins - 0.892s
success onPreInit - 0.010s
success delete …Run Code Online (Sandbox Code Playgroud) 我的网站使用 Gatsby 和 NetlifyCMS,目前在GitHub Actions中运行工作流程时收到以下错误消息:
error "gatsby-plugin-manifest" threw an error while running the onPostBootstrap lifecycle:
Error: Input file contains unsupported image format
Run Code Online (Sandbox Code Playgroud)
很奇怪的是,在我的本地系统上使用gatsbydevelop和gatsbybuild并没有出现该错误
我的插件数组如下所示:
plugins: [
{
resolve: `gatsby-plugin-typography`,
options: {
pathToConfigModule: `src/utils/typography`,
},
},
{
// keep as first gatsby-source-filesystem plugin for gatsby image support
resolve: "gatsby-source-filesystem",
options: {
path: `${__dirname}/static/img`,
name: "uploads",
},
},
`gatsby-plugin-smoothscroll`,
`gatsby-plugin-react-helmet`,
`gatsby-plugin-twitter`,
{
resolve: "gatsby-plugin-netlify-cms",
options: {
modulePath: `${__dirname}/src/cms/cms.js`,
htmlFavicon: `src/images/favicon.png`,
},
},
`gatsby-plugin-no-index`,
`gatsby-plugin-transition-link`, …Run Code Online (Sandbox Code Playgroud) 正如我所看到的,当仅查询 contentfulBlogPost 原始数据时,不再有 json 选项。我能够进行一些更改以获取身体中的所有内容,除了该帖子中的图像。
如果我在 GraphQL Playground 中进行测试,我可以获得图像 id 和 url,但仅此而已。
query {
allContentfulAsset {
edges {
node {
id
file {
url
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图找到一个如何获取嵌入图像的示例,但没有运气......
import React from 'react'
import { graphql } from 'gatsby'
import { documentToReactComponents } from '@contentful/rich-text-react-renderer'
import Layout from '../components/layout'
export const query = graphql`
query($slug: String!) {
contentfulBlogPost(slug: {eq: $slug}) {
title
publishedDate(formatString: "MMMM Do, YYYY")
body {
raw
}
}
allContentfulAsset {
edges {
node {
id
file …Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像的路径作为道具传递给组件
注意:组件和索引页都可以通过相同的路径访问图像。当我将路径作为道具传递时它不起作用
在下面的代码中,我尝试使用 GatbsyImage 和 StaticImage 标签似乎都失败了
索引.js
import React from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import '../styles/index.scss';
import Main from '../Layouts/main';
import ImageOverlay from '../components/ImageOverlay';
import { StaticImage } from "gatsby-plugin-image"
function Home(){
// Home page images
return (
<Main >
<section className="home_page">
<ImageOverlay path="../images/home/places/portblair.png"> </ImageOverlay>
</section>
</Main>
)
}
export default Home
Run Code Online (Sandbox Code Playgroud)
组件/ImageOverlay.js
import React from 'react';
import { GatsbyImage, StaticImage } from "gatsby-plugin-image"
const ImageOverlay = ({path}) =>{
return(
<div>
<GatsbyImage image={path}></GatsbyImage>
<StaticImage src={path}></StaticImage>
</div>
)
}
export default …Run Code Online (Sandbox Code Playgroud) 我将 GatsbyJS 与 TailwindCSS 一起使用,当我尝试将 tailwind 样式从 gatsby-image-plugin 传递到 StaticImage 的包装器中时,现有样式不会被覆盖(即 gatsby-image-wrapper 和 gatsby-image-wrapper-constrained 样式) )。
<StaticImage src="https://images.pexels.com/photos/189349/pexels-photo-189349.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="just an image" placeholder="blurred" className="absolute z-0" objectFit="cover" />
Run Code Online (Sandbox Code Playgroud)
包装器的位置保持不变(gatsby-image-wrapper 和 gatsby-image-wrapper-constrained),而传递到组件中的一些类将被忽略。
有什么方法可以删除默认样式,或者有任何其他方法可以让传递的类正常工作吗?
我正在建立一个盖茨比网站来展示我的照片。我想要每张照片都有一个全屏页面,并且照片应该填满页面,但要尊重宽高比。
\n问题在于,虽然横向拍摄的照片受到正确限制,但纵向拍摄的照片填满了所有水平空间,但溢出了垂直空间。
\n在文档中有这样的声明
\n\n\n如前所述,使用流体类型的图像会被拉伸以匹配容器\xe2\x80\x99s 的宽度和高度
\n
然而,我观察到的行为是它只拉伸以匹配宽度,而不是高度。
\n我将问题简化为这个小示例,它尝试将图像包含在 400x400px 容器中:
\nimport React from "react"\nimport { graphql } from "gatsby"\nimport Layout from "../components/layout"\nimport Img from "gatsby-image"\n\nexport default (props) => {\n const { data } = props;\n return (\n <Layout>\n <div style={{height: "400px", width: "400px", background: "white" }}>\n <Img fluid={ data.file.childImageSharp.fluid } />\n </div>\n </Layout>\n )\n}\n\nexport const query = graphql`\n query($id: String!) {\n file(id: { eq: $id }) {\n childImageSharp {\n fluid(maxWidth: 500, …Run Code Online (Sandbox Code Playgroud) gatsby ×10
gatsby-image ×10
graphql ×2
javascript ×2
contentful ×1
mdxjs ×1
netlify ×1
raw ×1
reactjs ×1
strapi ×1
twitter ×1
typescript ×1