Hi, I’m trying to build something with flow router and to leverage server side rendering. Since I use react, I removed the blaze html package and I need to inject some meta tags in the head. Here’s how I proceed :
/**
* Listen to every messages recieved
*/
WebApp.connectHandlers.use( function ( req, res, next ) {
/* If the requested url is not a web page, stops here */
if ( /\/favicon/.test( req.url ) || req.url === '/robots.txt' || req.url === '/app.manifest' || !/html/.test( req.headers.accept ) )
return next();
/* The tags to inject, starts with the basic elements */
let injection = `<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>`;
res.dataToInject = injection;
return next();
} );
/**
* Overwrite the basic write method
*/
let originalWrite = http.OutgoingMessage.prototype.write;
http.OutgoingMessage.prototype.write = function ( chunk, encoding ) {
/* If this is a buffer, convert it to string */
if ( this.dataToInject ) {
chunk = chunk.toString().replace( '<head>', '<head>' + this.dataToInject );
}
originalWrite.call( this, chunk, encoding );
};
Basically, I just check that the request is for a page and I inject some html.
This would be a great place to inject some tags like title
, and some meta like description, facebook open graph and stuff like that.
With req.url, I get can the url. But how do I break it down into the route name and parametters ? I could go with a regex, but there has to be something in FlowRouter to do that, no ?
And maybe this is not the best way to add seo data in my pages. Anyone has a better idea ?