Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context.deploy to edge functions context #5909

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the value we use for the DEPLOY_ID environment variable in Netlify Dev.

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