【问题标题】:Next.JS + AMP CSSNext.JS + AMP CSS
【发布时间】:2020-04-16 07:47:25
【问题描述】:

我在 Next.js 中遇到了 AMP 和 CSS 问题。在我的头部组件中,我有:

<Head>
    <style amp-custom>{`
        // CSS Here
    `}</style>
</Head>

在 HTML 源代码中,它显示为 &lt;style amp-custom=""&gt;&lt;/style&gt;&lt;style&gt;(CSS Here)&lt;/style&gt;

在控制台中我收到此错误:The mandatory attribute 'amp-custom' is missing in tag 'style amp-custom (transformed)'.

如何同时使用 AMPHTML 的 CSS 和 Next 规则?我尝试过的所有其他方法(例如使用 @zeit/next-sass 从文件导入)都会导致 CSS 根本不被渲染。这是我找到的唯一工作版本。

【问题讨论】:

    标签: css reactjs react-native next.js amp-html


    【解决方案1】:

    截至 2020 年 9 月,我也遇到了这个问题。我是新手,但没有官方教程的帮助。我确实找到了解决方法。

    首先,我想指出他们告诉您的 Next.js 中的几件事。

    1. 非 AMP 页面样式通常放在 next.js 示例中的 _document.js 中。
    </Head>
    <style jsx global>{ reset }</style>
    <style jsx global>{ globals }</style>
    <body>
      <Main />
      <NextScript />
     </body>
    
    1. 他们在教程中提到将&lt;style amp-custom&gt;。他们没有说在哪里,但它应该在 index.js 的&lt;Head&gt;&lt;/Head&gt; (或单个页面的任何 .js 文件)或每个页面的 _document.js 内。

    好的,听起来不错,但有部分错误!

    我将解释在 Next.JS 中打开 amp pages 时我发现它的作用。

    所以在一个单独的页面上,比如index.js,你需要把这段代码放在最上面:

    export const config = {
      amp: true,
    }
    

    那你得把这个放到返回函数中:

    const isAmp = useAmp()

    教程中的标准说明。现在 AMP 已打开,会发生以下情况:

    1. &lt;style amp-custom&gt; 中的任何内容都会变成&lt;style&gt;

    2. &lt;style jsx&gt; 中的任何内容都将转换为 &lt;style amp-custom&gt; 标记。

    3. 除了 #2 之外,它还注入了一个唯一的随机索引,该索引会破坏任何放入生成的&lt;style amp-custom&gt; 标记中的 css 代码。

     <style amp-custom>.jsx-2373233908{/* your CSS code that you put in <style jsx> from before */}</style>
    

    并且 .jsx-########### 会在标签 'style amp-custom' 中引发“/错误 CSS 语法错误 - 不完整的声明。”当你尝试编译时。

    这是相反和奇怪的行为。是的。我不明白为什么会这样,但我是新手。

    所以我的解决方法是这样的:

    1. 安装您的 CSS 框架包或将您的 CSS 文件放入样式文件夹(假设位于:./styles/styles.css)
    2. 我还从您的终端窗口添加了原始加载程序。因为我喜欢将我的 css 放在一个文件中,而不是将其与代码内联。现实一点,你要分离 CSS,你需要加载那个文件。

    npm install raw-loader --save-dev

    1. 在您的 _document.js 中加载 CSS(这是我的整个 _document.js)。我使用带有 fixCSS 的 "}" 和 "{" 来转义 .jsx-########### 并且注入的代码神奇地消失了。
    import Document, { Html, Head, Main, NextScript } from 'next/document'
    
    import styleCSS from '!!raw-loader!../styles/styles.css';
    const fixCSS = `}${styleCSS}{`;
    
    class MyDocument extends Document {
      static async getInitialProps(ctx) {
        const initialProps = await Document.getInitialProps(ctx)
        return { ...initialProps }
      }
    
      render() {
        return (
          <Html lang="en">
          <Head>
          </Head>
          <style jsx>{`
          ${fixCSS}
        ` }</style>
            <body>
              <Main />
              <NextScript />
            </body>
          </Html>
        )
      }
    }
    export default MyDocument
    

    就是这样。现在,您导入的 CSS 会显示在 AMP 页面上。请记住,这是 2020 年 9 月在我的 package.json 中使用这些包的:

      "dependencies": {
        "amp": "^0.3.1",
        "next": "^9.5.3-canary.25",
        "next-env": "^1.1.1",
        "react": "^16.13.1",
        "react-dom": "^16.13.1"
    },
      "devDependencies": {
        "cssnano": "^4.1.10",
        "now": "^19.2.0",
        "raw-loader": "^4.0.1"
      },
    

    【讨论】:

    • 谢谢,这适用于我在 /pages 目录中有特定 amp 文件的用例
    【解决方案2】:

    ...必须是:&lt;style jsx&gt;...&lt;/style&gt;。我整天都在寻找解决方法的非常愚蠢的错误。 :/

    【讨论】:

    【解决方案3】:

    试试这个:

    <Head>
        <style
          amp-custom=""
          dangerouslySetInnerHTML={{
            __html: `
              amp-img {
                border: 1px solid black;
              }
            `,
          }}
        ></style>
    </Head>
    

    【讨论】:

    • 谢谢,在重新阅读了几个小时后,谢谢你,我实际上正在这样做,但它不起作用
    • 你的是我找到的唯一可行的解​​决方案
    【解决方案4】:

    试试:

    <style jsx amp-custom>
    `
      ... my css
    `
    </style>
    

    我刚刚测试了它,它工作正常。不是最好的方法,NextJS 应该记录在某处添加 css 的方法。

    【讨论】:

    • amp-custom 标记在样式元素中没有任何效果。没有它它也能工作,所以它与@Ethan 回答是多余的。
    猜你喜欢
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2020-08-17
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多