-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (25 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const {parse} = require('url');
const polyfill = require('polyfill-service');
module.exports = async (req, res) => {
const {query: {ua, minify = true}} = parse(req.url, true);
const script = await polyfill.getPolyfillString({
// Allow the user to override the user agent through the querystring,
// otherwise fallback to the received header.
uaString: ua || req.headers['user-agent'],
// Use some sensible modern features.
features: {es6: {}, 'default-3.6': {}},
// Pass the minify option from the querystring through.
minify: JSON.parse(minify) === true,
// Just return all polyfills when the user agent is not recognized.
unknown: 'polyfill'
});
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/javascript;charset=utf-8');
res.setHeader('Content-Length', script.length);
if (process.env.NODE_ENV === 'production') {
// Cache all front-end assets for at least a year.
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
res.write(script);
res.end();
};