2020-08-26 00:45:53 +01:00
|
|
|
// create the vercel.json file
|
2019-02-16 22:30:34 +00:00
|
|
|
// Unfortunately this has to be re-run periodically, as AFAICT there is no way to
|
2020-08-26 00:45:53 +01:00
|
|
|
// give Zeit a script and tell them to run that, instead of using a static vercel.json file.
|
2019-02-16 22:30:34 +00:00
|
|
|
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import { promisify } from 'util'
|
2021-07-05 04:19:04 +01:00
|
|
|
import { routes } from '../__sapper__/service-worker.js'
|
|
|
|
import { cloneDeep } from '../src/routes/_utils/lodash-lite.js'
|
|
|
|
import inlineScriptChecksum from '../src/inline-script/checksum.js'
|
|
|
|
import { sapperInlineScriptChecksums } from '../src/server/sapperInlineScriptChecksums.js'
|
2019-02-16 22:30:34 +00:00
|
|
|
|
2021-07-05 04:19:04 +01:00
|
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
2019-02-16 22:30:34 +00:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
|
|
|
const JSON_TEMPLATE = {
|
2019-08-03 21:49:37 +01:00
|
|
|
version: 2,
|
|
|
|
env: {
|
|
|
|
NODE_ENV: 'production'
|
2019-02-16 22:30:34 +00:00
|
|
|
},
|
2021-02-14 22:02:02 +00:00
|
|
|
github: {
|
|
|
|
silent: true
|
|
|
|
},
|
2019-08-03 21:49:37 +01:00
|
|
|
builds: [
|
2019-02-16 22:30:34 +00:00
|
|
|
{
|
2019-08-03 21:49:37 +01:00
|
|
|
src: 'package.json',
|
|
|
|
use: '@now/static-build',
|
|
|
|
config: {
|
|
|
|
distDir: '__sapper__/export'
|
2019-02-16 22:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2019-08-03 21:49:37 +01:00
|
|
|
routes: [
|
2019-02-16 22:30:34 +00:00
|
|
|
{
|
2019-08-03 21:49:37 +01:00
|
|
|
src: '^/service-worker\\.js$',
|
|
|
|
headers: {
|
2019-02-16 22:30:34 +00:00
|
|
|
'cache-control': 'public,max-age=0'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-08-03 21:49:37 +01:00
|
|
|
src: '^/(report\\.html|stats\\.json)$',
|
|
|
|
headers: {
|
2019-02-16 22:30:34 +00:00
|
|
|
'cache-control': 'public,max-age=3600'
|
|
|
|
},
|
2019-08-03 21:49:37 +01:00
|
|
|
dest: 'client/$1'
|
2019-02-16 22:30:34 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-20 22:31:15 +01:00
|
|
|
src: '^/client/.*\\.(js|css|map|LICENSE|txt)$',
|
2019-08-03 21:49:37 +01:00
|
|
|
headers: {
|
2019-02-16 22:30:34 +00:00
|
|
|
'cache-control': 'public,max-age=31536000,immutable'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2022-03-28 04:59:02 +01:00
|
|
|
src: '^/.*\\.(png|css|json|svg|jpe?g|map|txt|gz|webapp|woff|woff2)$',
|
2019-08-03 21:49:37 +01:00
|
|
|
headers: {
|
2019-02-16 22:30:34 +00:00
|
|
|
'cache-control': 'public,max-age=3600'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-05-03 02:09:07 +01:00
|
|
|
const SCRIPT_CHECKSUMS = [inlineScriptChecksum]
|
|
|
|
.concat(sapperInlineScriptChecksums)
|
|
|
|
.map(_ => `'sha256-${_}'`)
|
|
|
|
.join(' ')
|
|
|
|
|
2021-04-17 21:34:46 +01:00
|
|
|
const PERMISSIONS_POLICY = 'sync-xhr=(),document-domain=(),interest-cohort=()'
|
2021-03-06 23:21:26 +00:00
|
|
|
|
2019-02-16 22:30:34 +00:00
|
|
|
const HTML_HEADERS = {
|
|
|
|
'cache-control': 'public,max-age=3600',
|
2020-05-03 02:09:07 +01:00
|
|
|
'content-security-policy': [
|
|
|
|
"default-src 'self'",
|
|
|
|
`script-src 'self' ${SCRIPT_CHECKSUMS}`,
|
|
|
|
"worker-src 'self'",
|
|
|
|
"style-src 'self' 'unsafe-inline'",
|
|
|
|
"img-src 'self' * data: blob:",
|
|
|
|
"media-src 'self' *",
|
|
|
|
"connect-src 'self' * data: blob:",
|
|
|
|
"frame-src 'none'",
|
|
|
|
"frame-ancestors 'none'",
|
|
|
|
"object-src 'none'",
|
|
|
|
"manifest-src 'self'",
|
2021-03-14 17:20:23 +00:00
|
|
|
"form-action 'self'", // we need form-action for the Web Share Target API
|
2020-05-03 02:09:07 +01:00
|
|
|
"base-uri 'self'"
|
|
|
|
].join(';'),
|
2019-02-16 22:30:34 +00:00
|
|
|
'referrer-policy': 'no-referrer',
|
|
|
|
'strict-transport-security': 'max-age=15552000; includeSubDomains',
|
2021-03-06 23:21:26 +00:00
|
|
|
'permissions-policy': PERMISSIONS_POLICY,
|
2019-02-16 22:30:34 +00:00
|
|
|
'x-content-type-options': 'nosniff',
|
|
|
|
'x-download-options': 'noopen',
|
2020-05-03 02:09:07 +01:00
|
|
|
'x-frame-options': 'DENY',
|
2021-03-09 16:08:19 +00:00
|
|
|
'x-xss-protection': '1; mode=block',
|
|
|
|
'cross-origin-opener-policy': 'same-origin'
|
2019-02-16 22:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
2019-08-03 21:49:37 +01:00
|
|
|
const json = cloneDeep(JSON_TEMPLATE)
|
2019-02-16 22:30:34 +00:00
|
|
|
|
2019-08-03 21:49:37 +01:00
|
|
|
const exportDir = path.resolve(__dirname, '../__sapper__/export')
|
2019-02-16 22:30:34 +00:00
|
|
|
|
2019-08-03 21:49:37 +01:00
|
|
|
for (const { pattern } of routes) {
|
|
|
|
const route = {
|
2019-02-16 22:30:34 +00:00
|
|
|
src: pattern.source,
|
|
|
|
headers: cloneDeep(HTML_HEADERS)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all the regexy stuff in the regex
|
2019-08-03 21:49:37 +01:00
|
|
|
const filename = pattern.source.replace(/^\^\\\//, '').replace(/(\\\/)?\?\$$/, '').replace(/\\\//g, '/')
|
2019-02-16 22:30:34 +00:00
|
|
|
|
|
|
|
// try two different possible paths
|
2019-08-03 21:49:37 +01:00
|
|
|
const filePath = [
|
2019-02-16 22:30:34 +00: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)
|
|
|
|
})
|
|
|
|
|
2020-08-26 00:45:53 +01:00
|
|
|
await writeFile(path.resolve(__dirname, '../vercel.json'), JSON.stringify(json, null, ' '), 'utf8')
|
2019-02-16 22:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|