最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactDOMServer.renderToStaticMarkup full markup - Stack Overflow

programmeradmin2浏览0评论

I'm using ReactDOMServer to generate a static site via the server side and it doesn't seem to like this ponent specifically the opening <!DOCTYPE html> tag. (see below)

I'm doing this as I'm trying to use React to fully render a page via the server-side for IE8 patibility and eventually bee an isomorphic app.

Is there a best practice on how to fully render static markup with React via the server-side (with inclusions of the opening html tags, etc.)?

'use strict';

import React from 'react';

export default class Root extends React.Component {

  render() {
    return (
      <!DOCTYPE html>
      <head>
      <title>Hello World!</title>
      </head>
      <body>

      <h1>Hello World!</h1>

      </body>
      </html>
    );
  }

}

I'm using ReactDOMServer to generate a static site via the server side and it doesn't seem to like this ponent specifically the opening <!DOCTYPE html> tag. (see below)

I'm doing this as I'm trying to use React to fully render a page via the server-side for IE8 patibility and eventually bee an isomorphic app.

Is there a best practice on how to fully render static markup with React via the server-side (with inclusions of the opening html tags, etc.)?

'use strict';

import React from 'react';

export default class Root extends React.Component {

  render() {
    return (
      <!DOCTYPE html>
      <head>
      <title>Hello World!</title>
      </head>
      <body>

      <h1>Hello World!</h1>

      </body>
      </html>
    );
  }

}

let html = ReactDOMServer.renderToStaticMarkup(<Root />);

Bonus: Although a simple DOCTYPE is breaking it, eventually I'd like to add additional IE tags like below at the top.

<!--[if lt IE 7]>  <html dir="ltr" lang="en-US" class="no-js ie ie6 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 7]>     <html dir="ltr" lang="en-US" class="no-js ie ie7 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 8]>     <html dir="ltr" lang="en-US" class="no-js ie ie8 lte9 lte8"> <![endif]-->
<!--[if IE 9]>     <html dir="ltr" lang="en-US" class="no-js ie ie9 lte9"> <![endif]-->
<!--[if gt IE 9]>  <html dir="ltr" lang="en-US" class="no-js"> <![endif]-->
<!--[if !IE]><!--><html><!--<![endif]-->

Share Improve this question asked Feb 9, 2016 at 17:34 vutranvutran 8878 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You'll likely want to emit the DOCTYPE header outside of your React ponent and replace <!DOCTYPE html> with an <html> node. JSX isn't HTML and just maps your elements to a corresponding React.createElement() call, which doesn't make sense for a DOCTYPE.

Take a look at these for reference:

  • Emitting a DOCTYPE before rendering the ponent

res.send('<!doctype html>\n' + ReactDOM.renderToString(<Html />));

  • An HTML ponent that handles tag generation

render() { return ( <html lang="en-us"> <head></head> ... </html>) }

发布评论

评论列表(0)

  1. 暂无评论