Skip to content

Commit

Permalink
fix(worker): clone proxy responses to allow modifying them
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaliske committed Jul 24, 2023
1 parent 868de98 commit 0608c7a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ app.use('*', cache())
app.get('*', headers())

// routes
app.post('/api/event', event())
app.post('/api/contact', contact())
app.post('/api/event', event())
app.get('/insights.js', script())
app.get('*', site())

Expand Down
14 changes: 10 additions & 4 deletions workers/routes/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const baseUrl = 'https://analytics.pascaliske.dev'
export const event: () => Handler = () => {
const log = logger('analytics')

return (context: Context<Environment>) => {
return async (context: Context<Environment>) => {
log(`Proxying analytics events for ${context.env.ENVIRONMENT}`)

// clone request
Expand All @@ -17,17 +17,23 @@ export const event: () => Handler = () => {
request.headers.delete('cookie')

// forward request
return fetch(`${baseUrl}/api/event`, request)
const response = await fetch(`${baseUrl}/api/event`, request)

// clone response
return new Response(response.body, response)
}
}

export const script: () => Handler = () => {
const log = logger('analytics')

return (context: Context<Environment>) => {
return async (context: Context<Environment>) => {
log(`Proxying analytics script for ${context.env.ENVIRONMENT}`)

// forward request
return fetch(`${baseUrl}/js/script.tagged-events.outbound-links.js`)
const response = await fetch(`${baseUrl}/js/script.tagged-events.outbound-links.js`)

// clone response
return new Response(response.body, response)
}
}

0 comments on commit 0608c7a

Please sign in to comment.