Skip to content

SERA integration on Node

Kindly follow the below examples to setup the routing of bot traffic to SERA.

  • Change YOUR_TOKEN to the SERA token shared with you
  • Keep the relevant bot user-agent regex condition mentioned below - to control the traffic being sent to SERA
const express = require('express')
const app = express()
const port = 3000


const seraEndpoint = 'https://sera.n7.io';
const seraToken = 'YOUR_TOKEN';
//USE THIS - DURING POC PHASE for bot User-Agent regex :
const seraUserAgentPattern = /(n7TestUserAgent|PTST|GTmetrix)/i;
//USE THIS - ON GOING LIVE ON SERA for bot User-Agent regex :
const seraUserAgentPattern = /(googlebot|google page speed|chrome-lighthouse|google-inspectiontool|PTST|GTmetrix)/i;
const seraExludePattern = /(\.ai|\.ashx|\.asmx|\.aspx|\.avi|\.avif|\.bmp|\.css|\.csv|\.dat|\.dmg|\.doc|\.eot|\.exe|\.flv|\.gif|\.ico|\.iso|\.jpeg|\.jpg|\.js|\.json|\.jsp|\.less|\.m4a|\.m4v|\.mov|\.mp3|\.mp4|\.mpeg|\.mpg|\.ogg|\.otf|\.pdf|\.php|\.png|\.ppt|\.psd|\.rar|\.rss|\.svg|\.swf|\.tif|\.torrent|\.ttf|\.txt|\.wav|\.webm|\.webp|\.wmv|\.woff|\.woff2|\.xls|\.xml|\.zip)/i;

const sera = async (req, res, next) => {
    const { "user-agent": userAgent, "accept-encoding": acceptEncoding, accept, referer } = req.headers;

    if (seraUserAgentPattern.test(userAgent) 
        && !seraExludePattern.test(req.url)
        && !req.headers['x-nv-app']
        && !req.headers['x-nv-sera-bypass']) {
        const headers = {
            'x-nv-sera-token': seraToken,
            'user-agent': userAgent || '',
            'accept-encoding': acceptEncoding || '',
            'accept': accept || '',
            'referer': referer || '',
        };
        const remoteUrl = `${seraEndpoint}${req.url}`;
        try {
            const response = await fetch(remoteUrl, { headers });
            if ((response.status === 200 || response.status === 410) && response.data) {
                res.status(response.status).send(response.data);
            } else {
                next();
            }
        } catch (error) {
            console.error('error downloading page:', error.message);
            next();
        }
    } else {
        next();
    }
}

app.use(sera)

app.get('/', (req, res) => {
    res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`example app listening on port ${port}`)
})

Testing and Refinement

  • Keep refining the logic by adjusting user-agent list, and SERA exclusion-patterns as needed.
  • Test thoroughly:
    • WPT and GTMetrix will automatically receive the response from SERA due to the routing rule set above.
      • Steps to validate page using WPT
    • To check the SERA rendered page on browser, you can use some browser extension (like Simple modify headers) to manipulate the browser user-agent.
      If in testing phase, use these user-agent values:
      For desktop:
      n7TestUserAgent
      
      For mobile:
      n7TestUserAgent; Android
      
      If SERA is live, you can use these actual bot user-agent values:
      For desktop:
      Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +<http://www.google.com/bot.html)> Chrome/W.X.Y.Z Safari/537.36
      
      For mobile:
      Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +<http://www.google.com/bot.html)>
      
  • On go-live day, set the user-agent condition regex to actual bot user-agent values. Additionally, ensure that your condition is case-insensitive.