SERA integration on Node¶
Kindly follow the below examples to setup the routing of bot traffic to SERA.
Prerequisites¶
- All your page requests must get intercepted by below SERA routing rule
- Please check your routing, caching and cache-variation policies for that
- If caching is enabled, Users and Bots should have a separate cache-variations configured. This is to not serve the SERA's bot-optimized-content to users.
Steps¶
- Change SERA_ENDPOINT to the SERA endpoint shared with you
- 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 = 'SERA_ENDPOINT';
const seraToken = 'YOUR_TOKEN';
const n7AccessKey = 'N7_ACCESS_KEY';
//USE THIS - DURING POC PHASE for bot User-Agent regex :
const seraUserAgentPattern = /(n7TestUserAgent|google page speed|chrome-lighthouse|google-inspectiontool|PTST|GTmetrix)/i;
//USE THIS - ON GOING LIVE ON SERA for bot User-Agent regex :
//const seraUserAgentPattern = /(googlebot|n7TestUserAgent|google page speed|chrome-lighthouse|google-inspectiontool|PTST|GTmetrix)/i;
const seraExludeExtnPattern = /.+\.(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 seraExludeQryPrmPattern = /\?(.*&)*(nsbp)=.*/i;
const seraExludePagePattern = /.*(\/static/|\/_next\/|\/api\/|\/login|\/logout|\/account\/|\/wishlist\/|\/cart\/|\/checkout\/|\/payment\/).*/i;
const sera = async (req, res, next) => {
const { "user-agent": userAgent, "accept-encoding": acceptEncoding, accept, referer } = req.headers;
if (req.method === "GET"
&& seraUserAgentPattern.test(userAgent)
&& !seraExludeExtnPattern.test(req.url)
&& !seraExludeQryPrmPattern.test(req.url)
&& !seraExludePagePattern.test(req.url)
&& !req.headers['x-nv-app']
&& !req.headers['x-nv-sera-bypass']) {
const headers = {
'x-nv-sera-token': seraToken,
'x-nv-access-key': n7AccessKey,
'user-agent': userAgent || '',
'accept-encoding': acceptEncoding || '',
'accept': accept || '',
'referer': referer || '',
};
const remoteUrl = `${seraEndpoint}${req.url.pathname}`; //USE THIS IF YOUR SITEMAP PAGE URLS DO NOT HAVE QUERYSTRING
const remoteUrl = `${seraEndpoint}${req.url}`; //USE THIS IF YOUR SITEMAP PAGE URLS HAVE QUERYSTRING
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 DevTools > Network conditions facility, or some browser extension (like Simple modify headers) to manipulate the browser user-agent. If in testing phase, use these user-agent values: For desktop:
For mobile: If SERA is live, you can use these actual bot user-agent values: For desktop: For mobile:
-
-
On go-live day, set the user-agent condition regex to actual bot user-agent values. Additionally, ensure that your condition is case-insensitive.
-
Whitelist SERA requests at your origin to avoid any issues with indexing. Details are here
-
After go-live, in any dashboards created for monitoring the "actual user traffic" e.g. Google analytics, exclude the requests with user-agent string containing word "Nitrogen SERA" to get correct understanding about user-traffic.