Skip to content

feat: add support for headers config #200

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

Merged
merged 10 commits into from
May 28, 2025

Conversation

serhalp
Copy link
Member

@serhalp serhalp commented May 22, 2025

Summary

This adds support for static response headers configuration: https://docs.netlify.com/routing/headers/.

This is largely ported from analogous code in Netlify CLI. As noted here (private link), this does avoid porting over a bug I noticed.

@serhalp serhalp force-pushed the serhalp/frb-1835-support-headers-config branch 3 times, most recently from 0e08454 to ff08f9a Compare May 26, 2025 14:57
@serhalp serhalp changed the title feat(wip): add support for headers config feat: add support for headers config May 26, 2025
@serhalp serhalp force-pushed the serhalp/frb-1835-support-headers-config branch 2 times, most recently from 8d6189e to ccf12b1 Compare May 26, 2025 22:44
"description": "TypeScript implementation of Netlify's headers engine",
"type": "module",
"engines": {
"node": "^18.14.0 || >=20"
Copy link
Member Author

Choose a reason for hiding this comment

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

See also #210

@serhalp serhalp force-pushed the serhalp/frb-1835-support-headers-config branch 2 times, most recently from b0d4959 to b881f9f Compare May 26, 2025 22:59
@serhalp serhalp marked this pull request as ready for review May 26, 2025 23:04
@serhalp serhalp force-pushed the serhalp/frb-1835-support-headers-config branch from b881f9f to 1f877fe Compare May 26, 2025 23:15
@serhalp serhalp force-pushed the serhalp/frb-1835-support-headers-config branch from 1f877fe to 4db3200 Compare May 26, 2025 23:16
@serhalp serhalp requested a review from eduardoboucas May 26, 2025 23:18
@@ -57,6 +57,7 @@
"@netlify/config": "^23.0.4",
"@netlify/dev-utils": "2.2.0",
"@netlify/functions": "3.1.9",
"@netlify/headers": "^0.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

I think we want to pin an exact version here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make that change for internal consistency, but what are we solving for here and how do we plan to enforce it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

When referencing packages from the monorepo, we always know the exact version we want to use. We're not enforcing that anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bleh, I wish npm workspaces had better functionality here. With pnpm or yarn we would just use workspace:*. 🤷🏼

const matchingHeaderRules = headersForPath(headerRules, new URL(request.url).pathname)

for (const [key, value] of Object.entries(matchingHeaderRules)) {
response.headers.set(key, value)
Copy link
Member

Choose a reason for hiding this comment

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

This won't work for any responses with immutable headers. For example:

> Response.redirect("https://example.com").headers.set("X-Foo", "bar")
Uncaught TypeError: immutable
    at _Headers.set (node:internal/deps/undici/undici:8609:17)

See https://github.com/netlify/edge-functions-bootstrap/blob/a380d9615394697bcc51f48eed1bfac41d2f1b18/src/bootstrap/headers.ts#L105-L118.

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting. Wouldn't this never apply here though? We only ever apply header rules to static files.

Are you suggesting we implement the guard as a failsafe?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, good point. It wouldn't hurt to have it as a failsafe, but not critical.

@serhalp serhalp requested review from a team as code owners May 27, 2025 18:52
@serhalp serhalp requested a review from eduardoboucas May 27, 2025 19:00
Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

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

LGTM! The only thing missing is adding the new package to the release flow:

# Publishing packages in topological order, as defined in `package.json`.

@serhalp serhalp requested a review from eduardoboucas May 28, 2025 13:48
@serhalp serhalp enabled auto-merge (squash) May 28, 2025 13:48
@serhalp serhalp merged commit dca313e into main May 28, 2025
23 checks passed
@serhalp serhalp deleted the serhalp/frb-1835-support-headers-config branch May 28, 2025 13:49
@pieh pieh mentioned this pull request Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants