reactjs - 如何删除`
问题描述
The resulting static html code from gatsby-image
is something like this:
<div class=" gatsby-image-wrapper" style="position: relative; overflow: hidden;">
<!-- Placeholder here (removed for abreviation)... -->
<picture>
<!-- Image tag and `sources` here... -->
</picture>
<!-- ⚠ This is what I want to remove ⚠ -->
<noscript>
<picture>
<!-- Image tag and `sources` here... -->
</picture>
</noscript>
<!-- ⚠ This is what I want to remove ⚠ -->
</div>
I couldn't find an option to <noscript>
in the docs.
This is the image component:
import * as React from "react"
import { StaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"
const MyPhoto = () => (
<StaticQuery
query={graphql`
query {
placeholderImage: file(relativePath: { eq: "image.png" }) {
childImageSharp {
fluid(maxWidth: 160) {
...GatsbyImageSharpFluid_withWebp
}
}
}
}
`}
render={data => <Img loading="eager" fluid={data.placeholderImage.childImageSharp.fluid} />}
/>
)
export default MyPhoto
Pretty much the default that comes with this starter template: gatsby-starter-typescript-jest.
This is the gatsby-config.js, I've removed some unecessary comments and properties:
module.exports = {
plugins: [
`gatsby-plugin-preact`,
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {...myManifestOptions},
},
`gatsby-plugin-typescript`,
],
}
The reason is that I just don't want to add support for disabled javascript.
解决方案
You can't remove that <noscript>
behavior since it's inherent by default gatsby-image
and it doesn't add any kind of customization to avoid it.
As it has been said by @Mike 'Pomxa' Mamermans it's strongly suggested to leave it. It's not a matter of "I just don't want to add support for disabled javascript", it's a matter of default fallback for old browsers or browsers that have the JavaScript disabled, and trying to change this behavior will thrive you in weird caveats since you will need to change the package functionality itself from node_modules
so your change won't be persistent.
If you want to add this behavior you'll need to add a PR to Gatsby's repository providing a solution for your use-case since what you are trying to achieve by removing the <noscript>
tag it's not intended behavior for gatsby-image
.
It also doesn't make sense to remove functionality that potentially will make your site reach more people at default cost, in a meaningless amount of bytes that those lines add.
推荐阅读
- go - 如何进行 For-Else 循环(其他语言的 While-Else)?
- git - 从 heroku buildpack 定位二进制文件时遇到问题
- c# - IdentityServer4 中带有授权代码流的“Invalid_grant”错误
- botframework - MS Teams 中的 OAuth 卡?
- python - 如何从json字典中的节点中提取属性?
- haskell - (l, r)之前的~是什么意思
- java - TableColumn #setCellValueFactory 用于具有 HashMaps 的类
- css - 使用 Angular 切换 div 类的可见性
- javascript - 双引号内的单引号弄乱了javascript代码
- c# - 如何修复“枚举约束失败”?