2018-11-07 23:37:06 +00:00
|
|
|
import { ModernBrowsers } from '@nuxt/common'
|
|
|
|
import { matchesUA } from 'browserslist-useragent'
|
|
|
|
|
|
|
|
const modernBrowsers = Object.keys(ModernBrowsers)
|
|
|
|
.map(browser => `${browser} >= ${ModernBrowsers[browser]}`)
|
|
|
|
|
|
|
|
const isModernBrowser = (ua) => {
|
|
|
|
return Boolean(ua) && matchesUA(ua, {
|
|
|
|
allowHigherVersions: true,
|
|
|
|
browsers: modernBrowsers
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (req, res, next) {
|
|
|
|
const { socket = {}, headers } = req
|
2018-11-07 23:48:22 +00:00
|
|
|
if (socket.modernMode === undefined) {
|
2018-11-07 23:37:06 +00:00
|
|
|
const ua = headers && headers['user-agent']
|
2018-11-07 23:48:22 +00:00
|
|
|
socket.modernMode = isModernBrowser(ua)
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|
2018-11-07 23:48:22 +00:00
|
|
|
req.modernMode = socket.modernMode
|
2018-11-07 23:37:06 +00:00
|
|
|
next()
|
|
|
|
}
|