Skip to content

Commit

Permalink
fix(maps): enable CORS for style json response (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 authored Jan 21, 2025
1 parent 7639b8d commit 58d3b27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/fastify-plugins/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export async function plugin(fastify, opts) {
if (resp && resp.status === 200) {
return reply
.headers({
'access-control-allow-origin': '*',
'cache-control': 'no-cache',
})
.redirect(url.toString())
Expand Down
13 changes: 0 additions & 13 deletions src/fastify-plugins/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,3 @@ export async function getFastifyServerAddress(server, { timeout } = {}) {

return 'http://' + addr
}

/**
* @param {Readonly<Date>} lastModified
*/
export function createStyleJsonResponseHeaders(lastModified) {
return {
'Cache-Control': 'max-age=' + 5 * 60, // 5 minutes
'Access-Control-Allow-Headers':
'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since',
'Access-Control-Allow-Origin': '*',
'Last-Modified': lastModified.toUTCString(),
}
}
18 changes: 18 additions & 0 deletions test/fastify-plugins/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ test('/style.json resolves style.json of custom map when available', async (t) =
'style response disables caching'
)

assert.equal(
styleResponse.headers['access-control-allow-origin'],
'*',
'style response enables CORS'
)

const location = styleResponse.headers['location']

assert.equal(
Expand Down Expand Up @@ -154,6 +160,12 @@ test('/style.json resolves online style.json when custom is not available', asyn
'style response disables caching'
)

assert.equal(
response.headers['access-control-allow-origin'],
'*',
'style response enables CORS'
)

assert.equal(
response.headers['location'],
DEFAULT_ONLINE_STYLE_URL,
Expand Down Expand Up @@ -199,6 +211,12 @@ test('/style.json resolves style.json of fallback map when custom and online are
'style response disables caching'
)

assert.equal(
styleResponse.headers['access-control-allow-origin'],
'*',
'style response enables CORS'
)

const location = styleResponse.headers['location']

assert.equal(
Expand Down

0 comments on commit 58d3b27

Please sign in to comment.