-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (54 loc) · 2.41 KB
/
index.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
/**
* This Cloudflare reverse proxy serves JSON-LD for resources on the openactive.io
* domain when the 'Accept' header of a request contains 'application/ld+json'.
* This is required to ensure compliance with JSON-LD.
*
* Changes to this script must be made via https://github.com/openactive/cloudflare-reverse-proxy/
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const jsonLdUrlMap = {
'/': '/ns/oa.jsonld',
'/activity-list': '/activity-list/activity-list.jsonld',
'/accessibility-support': '/accessibility-support/accessibility-support.jsonld',
'/participant-condition-list': '/participant-condition-list/participant-condition-list.jsonld',
'/ns-beta': '/ns-beta/beta.jsonld',
'/test-interface': '/test-interface/test-interface.jsonld',
'/ns-extension': '/ns-extension/extension.jsonld',
'/facility-types': '/facility-types/facility-types.jsonld',
'/facility-attribute-list': '/facility-attribute-list/facility-attribute-list.jsonld',
'/place-types': '/place-types/place-types.jsonld',
'/conformance-certification': '/conformance-certification/conformance-certification.jsonld',
'/stripe-extension': '/stripe-extension/stripe-extension.jsonld'
}
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
// Make the headers mutable by re-constructing the Request.
let url = new URL(request.url)
let hasJsonLdAcceptHeader = /(ld\+)?json/.test(request.headers.get('Accept'))
// If JSON-LD has been required
if (hasJsonLdAcceptHeader && jsonLdUrlMap[url.pathname]) {
// Construct JSON-LD URL for the path specified
const targetUrl = 'https://openactive.io' + jsonLdUrlMap[url.pathname];
// Fetch JSON-LD
let response = await fetch(targetUrl, request)
// Recreate the response so we can modify the headers
response = new Response(response.body, response)
// Set CORS headers
response.headers.set('Access-Control-Allow-Origin', '*')
return response
// If JSON-LD has not been requested, override the 301 redirect
// of GitHub pages with a 302 to ensure browsers don't incorrectly
// cache the redirect without respecting the varying Accept header
} else if (!/\/$/.test(url.pathname)) {
url.pathname = url.pathname + '/'
return Response.redirect(url, 302)
} else if (url.pathname === '/') {
return Response.redirect('https://www.openactive.io/', 302)
}
return fetch(request)
}