From 99348eaedb19a85ab596aa927008c451418ee194 Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Sat, 24 Jun 2017 10:19:40 -0700 Subject: [PATCH] [fix] StyleSheet: server-side rendering of styles Problem: The content of style sheets was being set as a child of 'style', which causes React to escape the content. This meant that the pointer-events selectors were broken (">" became ">") and pointer-events were disabled for the entire server-rendered document. Solution: Use 'dangerouslySetInnerHTML' to avoid the CSS text being escaped. Fix #546 --- docs/guides/getting-started.md | 2 +- .../renderApplication-test.js.snap | 123 +++++++++--------- .../__tests__/renderApplication-test.js | 6 +- src/apis/AppRegistry/renderApplication.js | 3 +- 4 files changed, 66 insertions(+), 68 deletions(-) diff --git a/docs/guides/getting-started.md b/docs/guides/getting-started.md index f17f5afb..bfbade7c 100644 --- a/docs/guides/getting-started.md +++ b/docs/guides/getting-started.md @@ -189,7 +189,7 @@ AppRegistry.registerComponent('App', () => AppContainer) // prerender the app const { element, stylesheets } = AppRegistry.getApplication('App', { initialProps }); const initialHTML = ReactDOMServer.renderToString(element); -const initialStyles = stylesheets.map((sheet) => ReactDOMServer.renderToString(sheet)).join('\n'); +const initialStyles = stylesheets.map((sheet) => ReactDOMServer.renderToStaticMarkup(sheet)).join('\n'); // construct HTML document const document = ` diff --git a/src/apis/AppRegistry/__tests__/__snapshots__/renderApplication-test.js.snap b/src/apis/AppRegistry/__tests__/__snapshots__/renderApplication-test.js.snap index f1ebf9f2..e9e68268 100644 --- a/src/apis/AppRegistry/__tests__/__snapshots__/renderApplication-test.js.snap +++ b/src/apis/AppRegistry/__tests__/__snapshots__/renderApplication-test.js.snap @@ -9,69 +9,62 @@ exports[`apis/AppRegistry/renderApplication getApplication 1`] = ` `; exports[`apis/AppRegistry/renderApplication getApplication 2`] = ` -Array [ - , - , -] +"" +`; + +exports[`apis/AppRegistry/renderApplication getApplication 3`] = ` +"" `; diff --git a/src/apis/AppRegistry/__tests__/renderApplication-test.js b/src/apis/AppRegistry/__tests__/renderApplication-test.js index 06c4bc92..80d7c02f 100644 --- a/src/apis/AppRegistry/__tests__/renderApplication-test.js +++ b/src/apis/AppRegistry/__tests__/renderApplication-test.js @@ -2,6 +2,7 @@ import { getApplication } from '../renderApplication'; import React from 'react'; +import ReactDOMServer from 'react-dom/server'; const RootComponent = () =>
; @@ -10,6 +11,9 @@ describe('apis/AppRegistry/renderApplication', () => { const { element, stylesheets } = getApplication(RootComponent, {}); expect(element).toMatchSnapshot(); - expect(stylesheets).toMatchSnapshot(); + stylesheets.forEach(sheet => { + const result = ReactDOMServer.renderToStaticMarkup(sheet); + expect(result).toMatchSnapshot(); + }); }); }); diff --git a/src/apis/AppRegistry/renderApplication.js b/src/apis/AppRegistry/renderApplication.js index f4227dc7..7bf114ec 100644 --- a/src/apis/AppRegistry/renderApplication.js +++ b/src/apis/AppRegistry/renderApplication.js @@ -37,7 +37,8 @@ export function getApplication(RootComponent: ReactClass, initialProps: ); const stylesheets = StyleSheet.getStyleSheets().map(sheet => - + // ensure that CSS text is not escaped +