Next.js: 使用 `dangerouslySetInnerHTML` 时服务器无法正确呈现

创建于 2016-11-05  ·  3评论  ·  资料来源: vercel/next.js

背景

我使用Prismic.io作为 CMS 并在getInitialProps()获取这些数据。 内容已预先格式化为 HTML,因此我使用的是dangerouslySetInnerHTML

预期行为

在服务器和客户端上执行时, render()的输出是相同的。

实际行为

render()在服务器端输出错误,与客户端不匹配。

从服务器渲染:

image

从客户端渲染

image

不同之处在于内容在服务器上没有正确的样式,因为 DOM 节点没有正确嵌套。

来自服务器的 DOM:

image

来自客户端的 DOM:

image

相关代码

const htmlFor = (type) => ({__html: this.props[type]})
<Article heading="Devotional" html={true} >
   {htmlFor('devotional')}
</Article>

const Article = ({heading, children, date, link, style, html}) => (
  <article
    className="mv3 mv4-m mv5-l measure"
    style={style}
  >
    <header>
      <h1 className="f3 f2-ns mt0 lh-title i garamond bb bw3 b--gold">
        {heading}
      </h1>
    </header>
    { html
      ? <p className="lh-copy" dangerouslySetInnerHTML={children} />
      : <p className="lh-copy" >{children}</p>
    }

    <footer>
      { date
        ? <p className="f6 i">{date}</p>
        : null
      }
      {link
        ? <a href="#" className='link dim mid-gray bb b--gold'>
            {link}
          </a>
        : null
      }
    </footer>
  </article>
)

更多信息

此部署和/_src在此处公开: https :

最有用的评论

@flybayer感谢您的报告。 我可能是错的,但是通过 HTML 验证器传递你的 sith 会给我这个错误https://validator.w3.org/nu/?doc=https%3A%2F%2Fthefaithnet-ynikfpghwu.now.sh%2F我认为是因为在 HTML 中嵌套p标签无效。 您可以尝试使用 div 而不是外部p吗?

所有3条评论

@flybayer感谢您的报告。 我可能是错的,但是通过 HTML 验证器传递你的 sith 会给我这个错误https://validator.w3.org/nu/?doc=https%3A%2F%2Fthefaithnet-ynikfpghwu.now.sh%2F我认为是因为在 HTML 中嵌套p标签无效。 您可以尝试使用 div 而不是外部p吗?

@impronunciable哇,就是这样!

很好的捕获,非常感谢您的帮助!

很高兴它起作用了!

2016 年 11 月 6 日星期日上午 11:57 Brandon Bayer通知@ github.com
写道:

@impronunciable https://github.com/impronunciable哇,就是这样!

很好的捕获,非常感谢您的帮助!


你收到这个是因为你被提到了。

直接回复本邮件,在GitHub上查看
https://github.com/zeit/next.js/issues/213#issuecomment -258693902,或者静音
线程
https://github.com/notifications/unsubscribe-auth/AAKHp5EvRuw2GOjOViKL0MjKr0Vrt65sks5q7gbtgaJpZM4KqaPT
.

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

timneutkens picture timneutkens  ·  72评论

tomaswitek picture tomaswitek  ·  73评论

nickredmark picture nickredmark  ·  60评论

robinvdvleuten picture robinvdvleuten  ·  74评论

dunika picture dunika  ·  58评论