2019-02-16 23:30:34 +01:00
|
|
|
// create the now.json file
|
|
|
|
// Unfortunately this has to be re-run periodically, as AFAICT there is no way to
|
|
|
|
// give Zeit a script and tell them to run that, instead of using a static now.json file.
|
|
|
|
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
import { routes } from '../__sapper__/service-worker'
|
|
|
|
import cloneDeep from 'lodash-es/cloneDeep'
|
|
|
|
import inlineScriptChecksum from '../src/inline-script/checksum'
|
|
|
|
import { sapperInlineScriptChecksums } from '../src/server/sapperInlineScriptChecksums'
|
|
|
|
|
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
|
|
|
const JSON_TEMPLATE = {
|
2019-08-03 22:49:37 +02:00
|
|
|
version: 2,
|
|
|
|
env: {
|
|
|
|
NODE_ENV: 'production'
|
2019-02-16 23:30:34 +01:00
|
|
|
},
|
2019-08-03 22:49:37 +02:00
|
|
|
builds: [
|
2019-02-16 23:30:34 +01:00
|
|
|
{
|
2019-08-03 22:49:37 +02:00
|
|
|
src: 'package.json',
|
|
|
|
use: '@now/static-build',
|
|
|
|
config: {
|
|
|
|
distDir: '__sapper__/export'
|
2019-02-16 23:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2019-08-03 22:49:37 +02:00
|
|
|
routes: [
|
2019-02-16 23:30:34 +01:00
|
|
|
{
|
2019-08-03 22:49:37 +02:00
|
|
|
src: '^/service-worker\\.js$',
|
|
|
|
headers: {
|
2019-02-16 23:30:34 +01:00
|
|
|
'cache-control': 'public,max-age=0'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-08-03 22:49:37 +02:00
|
|
|
src: '^/(report\\.html|stats\\.json)$',
|
|
|
|
headers: {
|
2019-02-16 23:30:34 +01:00
|
|
|
'cache-control': 'public,max-age=3600'
|
|
|
|
},
|
2019-08-03 22:49:37 +02:00
|
|
|
dest: 'client/$1'
|
2019-02-16 23:30:34 +01:00
|
|
|
},
|
|
|
|
{
|
2019-10-15 15:52:27 +02:00
|
|
|
src: '^/client/.*\\.(js|css|map|LICENSE)$',
|
2019-08-03 22:49:37 +02:00
|
|
|
headers: {
|
2019-02-16 23:30:34 +01:00
|
|
|
'cache-control': 'public,max-age=31536000,immutable'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-11-28 19:26:01 +01:00
|
|
|
src: '^/.*\\.(png|css|json|svg|jpe?g|map|txt|gz|webapp)$',
|
2019-08-03 22:49:37 +02:00
|
|
|
headers: {
|
2019-02-16 23:30:34 +01:00
|
|
|
'cache-control': 'public,max-age=3600'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const HTML_HEADERS = {
|
|
|
|
'cache-control': 'public,max-age=3600',
|
2019-08-20 04:08:59 +02:00
|
|
|
'content-security-policy': 'script-src \'self\' ' +
|
2019-02-16 23:30:34 +01:00
|
|
|
`${[inlineScriptChecksum].concat(sapperInlineScriptChecksums).map(_ => `'sha256-${_}'`).join(' ')}; ` +
|
2019-08-20 04:08:59 +02:00
|
|
|
'worker-src \'self\'; style-src \'self\' \'unsafe-inline\'; frame-src \'none\'; object-src \'none\'; manifest-src \'self\'',
|
2019-02-16 23:30:34 +01:00
|
|
|
'referrer-policy': 'no-referrer',
|
|
|
|
'strict-transport-security': 'max-age=15552000; includeSubDomains',
|
|
|
|
'x-content-type-options': 'nosniff',
|
|
|
|
'x-download-options': 'noopen',
|
|
|
|
'x-frame-options': 'SAMEORIGIN',
|
|
|
|
'x-xss-protection': '1; mode=block'
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
2019-08-03 22:49:37 +02:00
|
|
|
const json = cloneDeep(JSON_TEMPLATE)
|
2019-02-16 23:30:34 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
const exportDir = path.resolve(__dirname, '../__sapper__/export')
|
2019-02-16 23:30:34 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
for (const { pattern } of routes) {
|
|
|
|
const route = {
|
2019-02-16 23:30:34 +01:00
|
|
|
src: pattern.source,
|
|
|
|
headers: cloneDeep(HTML_HEADERS)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all the regexy stuff in the regex
|
2019-08-03 22:49:37 +02:00
|
|
|
const filename = pattern.source.replace(/^\^\\\//, '').replace(/(\\\/)?\?\$$/, '').replace(/\\\//g, '/')
|
2019-02-16 23:30:34 +01:00
|
|
|
|
|
|
|
// try two different possible paths
|
2019-08-03 22:49:37 +02:00
|
|
|
const filePath = [
|
2019-02-16 23:30:34 +01:00
|
|
|
`${filename}.html`,
|
|
|
|
path.join(filename, 'index.html')
|
|
|
|
].map(_ => path.resolve(exportDir, _)).find(_ => fs.existsSync(_))
|
|
|
|
|
|
|
|
if (!filePath) { // dynamic route, e.g. /accounts/<accountId/
|
|
|
|
// serve calls to dynamic routes via the generic "service worker" index.html,
|
|
|
|
// since we can't generate the dynamic content using Zeit's static server
|
|
|
|
route.dest = 'service-worker-index.html'
|
|
|
|
}
|
|
|
|
json.routes.push(route)
|
|
|
|
}
|
|
|
|
|
|
|
|
// push a generic route to handle everything else
|
|
|
|
json.routes.push({
|
|
|
|
src: '^/(.*)',
|
|
|
|
headers: cloneDeep(HTML_HEADERS)
|
|
|
|
})
|
|
|
|
|
|
|
|
await writeFile(path.resolve(__dirname, '../now.json'), JSON.stringify(json, null, ' '), 'utf8')
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|