2018-11-26 22:49:47 +00:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import consola from 'consola'
|
2018-12-10 21:35:04 +00:00
|
|
|
import UAParser from 'ua-parser-js'
|
|
|
|
import semver from 'semver'
|
2018-11-07 23:37:06 +00:00
|
|
|
|
2018-12-22 21:05:13 +00:00
|
|
|
import ModernBrowsers from './modern-browsers'
|
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
const modernBrowsers = Object.keys(ModernBrowsers)
|
2018-12-10 21:35:04 +00:00
|
|
|
.reduce((allBrowsers, browser) => {
|
|
|
|
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
|
|
|
|
return allBrowsers
|
|
|
|
}, {})
|
2018-11-07 23:37:06 +00:00
|
|
|
|
|
|
|
const isModernBrowser = (ua) => {
|
2018-12-10 21:35:04 +00:00
|
|
|
if (!ua) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const { browser } = UAParser(ua)
|
|
|
|
const browserVersion = semver.coerce(browser.version)
|
2019-01-02 21:52:09 +00:00
|
|
|
if (!browserVersion) {
|
|
|
|
return false
|
|
|
|
}
|
2018-12-10 21:35:04 +00:00
|
|
|
return modernBrowsers[browser.name] && semver.gte(browserVersion, modernBrowsers[browser.name])
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:49:47 +00:00
|
|
|
let detected = false
|
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
const distinctModernModeOptions = [false, 'client', 'server']
|
|
|
|
|
2018-11-26 22:49:47 +00:00
|
|
|
const detectModernBuild = ({ options, resources }) => {
|
2019-02-08 16:25:11 +00:00
|
|
|
if (detected || distinctModernModeOptions.includes(options.modern)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
detected = true
|
|
|
|
|
|
|
|
if (!resources.modernManifest) {
|
|
|
|
options.modern = false
|
|
|
|
return
|
2018-11-26 22:49:47 +00:00
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
|
|
|
options.modern = options.render.ssr ? 'server' : 'client'
|
|
|
|
consola.info(`Modern bundles are detected. Modern mode (${chalk.green.bold(options.modern)}) is enabled now.`)
|
2018-11-26 22:49:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:26:39 +00:00
|
|
|
const detectModernBrowser = ({ socket = {}, headers }) => {
|
2019-02-08 16:25:11 +00:00
|
|
|
if (socket.isModernBrowser !== undefined) {
|
|
|
|
return
|
2018-12-24 16:26:39 +00:00
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
|
|
|
const ua = headers && headers['user-agent']
|
|
|
|
socket.isModernBrowser = isModernBrowser(ua)
|
2018-12-24 16:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const setModernMode = (req, options) => {
|
|
|
|
const { socket = {} } = req
|
2019-01-17 21:18:29 +00:00
|
|
|
const { isModernBrowser } = socket
|
2018-11-26 22:49:47 +00:00
|
|
|
if (options.modern === 'server') {
|
2018-12-24 16:26:39 +00:00
|
|
|
req.modernMode = isModernBrowser
|
|
|
|
}
|
2018-12-28 13:26:46 +00:00
|
|
|
if (options.dev && !!options.modern) {
|
2018-12-24 16:26:39 +00:00
|
|
|
req.devModernMode = isModernBrowser
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|
2018-11-26 22:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ context }) => (req, res, next) => {
|
|
|
|
detectModernBuild(context)
|
2018-12-24 16:26:39 +00:00
|
|
|
detectModernBrowser(req)
|
|
|
|
setModernMode(req, context.options)
|
2018-11-07 23:37:06 +00:00
|
|
|
next()
|
|
|
|
}
|