2017-06-08 17:41:22 +00:00
|
|
|
import { join } from 'path'
|
|
|
|
import { readdirSync, existsSync } from 'fs'
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
export const IS_BUNDLED_PAGE = /^bundles[/\\]pages.*\.js$/
|
|
|
|
export const MATCH_ROUTE_NAME = /^bundles[/\\]pages[/\\](.*)\.js$/
|
2017-06-08 17:41:22 +00:00
|
|
|
|
|
|
|
export function getAvailableChunks (dir, dist) {
|
|
|
|
const chunksDir = join(dir, dist, 'chunks')
|
|
|
|
if (!existsSync(chunksDir)) return {}
|
|
|
|
|
|
|
|
const chunksMap = {}
|
|
|
|
const chunkFiles = readdirSync(chunksDir)
|
|
|
|
|
|
|
|
chunkFiles.forEach(filename => {
|
|
|
|
if (/\.js$/.test(filename)) {
|
2017-12-27 18:59:17 +00:00
|
|
|
const chunkName = filename.replace(/-.*/, '')
|
|
|
|
chunksMap[chunkName] = filename
|
2017-06-08 17:41:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return chunksMap
|
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
const internalPrefixes = [
|
|
|
|
/^\/_next\//,
|
|
|
|
/^\/static\//
|
|
|
|
]
|
|
|
|
|
|
|
|
export function isInternalUrl (url) {
|
|
|
|
for (const prefix of internalPrefixes) {
|
|
|
|
if (prefix.test(url)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addCorsSupport (req, res) {
|
|
|
|
if (!req.headers.origin) {
|
|
|
|
return { preflight: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
|
|
|
|
res.setHeader('Access-Control-Request-Method', req.headers.origin)
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', req.headers.origin)
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
return { preflight: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
return { preflight: false }
|
|
|
|
}
|