Skip to content

Commit e6b2fd1

Browse files
authored
Merge branch 'main' into rs/isr-i18n-homepage-revisit
2 parents b9242af + a8123ac commit e6b2fd1

File tree

12 files changed

+192
-53
lines changed

12 files changed

+192
-53
lines changed

.github/workflows/test-deno.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deno tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Git Checkout Deno Module
15+
uses: actions/checkout@v2
16+
- name: Use Deno Version ${{ matrix.deno-version }}
17+
uses: denolib/setup-deno@v2
18+
with:
19+
deno-version: vx.x.x
20+
- name: Test Deno
21+
run: deno test packages/runtime/src/templates/edge-shared/

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"packages/runtime": "4.29.4",
2+
"packages/runtime": "4.30.0",
33
"packages/next": "1.4.3"
44
}

cypress/integration/middleware/enhanced.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
describe('Enhanced middleware', () => {
2-
it('adds request headers', () => {
3-
cy.request('/api/hello').then((response) => {
4-
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
5-
})
6-
})
7-
8-
it('adds request headers to a rewrite', () => {
9-
cy.request('/headers').then((response) => {
10-
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
11-
})
12-
})
13-
142
it('rewrites the response body', () => {
153
cy.visit('/static')
164
cy.get('#message').contains('This was static but has been transformed in')

cypress/integration/middleware/standard.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
describe('Standard middleware', () => {
2+
it('adds request headers', () => {
3+
cy.request('/api/hello').then((response) => {
4+
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
5+
})
6+
})
7+
8+
it('adds request headers to a rewrite', () => {
9+
cy.request('/headers').then((response) => {
10+
expect(response.body).to.have.nested.property('headers.x-hello', 'world')
11+
})
12+
})
13+
214
it('rewrites to internal page', () => {
315
// preview mode is off by default
416
cy.visit('/shows/rewriteme')

demos/middleware/middleware.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ export async function middleware(req: NextRequest) {
88
let response
99
const { pathname } = req.nextUrl
1010

11+
if (pathname.startsWith('/api/hello')) {
12+
// Next 13 request header mutation functionality
13+
const headers = new Headers(req.headers)
14+
15+
headers.set('x-hello', 'world')
16+
return NextResponse.next({
17+
request: {
18+
headers
19+
}
20+
})
21+
}
22+
1123
const request = new MiddlewareRequest(req)
1224
if (pathname.startsWith('/static')) {
1325
// Unlike NextResponse.next(), this actually sends the request to the origin
@@ -24,12 +36,6 @@ export async function middleware(req: NextRequest) {
2436
return res
2537
}
2638

27-
if (pathname.startsWith('/api/hello')) {
28-
// Add a header to the request
29-
req.headers.set('x-hello', 'world')
30-
return request.next()
31-
}
32-
3339
if (pathname.startsWith('/api/geo')) {
3440
req.headers.set('x-geo-country', req.geo.country)
3541
req.headers.set('x-geo-region', req.geo.region)

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"postinstall": "run-s build install-husky",
1919
"install-husky": "if-env CI=1 || husky install node_modules/@netlify/eslint-config-node/.husky",
2020
"test": "run-s build:demo test:jest",
21+
"test:deno": "deno test packages/runtime/src/templates/edge-shared/",
2122
"test:next": "jest -c test/e2e/jest.config.js",
2223
"test:next:disabled": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.disabled.js",
2324
"test:next:all": "RUN_SKIPPED_TESTS=1 jest -c test/e2e/jest.config.all.js",
@@ -51,7 +52,7 @@
5152
"@babel/preset-env": "^7.15.8",
5253
"@babel/preset-typescript": "^7.16.0",
5354
"@delucis/if-env": "^1.1.2",
54-
"@netlify/build": "^29.4.0",
55+
"@netlify/build": "^29.4.2",
5556
"@netlify/eslint-config-node": "^7.0.1",
5657
"@testing-library/cypress": "^8.0.1",
5758
"@types/fs-extra": "^9.0.13",
@@ -101,7 +102,8 @@
101102
"**/test/**/*.spec.ts",
102103
"!**/test/e2e/**",
103104
"!**/test/fixtures/**",
104-
"!**/test/sample/**"
105+
"!**/test/sample/**",
106+
"!**/test/templates/edge-shared/**"
105107
],
106108
"transform": {
107109
"\\.[jt]sx?$": "babel-jest"

packages/runtime/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## [4.30.0](https://github.com/netlify/next-runtime/compare/plugin-nextjs-v4.29.4...plugin-nextjs-v4.30.0) (2023-01-09)
4+
5+
6+
### Features
7+
8+
* add Next 13 request header mutation to middleware ([#1866](https://github.com/netlify/next-runtime/issues/1866)) ([5d60191](https://github.com/netlify/next-runtime/commit/5d60191905e9df8ca6cec7dc586f95917906b750))
9+
10+
11+
### Bug Fixes
12+
13+
* **deps:** update dependency @netlify/functions to ^1.4.0 ([#1869](https://github.com/netlify/next-runtime/issues/1869)) ([3960c31](https://github.com/netlify/next-runtime/commit/3960c31f5dc9067eac347f437274f18265ffb589))
14+
* **deps:** update dependency @netlify/ipx to ^1.3.3 ([#1868](https://github.com/netlify/next-runtime/issues/1868)) ([d75d0ff](https://github.com/netlify/next-runtime/commit/d75d0ffafdc7b7623328f5176e6d4cf45185e69e))
15+
* merge Middleware and API route response cookies ([#1870](https://github.com/netlify/next-runtime/issues/1870)) ([878ddd7](https://github.com/netlify/next-runtime/commit/878ddd750d21632d2f4e67d2f6d7724bffcef218))
16+
* serve static files from basePath ([#1850](https://github.com/netlify/next-runtime/issues/1850)) ([d4ff894](https://github.com/netlify/next-runtime/commit/d4ff894e88bed1138fefacb9ce2fcbe62f62f5b0))
17+
318
## [4.29.4](https://github.com/netlify/next-runtime/compare/plugin-nextjs-v4.29.3...plugin-nextjs-v4.29.4) (2022-12-19)
419

520

packages/runtime/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netlify/plugin-nextjs",
3-
"version": "4.29.4",
3+
"version": "4.30.0",
44
"description": "Run Next.js seamlessly on Netlify",
55
"main": "index.js",
66
"files": [
@@ -36,7 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"@delucis/if-env": "^1.1.2",
39-
"@netlify/build": "^29.4.0",
39+
"@netlify/build": "^29.4.2",
4040
"@types/fs-extra": "^9.0.13",
4141
"@types/jest": "^27.4.1",
4242
"@types/merge-stream": "^1.1.2",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
2+
import { beforeEach, describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'
3+
import { updateModifiedHeaders, FetchEventResult } from './utils.ts'
4+
5+
describe('updateModifiedHeaders', () => {
6+
it("does not modify the headers if 'x-middleware-override-headers' is not found", () => {
7+
const mockHeaders = new Headers()
8+
// There shouldn't be a case where x-middleware-override-headers is not set and a header has
9+
// been modified with 'x-middleware-request' added to it, this is more to confirm the test case
10+
mockHeaders.set('x-middleware-request-foo', 'bar')
11+
12+
const mockResponse = {
13+
headers: mockHeaders,
14+
}
15+
16+
const mockRequest = {
17+
headers: new Headers(),
18+
}
19+
20+
updateModifiedHeaders(mockRequest.headers, mockResponse.headers)
21+
22+
assertEquals(mockRequest.headers.get('x-middleware-request-foo'), null)
23+
})
24+
25+
describe("when the 'x-middleware-override-headers' headers is present", () => {
26+
let mockHeaders
27+
let mockRequest: { headers: Headers }
28+
let mockResponse: { headers: Headers }
29+
30+
beforeEach(() => {
31+
mockHeaders = new Headers()
32+
mockHeaders.set('foo', 'bar')
33+
mockHeaders.set('x-middleware-request-hello', 'world')
34+
mockHeaders.set('x-middleware-request-test', '123')
35+
mockHeaders.set('x-middleware-override-headers', 'hello,test')
36+
37+
mockRequest = {
38+
headers: new Headers(),
39+
}
40+
41+
mockResponse = {
42+
headers: mockHeaders,
43+
}
44+
45+
updateModifiedHeaders(mockRequest.headers, mockResponse.headers)
46+
})
47+
48+
it("does not modify or add headers that are missing 'x-middleware-request' in the name", () => {
49+
assertEquals(mockRequest.headers.get('foo'), null)
50+
})
51+
52+
it("removes 'x-middleware-request-' from headers", () => {
53+
assertEquals(mockRequest.headers.get('x-middleware-request-hello'), null)
54+
assertEquals(mockRequest.headers.get('x-middleware-request-test'), null)
55+
56+
assertEquals(mockRequest.headers.get('hello'), 'world')
57+
assertEquals(mockRequest.headers.get('test'), '123')
58+
})
59+
60+
it("removes 'x-middleware-override-headers' after cleaning headers", () => {
61+
assertEquals(mockRequest.headers.get('x-middleware-override-headers'), null)
62+
})
63+
})
64+
})

0 commit comments

Comments
 (0)