Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getAsyncBundle = (modules) => {
let loadableJson = getBundleAssets();
let bundles = [];
try {
bundles = getBundles(loadableJson, modules)
} catch (e) {
// todo: logger
}
return bundles
.filter((bundle) => {
return /\.js$/.test(bundle.file)
})
.map((item) => item ? `${preLoc}${item.file}` : '')
};
}
const loadableBundleStatsPath = path.join(settings.build, 'react-loadable.json')
const craBundleStatsPath = path.join(settings.build, 'asset-manifest.json')
let loadableBundleStats = false
let craBundleStats = false
try { loadableBundleStats = await readSourceFile(loadableBundleStatsPath, true) } catch (err) {}
try { craBundleStats = await readSourceFile(craBundleStatsPath, true) } catch (err) {}
let allBundles = false
// generate assets map with the ReactLoadable bundles stats
if (loadableBundleStats !== false) {
try {
allBundles = getBundles(loadableBundleStats, modules)
} catch (err) {
console.log(`[ssr] "react-loadable.json" bundles stats failed - ${err.message}`)
}
}
// use basic CRA's "asset-manifest.json"
if (allBundles === false && craBundleStats !== false) {
try {
allBundles = Object.keys(craBundleStats)
.filter((bundleName) => modules
.map(moduleName => moduleName.replace('./', ''))
.some((moduleName) => (
bundleName.indexOf(`${moduleName}.js`) !== -1 ||
bundleName.indexOf(`${moduleName}.css`) !== -1
)))
.map((bundleName) => ({
// the specific status and redirect header and end the response
if (staticContext.url) {
res.status(301).setHeader('Location', staticContext.url);
res.end();
return;
}
// Check page status
const status = staticContext.status === '404' ? 404 : 200;
const head = Helmet.renderStatic();
// $FlowFixMe: isn't an issue
const loadableManifest = require('../public/loadable-assets.json');
const bundles = getBundles(loadableManifest, modules);
let assets = bundles
.map(({ publicPath }) =>
!publicPath.includes('main') ? publicPath : ''
)
// In development, main.css and main.js are webpack default file bundling name
// we put these files into assets with publicPath
.concat(['/assets/main.css', '/assets/main.js']);
if (!__DEV__) {
// $FlowFixMe: isn't an issue
const webpackManifest = require('../public/webpack-assets.json');
assets = bundles
.map(({ publicPath }) => publicPath)
// .filter((item, index) => bundles.indexOf(item) >= index)
.concat(
Promise.all(fetchData).then(() => {
const state = store.getState();
const html = renderToString(getComponent());
const bundles = stats && getBundles(stats, modules) || [];
const markup = render(html, state, bundles);
const status = matches.length && matches[0].match.path === '*' ? 404 : 200;
// A 301 redirect was rendered somewhere if context.url exists after
// rendering has happened.
if (context.url) {
return res.redirect(302, context.url);
}
res.contentType('text/html');
return res.status(status).send(markup);
});
}
const sheet = new ServerStyleSheet()
let markup = renderToString(
sheet.collectStyles(
modules.push(moduleName)}>
)
)
const helmet = Helmet.renderStatic()
const styleTags = sheet.getStyleTags()
const bundles = getBundles(stats, modules)
const chunks = bundles.filter(bundle => bundle.file.endsWith('.js'))
const globals = { initialData: initialData, context: serverContext }
let html = template(styleTags, markup, chunks, globals, helmet)
return res.send(html)
})
.catch(err => console.error(err))
.then(() => {
const initialState = store.getState();
const modules = [];
const InitialView = ( modules.push(moduleName)}>
);
const data = ReactDom.renderToString(InitialView);
const allBundles = getBundles(stats, modules);
const cssBundles = [];
const jsBundles = [];
allBundles.forEach(item => item.file.endsWith('.css') ? cssBundles.push(item.publicPath) : jsBundles.push(item.publicPath) );
const head = Helm.rewind();
const outputhtml = `
${head.title.toString()}
${head.meta.toString()}
${head.link.toString()}
${cssBundles.map(item => ``)}
${assets.vendors.css ? `` : ''}
let content = renderToString(
modules.push(moduleName)}>
{mobile === null ? : }
)
let bundles = getBundles(stats, modules)
const helmet = Helmet.renderStatic()
const state = store.getState()
state.mobile = mobile
return template(sheetsRegistry, helmet, state, content, bundles)
}
const context = {};
const modules = [];
const markup = ReactDOMServer.renderToString(
modules.push(moduleName)}>
);
if (context.url) {
res.redirect(context.url);
} else {
const fullMarkup = indexHtml({
helmet: Helmet.renderStatic(),
serverData: data,
bundles: getBundles(stats, modules),
markup
});
res.status(200).send(fullMarkup);
}
}
const context = {};
const modules = [];
const markup = ReactDOMServer.renderToString(
modules.push(moduleName)}>
);
if (context.url) {
res.redirect(context.url);
} else {
const fullMarkup = indexHtml({
helmet: Helmet.renderStatic(),
serverData: data,
bundles: getBundles(stats, modules),
markup
});
res.status(200).send(fullMarkup);
}
}