react-router#StaticRouter JavaScript Examples
The following examples show how to use
react-router#StaticRouter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: server.js From covid19Nepal-react with MIT License | 6 votes |
Serverrenderer = (req, res, next) => {
const context = {};
fs.readFile(path.resolve('./build/index.html'), 'utf8', (err, data) => {
if (err) {
console.error(err);
return res.status(500).send('An error occurred');
}
return res.send(
data.replace(
'<div id="root"></div>',
`<div id="root">${ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
)}</div>`
)
);
});
}
Example #2
Source File: RenderService.js From VoltranJS with MIT License | 6 votes |
renderHtml = (component, initialState, context) => {
// eslint-disable-next-line no-param-reassign
component.id = guid();
const initialStateWithLocation = { ...initialState, location: context, id: component.id };
const sheet = new ServerStyleSheet();
if (isWithoutHTML(context.query)) {
return PureHtml(component.path, component.name, initialStateWithLocation);
}
const children = ReactDOMServer.renderToString(
sheet.collectStyles(
<StaticRouter location={component.path} context={context}>
<ConnectedApp initialState={initialStateWithLocation} location={context} />
</StaticRouter>
)
);
const styleTags = sheet.getStyleTags();
const resultPath = `'${component.path}'`;
return Html({
resultPath,
componentName: component.name,
children,
styleTags,
initialState: initialStateWithLocation,
fullWidth: component.fullWidth,
isMobileFragment: component.isMobileFragment,
context
});
}
Example #3
Source File: index.js From react-myportfolio with MIT License | 5 votes |
app.get('*', (req, res) => {
var title = "My Portfolio | MERN Application | POCCODER";
var description = "My Portfolio is a MERN based Full-Stack web application where users can see all the projects that I have completed as a professional Software Developer.";
var thumb = "https://assets.poccoder.com/images/banner_my_portfolio.png";
var favicon = "https://assets.poccoder.com/images/deeptiman.png";
var link = "https://portfolio.poccoder.com";
const context = {}
const markup = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>${title}</title>
<meta name="keywords" content="poccoder, deeptiman poccoder, developer portfolio, mern application, blockchain portfolio, react-native portfolio, android portfolio, mongodb application, webpack application, webpack"/>
<meta name="description" content="${description}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@deeptimancode">
<meta name="twitter:title" content="${title}">
<meta name="twitter:description" content="${description}">
<meta name="twitter:creator" content="@deeptimancode">
<meta name="twitter:image" content="${thumb}">
<meta property="og:title" content="${title}" />
<meta property="og:description" content="${description}">
<meta property="og:type" content="article" />
<meta property="og:url" content="${link}" />
<meta property="og:image" content="${thumb}" />
<meta property="og:site_name" content="${title}" />
<link rel="canonical" href="${link}"/>
<link rel="shortcut icon" type="image/png" href="${favicon}"/>
<link rel="apple-touch-icon-precomposed" href="${thumb}"/>
</head>
<body>
<div id="root">${markup}</div>
<script src="client_bundle.js"></script>
</body>
</html>`;
res.send(html);
})
Example #4
Source File: index.js From reactjs-portfolio-mern-website with MIT License | 5 votes |
app.get('*', (req, res) => {
var title = "My Portfolio | Full Stack Mern Application";
var description = "A portfolio site is essential for every software developer to showcase projects and technical skills which demonstrates what you can do based upon your resume.";
var thumb = "https://assets.hackbotone.com/images/my-portfolio-full-stack-mern-application/Thumbnail.png";
var favicon = "https://assets.hackbotone.com/images/icons/anshuman_pattnaik.jpg";
var link = "https://myportfolio.hackbotone.com";
const context = {}
const markup = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>${title}</title>
<meta name="keywords" content="portfolio, software developer portfolio, anshuman portfolio, hackbotone, full stack application, web application development, react application, mern application, mern stack application, portfolio design, portfolio tutorials"/>
<meta name="description" content="${description}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@anspattnaik">
<meta name="twitter:title" content="${title}">
<meta name="twitter:description" content="${description}">
<meta name="twitter:creator" content="@anspattnaik">
<meta name="twitter:image" content="${thumb}">
<meta property="og:title" content="${title}" />
<meta property="og:description" content="${description}">
<meta property="og:type" content="article" />
<meta property="og:url" content="${link}" />
<meta property="og:image" content="${thumb}" />
<meta property="og:site_name" content="${title}" />
<link rel="canonical" href="${link}"/>
<link rel="shortcut icon" type="image/png" href="${favicon}"/>
<link rel="apple-touch-icon-precomposed" href="${thumb}"/>
</head>
<body>
<div id="root">${markup}</div>
<script src="client_bundle.js"></script>
</body>
</html>`;
res.send(html);
})