简体   繁体   English

如何将强制 AMP HTML 属性添加到 HTML 标记

[英]How to add mandatory AMP HTML attribute to HTML tag

I'm using Gatsby and would like have my website identified an AMP website .我正在使用Gatsby并希望我的网站识别为AMP 网站

How do I add the mandatory AMP attribute to the <html> tag?如何将强制 AMP 属性添加到<html>标记?

In order to add the mandatory AMP HTML attribute to your <html> tag using Gatsby, you may want to:为了使用 Gatsby 将强制性 AMP HTML 属性添加到您的<html>标记,您可能需要:

  1. Customise your html.js file by cloning the .cache/default-html.js to your src/ directory.通过将.cache/default-html.js克隆到src/目录来自定义html.js文件。

    You can achieve that by running the command line below;您可以通过运行下面的命令行来实现;

cp .cache/default-html.js src/html.js
  1. Then, add amp="" to the newly created src/html.js file as follows;然后,将amp=""添加到新创建的src/html.js文件中,如下所示;
<html amp="" {...props.htmlAttributes}>

Aside the option above, you can achieve same by simply adding the snippet below to your gatsby-ssr.js file;除了上面的选项,你可以通过简单地将下面的代码片段添加到你的gatsby-ssr.js文件中来实现相同的目的;

import React from "react"

// Adds an amp attribute to the <html> tag
export const onRenderBody = ({ setHtmlAttributes }) => {
  setHtmlAttributes({
    amp: ``,
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM