Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
renderWithStream(url, data = {}, routerContext = {}) {
// let modules = [];
const sheet = new ServerStyleSheet();
const extractor = new ChunkExtractor({ statsFile: this.statsFile });
const jsx = extractor.collectChunks(
sheet.collectStyles(
)
);
const htmlStream = sheet.interleaveWithNodeStream(
ReactDOMServer.renderToNodeStream(jsx)
);
const renderedScriptTags = extractor.getScriptTags();
const renderedLinkTags = extractor.getLinkTags();
const renderedStyleTags = extractor.getStyleTags();
/*console.log('renderedScriptTags: \n', renderedScriptTags);
console.log('renderedLinkTags: \n', renderedLinkTags);
console.log('renderedStyleTags: \n', renderedStyleTags);*/
return {
htmlStream,
extractor,
scriptTags: renderedScriptTags,
linkTags: renderedLinkTags,
styleTags: renderedStyleTags,
};
}
}
if (config.type !== 'ssr') {
const str = await renderLayoutForFass(ctx)
return str
}
if (isLocal) {
// 本地开发环境下每次刷新的时候清空require服务端文件的缓存,保证服务端与客户端渲染结果一致
delete require.cache[serverJs]
}
if (!global.serverStream || isLocal) {
global.serverStream = typeof serverJs === 'string' ? require('@/' + serverJs).default : serverJs
}
const serverRes = await global.serverStream(ctx)
const stream = ReactDOMServer.renderToNodeStream(serverRes)
return stream
}
const renderStreamed = async (ctx, path, clientStats, outputPath) => {
// Grab the CSS from our sheetsRegistry.
clearChunks();
const store = await configureStore(ctx);
if (!store) return; // no store means redirect was already served
const stateJson = JSON.stringify(store.getState());
const {css} = flushChunks(clientStats, {outputPath});
const chunkNames = [];
const app = createApp(App, store, chunkNames);
const stream = renderToNodeStream(app).pipe(renderStylesToNodeStream());
// flush the head with css & js resource tags first so the download starts immediately
const early = earlyChunk(css, stateJson);
// DO not use redis cache on dev
let mainStream;
if (process.env.NODE_ENV === 'development') {
mainStream = ctx.body;
}
else {
mainStream = createCacheStream(path);
mainStream.pipe(ctx.body);
}
mainStream.write(early);
store.runSaga(sagas).done.then(() => {
const helmet = Helmet.renderStatic();
res.status(200).write(renderHeader(helmet));
const preloadedState = store.getState();
const css = styleManager.sheetsToString();
const htmlSteam = renderToNodeStream(appWithRouter);
htmlSteam.pipe(res, { end: false });
htmlSteam.on('end', () => {
res.write(renderFooter(css, loadableState, preloadedState));
return res.send();
});
});
return new Promise<{}>((resolve, reject) => {
let stream = ReactDOMServer.renderToNodeStream(view)
let buffers: Uint8Array[] = []
stream.on('data', chunk => buffers.push(chunk))
stream.on('end', () => {
React.createElement = createElement
resolve(Buffer.concat(buffers))
})
stream.on('error', error => {
if (!controller) {
React.createElement = createElement
return reject(error)
}
if (controller.errorDidCatch) {
controller.errorDidCatch(error, 'view')
}
const reactToStream = (Component: React.FunctionComponent, props: object): Stream => {
return renderToNodeStream(React.createElement(Component, props))
}
const device = devices[req.device.type]
const props = {
scripts: clientStats[device]
.chunks
.filter(chunk => chunk.initial)
.reduce((files, chunk) => files.concat(`${clientStats[device].publicPath}${chunk.files[0]}`), []),
}
res.status(200)
res.contentType('text/html; charset=utf-8')
res.write('')
const Server = servers[req.device.type]
const stream = renderToNodeStream().pipe(renderStylesToNodeStream())
stream.pipe(res, {end: false})
stream.on('end', () => res.end())
}
renderToString(jsx);
const meta = metaTagsInstance.renderToString();
const sagasInProgress = store.sagas.map(saga => saga.toPromise());
store.dispatch(END);
await Promise.all(sagasInProgress);
const reduxState = store.getState();
stream.push(renderHeader(meta));
ctx.status = 200;
ctx.res.write(renderHeader(meta));
const htmlSteam = renderToNodeStream(jsx);
htmlSteam.pipe(ctx.res, { end: false });
await readHTMLStream(htmlSteam);
let scripts = '';
if (!!process.env.FRONTEND_HAS_VENDOR) {
scripts += '\n';
}
scripts += webExtractor.getScriptTags();
ctx.res.write(
renderFooter(
reduxState,
process.env.NODE_ENV === 'development' ?
`<style type="text/css">${[...css].join('')}</style>` :
const handleRequest = (App, opts) => async (req, res) => {
if (!opts.raw && !opts.noWrap) res.write(header)
if (!opts.noWrap) res.write('<div id="div">')
const props = Object.assign({}, opts, { req, res })
delete props.script
const el = isAsync(App)
? await createAsyncElement(App, props)
: React.createElement(App, props)
const stream = opts.bundle
? renderToNodeStream(el)
: renderToStaticNodeStream(el)
stream.pipe(res, { end: false })
stream.on('end', async () => {
if (!opts.noWrap) res.write('</div>')
if (opts.script) {
const json = jsonStringify(props)
res.write(``)
res.write(``)
}
res.end()
})
stream.on('error', error => {
const getApplicationStream = (originalUrl, context) => {
const helmetContext = {}
const app = (
)
const sheet = new ServerStyleSheet()
return sheet.interleaveWithNodeStream(
renderToNodeStream(sheet.collectStyles(app))
)
}