Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
preRender(req, res, app) {
Cookie.plugToRequest(req, res);
//console.log('preRender hook', app);
// console.log(req.cookies);
return Promise.await(getDataFromTree(app));
},
dehydrateHook(req, res) {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
console.log('ERROR', error);
// throw new Error(error)
Raven.captureException(error)
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
/**/
reactCookie.plugToRequest(req, res)
let startingState = defaultState
try {
startingState = getStateFromToken()
} catch (err) {
}
const profileLanguageTag = startingState !== null &&
typeof startingState !== 'undefined' &&
typeof startingState.auth !== 'undefined' &&
typeof startingState.auth.userData !== 'undefined' &&
typeof startingState.auth.userData.language_tag ? startingState.auth.userData.language_tag : null
req.Locale = getLocale(req, profileLanguageTag);
// We are not authenticated
app.use((req, res) => {
if (__DEVELOPMENT__) {
// Do not cache webpack stats: the script file would change since
// hot module replacement is enabled in the development env
webpackIsomorphicTools.refresh()
}
reactCookie.setRawCookie(req.headers.cookie)
reactCookie.plugToRequest(req, res)
const historyNotSync = createHistory(req.originalUrl)
const store = createStore(historyNotSync, {
userAgent: req.headers['user-agent']
})
const history = syncHistoryWithStore(historyNotSync, store)
const assets = webpackIsomorphicTools.assets()
// assets.styles.z_datePicker = `${config.prefix}/DatePicker.css` to add css to index.html
function hydrateOnClient() {
res.send(`\n
${renderToStaticMarkup()}`)
}
export default function reactApplicationMiddleware(
request: $TsFixMe,
response: $TsFixMe
) {
cookie.plugToRequest(request, response);
// Ensure a nonce has been provided to us.
// See the server/middleware/security.js for more info.
if (typeof response.locals.nonce !== 'string') {
throw new Error('A "nonce" value has not been attached to the response');
}
const { nonce } = response.locals;
// It's possible to disable SSR, which can be useful in development mode.
// In this case traditional client side only rendering will occur.
if (config('disableSSR')) {
if (process.env.BUILD_FLAG_IS_DEV === 'true') {
// eslint-disable-next-line no-console
log({
title: 'Server',
fs.readdir(path.resolve(__dirname, bannerDir), (err, files) => {
if (err) {
//do nothing
console.log(err);
res.sendFile(path.resolve(__dirname, bannerDir) + '/' + file);
}else{
let randomIndex = Math.floor(Math.random() * files.length);
file = files[randomIndex];
res.sendFile(path.resolve(__dirname, bannerDir) + '/' + file);
}
});
return;
}
log.info({Id: req.reqId, Method: req.method, URL: req.url, IP: req.ip, Message: 'New request'});
cookie.plugToRequest(req,res);
debug('Executing loadIntl action');
context.getActionContext().executeAction(loadIntlMessages, req.locale, (err) => {
if (err) { //Hello, Vinay =)
err.statusCode = 503;
let html = '<h1>Not found locale</h1>';
debug('Sending markup');
res.type('html');
res.status(err.statusCode).send('' + html);
}
else{
debug('Executing navigate action');
context.getActionContext().executeAction(navigateAction, {
url: req.url,
reqId: req.reqId
}, (err) => {
if (err) {
match({routes, location: req.url}, (err, redirect, renderProps)=> {
if (err) {
res.status(500).send('Internal error :(')
} else if (redirect) {
res.redirect(302, redirect.pathname + redirect.search)
} else if (renderProps) {
store.dispatch(setUserAgent(req.get('user-agent')))
store.dispatch(setLocale(req.cookies.locale || defaultLocale))
const unplug = plugToRequest(req, res)
loadOnServer({store, renderProps}).then(
()=> {
const locale = req.cookies.locale || config().defaultLocale
const localeData = require(`react-intl/locale-data/${locale.split('-')[0]}`)
const messages = require(`../src/locales/${locale.split('-')[0]}.json`)
addLocaleData([...localeData])
const sheet = new ServerStyleSheet()
const html = renderToString(
sheet.collectStyles(
)
export default function(req, res) {
reactCookie.plugToRequest(req, res);
const store = createStore();
store.dispatch({
type: MEMBER_LOAD_AUTH
});
match({ routes: getRoutes(store), location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps && renderProps.components) {
const routeStatus = renderProps.routes.reduce((prev, cur) => cur.status || prev, null);
if (routeStatus) {
res.status(routeStatus);
}
server.get('*', (req, res) => {
cookie.plugToRequest(req, res);
return handle(req, res)
});
function plugInCookiesFromDotNet(cookieData: { key: string, value: string }[], res) {
const formattedData = {};
cookieData.forEach(keyValuePair => {
formattedData[keyValuePair.key] = keyValuePair.value;
});
cookie.plugToRequest({ cookies: formattedData }, res);
}