Skip to content

Commit

Permalink
feat: add context.deploy to edge functions context (#5909)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Jul 31, 2023
1 parent 3850c33 commit df9ef26
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/edge-functions/bootstrap.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env } from 'process'

const latestBootstrapURL = 'https://64ae60d920fd0f000865bcfc--edge.netlify.com/bootstrap/index-combined.ts'
const latestBootstrapURL = 'https://64c264287e9cbb0008621df3--edge.netlify.com/bootstrap/index-combined.ts'

export const getBootstrapURL = () => env.NETLIFY_EDGE_BOOTSTRAP || latestBootstrapURL
1 change: 1 addition & 0 deletions src/lib/edge-functions/headers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Buffer } from 'buffer'

export const headers = {
DeployID: 'x-nf-deploy-id',
FeatureFlags: 'x-nf-feature-flags',
ForwardedHost: 'x-forwarded-host',
Functions: 'x-nf-edge-functions',
Expand Down
1 change: 1 addition & 0 deletions src/lib/edge-functions/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const initializeProxy = async ({

// Setting header with geolocation and site info.
req.headers[headers.Geo] = Buffer.from(JSON.stringify(geoLocation)).toString('base64')
req.headers[headers.DeployID] = '0'
req.headers[headers.Site] = createSiteInfoHeader(siteInfo)
req.headers[headers.Account] = createAccountInfoHeader({ id: accountId })

Expand Down
15 changes: 11 additions & 4 deletions tests/integration/100.command.dev.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ test('Serves an Edge Function with caching', async (t) => {
})
})

test('Serves an Edge Function that includes context with site information', async (t) => {
await withSiteBuilder('site-with-edge-function-printing-site-info', async (builder) => {
test('Serves an Edge Function that includes context with site and deploy information', async (t) => {
await withSiteBuilder('site-with-edge-function-printing-site-deploy-info', async (builder) => {
const publicDir = 'public'
builder
.withNetlifyToml({
Expand All @@ -339,7 +339,11 @@ test('Serves an Edge Function that includes context with site information', asyn
},
})
.withEdgeFunction({
handler: async (_, context) => new Response(JSON.stringify(context.site)),
handler: async (_, context) => {
const { deploy, site } = context

return new Response(JSON.stringify({ deploy, site }))
},
name: 'siteContext',
})

Expand Down Expand Up @@ -376,7 +380,10 @@ test('Serves an Edge Function that includes context with site information', asyn
const response = await got(`${server.url}`)

t.is(response.statusCode, 200)
t.is(response.body, '{"id":"site_id","name":"site-name","url":"site-url"}')
t.deepEqual(JSON.parse(response.body), {
deploy: { id: '0' },
site: { id: 'site_id', name: 'site-name', url: 'site-url' },
})
},
)
})
Expand Down

2 comments on commit df9ef26

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,303
  • Package size: 271 MB

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,303
  • Package size: 271 MB

Please sign in to comment.