SEO for dynamically generated content in SPA (single page application) using node.js + express.js on server side

Ad

My client side code is a single page app (written in knockout.js) with its own routing system so when google crawler bot will try to access links (that have nothing to do with requesting new page from back end BUT just a part of client side routing) it will ask server (node.js + express.js) to serve page (for example 'mywebsite/about') and of course server will return 404 because it unawares of client routing system. Here is my current server code:

router.get('*', function(req, res, next) {
    res.sendFile(path.resolve('../dist/index.html'));
});

My idea is to define the same routing structure as in a client and pass routs for client routing system in search parameter:

router.get('/about', function(req, res, next) {
    res.sendFile(path.resolve('../dist/index.html?tab=about'));
});

then in client side I can catch it in javascript and pick correct route. Here off course I have another problem - as I understand google bot doesn't run javascript.. but here I can use prerender.io middleware I guess.

1) Is it a right way to go with single page apps with generated content and SEO?

2) How to pass search parameter from express.js?

Ad

Answer

Ad

Use prerender.io as first middleware of your pipe:

app.use(require('prerender-node').set('prerenderToken', 'YOUR_TOKEN')); app.get('*', (req, res) => res.render('index.html'));

Ad
source: stackoverflow.com
Ad