Skip to content

feat: add Next 13 request header mutation to middleware #1866

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 15 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
21 changes: 21 additions & 0 deletions .github/workflows/test-deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Deno tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Git Checkout Deno Module
uses: actions/checkout@v2
- name: Use Deno Version ${{ matrix.deno-version }}
uses: denolib/setup-deno@v2
with:
deno-version: vx.x.x
- name: Test Deno
run: deno test packages/runtime/src/templates/edge-shared/
18 changes: 12 additions & 6 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export async function middleware(req: NextRequest) {
let response
const { pathname } = req.nextUrl

if (pathname.startsWith('/api/hello')) {
// Next 13 request header mutation functionality
const headers = new Headers(req.headers)

headers.set('x-hello', 'world')
return NextResponse.next({
request: {
headers
}
})
}

const request = new MiddlewareRequest(req)
if (pathname.startsWith('/static')) {
// Unlike NextResponse.next(), this actually sends the request to the origin
Expand All @@ -24,12 +36,6 @@ export async function middleware(req: NextRequest) {
return res
}

if (pathname.startsWith('/api/hello')) {
// Add a header to the request
req.headers.set('x-hello', 'world')
return request.next()
}

if (pathname.startsWith('/api/geo')) {
req.headers.set('x-geo-country', req.geo.country)
req.headers.set('x-geo-region', req.geo.region)
Expand Down
273 changes: 273 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"postinstall": "run-s build install-husky",
"install-husky": "if-env CI=1 || husky install node_modules/@netlify/eslint-config-node/.husky",
"test": "run-s build:demo test:jest",
"test:deno": "deno test packages/runtime/src/templates/edge-shared/",
"test:next": "jest -c test/e2e/jest.config.js",
"test:next:disabled": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.disabled.js",
"test:next:all": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.all.js",
Expand Down Expand Up @@ -101,7 +102,8 @@
"**/test/**/*.spec.ts",
"!**/test/e2e/**",
"!**/test/fixtures/**",
"!**/test/sample/**"
"!**/test/sample/**",
"!**/test/templates/edge-shared/**"
],
"transform": {
"\\.[jt]sx?$": "babel-jest"
Expand Down
Loading