-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
109 lines (94 loc) · 3.17 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if (!+process.env.HEROKU) require('dotenv/config')
const jalla = require('jalla')
const body = require('koa-body')
const dedent = require('dedent')
const { get, post } = require('koa-route')
const compose = require('koa-compose')
const Prismic = require('prismic-javascript')
const purge = require('./lib/purge')
const imageproxy = require('./lib/media')
const { resolve } = require('./components/base')
const PRISMIC_ENDPOINT = 'https://thenewdivision.cdn.prismic.io/api/v2'
const REDIRECTS = [
['/about', 'company'],
['/thekidsshow', 'global-goals-kids-show'],
['/varldens-plan', 'global-goals-kids-show'],
['/undp', 'film-for-equality'],
['/sei', 'interconnected-agenda'],
['/icc', '/'],
['/ghg-calculation', 'offer'],
['/sustainordic', 'the-nordic-report'],
['/boverket', 'branding-for-natures-living-infrastructure'],
['/climate_action', '/'],
['/hallbarhetsfokus-2019', 'sustainability-focus-2019'],
['/communicating-the-goals', 'communicating-the-global-goals']
]
const app = jalla('index.js', {
sw: 'sw.js',
serve: process.env.NODE_ENV === 'production'
})
app.use(get('/robots.txt', function (ctx, next) {
ctx.type = 'text/plain'
ctx.body = dedent`
User-agent: *
Disallow: ${process.env.NODE_ENV === 'production' ? '' : '/'}
`
}))
REDIRECTS.map((path) => app.use(get(path[0], async (ctx) => ctx.redirect(path[1]))))
app.use(imageproxy)
app.use(post('/prismic-hook', compose([body(), async function (ctx) {
const secret = ctx.request.body && ctx.request.body.secret
ctx.assert(secret === process.env.PRISMIC_SECRET, 403, 'Secret mismatch')
await new Promise(function (resolve, reject) {
purge(function (err, response) {
if (err) return reject(err)
resolve()
})
})
ctx.type = 'application/json'
ctx.body = JSON.stringify({ success: true })
}])))
app.use(function (ctx, next) {
if (!ctx.accepts('html')) return next()
const previewCookie = ctx.cookies.get(Prismic.previewCookie)
if (previewCookie) {
ctx.state.ref = previewCookie
ctx.set('Cache-Control', 'max-age=0')
} else {
ctx.state.ref = null
}
const allowCache = process.env.NODE_ENV !== 'development'
if (!previewCookie && allowCache && ctx.path !== '/prismic-preview') {
ctx.set('Cache-Control', `s-maxage=${60 * 60 * 24 * 7}, max-age=${60 * 10}`)
}
return next()
})
app.use(get('/prismic-preview', async function (ctx) {
const { token, documentId } = ctx.query
const api = await Prismic.api(PRISMIC_ENDPOINT)
const href = await api.getPreviewResolver(token, documentId).resolve(resolve, '/')
const expires = process.env.NODE_ENV === 'development'
? new Date(Date.now() + (1000 * 60 * 60 * (24 - new Date().getHours())))
: new Date(Date.now() + (1000 * 60 * 30))
ctx.set('Cache-Control', 'no-cache, private, max-age=0')
ctx.cookies.set(Prismic.previewCookie, token, {
expires,
httpOnly: false,
path: '/'
})
ctx.redirect(href)
}))
if (+process.env.HEROKU && process.env.NODE_ENV === 'production') {
purge(['/sw.js'], function (err) {
// if (err) throw err
if (err) console.log(err)
start()
})
} else {
start()
}
// start server
// () -> void
function start () {
app.listen(process.env.PORT || 8080)
}