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

How to add mandatory AMP HTML attribute to HTML tag

我正在使用 Gatsby and would like have my website identified an AMP website

如何将强制 AMP 属性添加到 <html> 标签?

为了使用 Gatsby 将强制性 AMP HTML 属性添加到您的 <html> 标签,您可能需要:

  1. 通过将 .cache/default-html.js 克隆到您的 src/ 目录来自定义您的 html.js 文件。

    您可以通过 运行 下面的命令行实现;

cp .cache/default-html.js src/html.js
  1. 然后,在新建的src/html.js文件中添加amp=""如下;
<html amp="" {...props.htmlAttributes}>

除了上面的选项,您只需将下面的代码片段添加到您的 gatsby-ssr.js 文件即可实现同样的效果;

import React from "react"

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