Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created March 22, 2020 22:33
Show Gist options
  • Save BetterProgramming/8005e7591e63c33da1e0c74f89ac9008 to your computer and use it in GitHub Desktop.
Save BetterProgramming/8005e7591e63c33da1e0c74f89ac9008 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
...
//in your server.js (or routes file), include the following above your api routes and the main route that hands users your index.html file
nonSPArouter = express.Router();
//in a general route, you check all incoming traffic for your preferred crawlers through the user-agent
app.use(function(req,res,next) {
var ua = req.headers['user-agent'];
if (/^(facebookexternalhit|twitterbot)/gi.test(ua)) {
nonSPArouter(req,res,next);
} else {
next();
}
});
...
//this should be your very last route - it will hand all non-crawler users your index.html file (that includes the links to your app files - the vendor.js, app.js etc. files deployed on cloud distribution system such as Amazon S3 + cloudfront)
// MAIN CATCHALL ROUTE ---------------
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment