简体   繁体   English

Next.js 拦截响应

[英]Next.js intercept response

Here is snippet of what my server looks like这是我的服务器的片段

nextApp.prepare().then(() => {
    createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    handleNextRequest(req, res, parsedUrl);
    }).listen(port);
});

Once I get the response for the request, how can I append html comment or any tag basically?一旦我得到请求的响应,我如何基本上附加 html 注释或任何标签?

What I wanted to do is, before and after body tag, I want to add some comments.我想做的是,在body标签之前和之后,我想添加一些评论。

Current structure现行结构

<body></body>

What I want我想要的是

<!--someContext-->
  <body> some content </body>
<!--someMoreContext-->

You can do it by editing the _document special file of Next that used as a base template to the page.您可以通过编辑用作页面基本模板的 Next 的_document特殊文件来实现。

// ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

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

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