|
1 | 1 | import fs from 'fs'; |
2 | 2 | import React from 'react'; |
| 3 | +import serialize from 'serialize-javascript'; |
3 | 4 | import ReactDOMServer from 'react-dom/server'; |
4 | 5 | import { StaticRouter } from 'react-router-dom'; |
5 | | -import common, { clientDir, serverDir, cacheDir, SSRKEY, Logger } from '@srejs/common'; |
| 6 | +import common, { |
| 7 | + clientDir, |
| 8 | + serverDir, |
| 9 | + cacheDir, |
| 10 | + SSRKEY, |
| 11 | + Logger, |
| 12 | + filterXssByJson |
| 13 | +} from '@srejs/common'; |
6 | 14 | import { loadGetInitialProps } from './initialProps'; |
7 | 15 | import { DevMiddlewareFileSystem, getEntryList, WebpackReact as webPack } from '@srejs/webpack'; |
8 | 16 |
|
@@ -56,17 +64,6 @@ export const readPageHtml = (page) => { |
56 | 64 | }); |
57 | 65 | }; |
58 | 66 |
|
59 | | -const filterXss = (str) => { |
60 | | - var s = ''; |
61 | | - s = str.replace(/&/g, '&'); |
62 | | - s = s.replace(/</g, '<'); |
63 | | - s = s.replace(/>/g, '>'); |
64 | | - s = s.replace(/ /g, ' '); |
65 | | - s = s.replace("'", '''); |
66 | | - s = s.replace('"', '"'); |
67 | | - return s; |
68 | | -}; |
69 | | - |
70 | 67 | const writeFileHander = (cacheDir, cacheUrl, Content) => { |
71 | 68 | fs.exists(cacheUrl, (exists) => { |
72 | 69 | if (exists) { |
@@ -107,6 +104,7 @@ export const renderServer = async (ctx, initProps, ssr = true) => { |
107 | 104 | const context = {}; |
108 | 105 | let props = {}; |
109 | 106 | var { page, query } = ctx[SSRKEY]; |
| 107 | + query = filterXssByJson(query); |
110 | 108 | if (!getEntryList().has(page)) { |
111 | 109 | return false; |
112 | 110 | } |
@@ -154,32 +152,21 @@ export const renderServer = async (ctx, initProps, ssr = true) => { |
154 | 152 | }); |
155 | 153 | ctx.res.end(); |
156 | 154 | } else { |
157 | | - // 加载 index.html 的内容 |
158 | 155 | let data = await readPageHtml(page); |
159 | | - //进行xss过滤 |
160 | | - for (let key in query) { |
161 | | - if (query[key] instanceof Array) { |
162 | | - query[key] = query[key].map((item) => { |
163 | | - return filterXss(item); |
164 | | - }); |
165 | | - } else { |
166 | | - query[key] = filterXss(query[key]); |
167 | | - } |
168 | | - } |
| 156 | + const ssrData = { |
| 157 | + initProps: props, |
| 158 | + page, |
| 159 | + path: ctx[SSRKEY].path, |
| 160 | + query, |
| 161 | + options: ctx[SSRKEY].options |
| 162 | + }; |
169 | 163 | let rootNode = ctx[SSRKEY].options.rootNode; |
170 | 164 | let replaceReg = new RegExp(`<div id="${rootNode}"><\/div>`); |
171 | 165 | // 把渲染后的 React HTML 插入到 div 中 |
172 | 166 | let document = data.replace( |
173 | 167 | replaceReg, |
174 | 168 | `<div id="${rootNode}">${Html}</div> |
175 | | - <script>var __SSR_DATA__ = |
176 | | - { |
177 | | - initProps:${JSON.stringify(props || {})}, |
178 | | - page: "${page}", |
179 | | - path:"${ctx[SSRKEY].path}", |
180 | | - query:${JSON.stringify(query || {})}, |
181 | | - options:${JSON.stringify(ctx[SSRKEY].options || {})} |
182 | | - } |
| 169 | + <script>var __SSR_DATA__ = ${serialize(ssrData, { isJSON: true })} |
183 | 170 | </script>` |
184 | 171 | ); |
185 | 172 | document = renderDocumentHead(document, props); |
|
0 commit comments