Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.get(`*`, (req, res) => {
const reactHelmet = ReactHelmet.renderStatic();
const requestPath = req.path;
const context = {}; // context object contains the results of the render
const appHtml = renderToString(
<main>
);
if (context.url) {
// Somewhere a `` was rendered
res.redirect(301, context.url);
} else if (requestPath === "/get-fringe-events") {
getFringeEvents(res);
} else if (requestPath === "/get-house-events") {
getHouseEvents(res);</main>
const initialState = store.getState()
const context = {}
const appHtml = renderToString(
)
if (context.url) {
response.redirect(context.url)
} else {
// Get Meta header tags
const helmet = Helmet.renderStatic()
const styles = flushToHTML()
const html = view(APP_URL, NODE_ENV, helmet, appHtml, styles, initialState)
// Reset the state on server
store.dispatch({
type: 'RESET'
})
// Finally send generated HTML with initial data to the client
return response.status(status).send(html)
}
})
.catch(error => {
return async function handler (req, res) {
const routerContext = { url: null }
const serverContext = { req, res }
try {
const { markup, data, headTags, bodyTags } = await renderRoute(App, routerContext, serverContext)
// redirected (see: https://reacttraining.com/react-router/web/api/StaticRouter/context-object)
if (routerContext.url) {
res.writeHead(302, { Location: routerContext.url })
res.end()
return
}
const helmet = Helmet.renderStatic()
const html = toHtml({ helmet, markup, data, bundleUrl, headTags, bodyTags })
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html, 'utf8')
} catch (err) {
const content = JSON.stringify({ status: err.statusCode || 500, message: err.message, name: err.name }, undefined, 2)
res.setHeader('Content-Type', 'text/json; charset=utf-8')
res.setHeader('Content-Length', Buffer.byteLength(content))
res.end(content, 'utf-8')
}
}
}
);
// get the stringified state
const reduxState = JSON.stringify(store.getState());
// map required assets to script tags
const extraChunks = extractAssets(manifest, modules)
.map(c => ``);
// get HTML headers
const helmet = Helmet.renderStatic();
// now inject the rendered app into our html and send it to the client
return res.send(
htmlData
// write the React app
.replace('<div id="root"></div>', `<div id="root">${html}</div>`)
// write the string version of our state
.replace('__REDUX_STATE__={}', `__REDUX_STATE__=${reduxState}`)
// append the extra js assets
.replace('', extraChunks.join('') + '')
// write the HTML header tags
.replace('<title></title>', helmet.title.toString() + helmet.meta.toString())
);
});
}
);
try {
const content = await renderToStringWithData(clientApp);
const initialState = client.extract();
return {
head: Helmet.renderStatic(),
html: content,
initialData: { ...initialData, initialProps },
apolloState: initialState,
sheet: sheet,
};
} catch (error) {
console.log("error :", error);
}
};
.then(data => {
try {
const app = ReactDOMServer.renderToString(
);
const helmet = Helmet.renderStatic();
const htmlAttrs = helmet.htmlAttributes.toString();
const bodyAttrs = helmet.bodyAttributes.toString();
if (context.url) {
return Promise.reject({
error: 'RedirectException',
message: '',
context
});
}
return '' +
`` +
'' +
`${helmet.title.toString()}` +
`${helmet.meta.toString()}` +
);
const materialUICss = sheetsRegistry.toString();
const cssArr = Array.from(css);
const helmet = Helmet.renderStatic();
const currentState = store.getState();
const stringifiedInitialReduxState = JSON.stringify(currentState);
const html: string = await generateFullHTML({
reactDom: renderedHTML,
scriptPath: scriptVersion,
helmet,
initialState: stringifiedInitialReduxState,
css: cssArr.join("") + materialUICss,
version,
});
return {
html,
statusCode: routeContext.statusCode,
};
export default Component => ({ script, stylesheet }, props) => {
const component = React.createElement(Component, props)
const html = ReactDOMServer.renderToString(component)
const helmet = Helmet.renderStatic()
const serializedProps = encodeURIComponent(JSON.stringify(props))
return [
'',
``,
'',
'',
'',
helmet.title.toString(),
helmet.base.toString(),
helmet.meta.toString(),
helmet.link.toString(),
helmet.style.toString(),
flushToHTML(),
stylesheet && ``,
'',
return (req, res, next) => {
const context = {};
const markup = ReactDOMServer.renderToString(
);
const helmet = Helmet.renderStatic();
res.status(200).send(Template({
markup: markup,
helmet: helmet,
}));
};
}
const d = device(req.get('user-agent'))
let [html, preloadedState, modules] = await renderer(
true,
renderToString,
null,
req,
await API.filterCourses(undefined, { limit: 10 }),
await API.filterCourses64(undefined, { limit: 16 }),
websiteStats,
d.is('phone'),
d.is('tablet')
)
const getBundles = require('react-loadable/webpack').getBundles
const stats = require('../../build/react-loadable.json')
const bundles = getBundles(stats, modules)
const helmet = Helmet.renderStatic()
const index = cheerio.load($index.html())
index('#root').html(html)
index('head').prepend(helmet.title.toString())
index('head').prepend(helmet.meta.toString())
index('head').prepend(bundles.map((bundle: any): string =>
``
).join('\n'))
index('body').prepend(``)
res.send(index.html())
})