Unverified Commit 6390e074 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

[backport] middleware subrequest patch (#77424)

Same as https://github.com/vercel/next.js/pull/77202 but for v12 release
branch
parent e2c5952e
This diff is collapsed.
......@@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
if: github.repository_owner == 'vercel'
steps:
- uses: dessant/lock-threads@v3
- uses: dessant/lock-threads@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
issue-inactive-days: 30
......
......@@ -19,7 +19,7 @@ jobs:
- name: tune linux network
run: sudo ethtool -K eth0 tx off rx off
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 25
......@@ -28,7 +28,7 @@ jobs:
id: docs-change
- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
with:
node-version: 16
......@@ -61,7 +61,7 @@ jobs:
- name: Turbo Cache
id: turbo-cache
uses: actions/cache@v3
uses: actions/cache@v4
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
with:
path: .turbo
......@@ -75,7 +75,7 @@ jobs:
# "If there are multiple partial matches for a restore key, the action returns the most recently created cache."
# So we get latest cache
- name: Cache built files
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ./packages/next-target
key: next-swc-cargo-cache-ubuntu-latest--${{ hashFiles('**/Cargo.lock') }}
......@@ -97,7 +97,7 @@ jobs:
TURBO_PROJECT: nextjs
- name: Upload artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: next-swc-dev-binary
path: packages/next-swc/native/next-swc.linux-x64-gnu.node
......@@ -113,7 +113,7 @@ jobs:
needs: build-native-dev
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 25
......@@ -121,7 +121,7 @@ jobs:
run: echo "::set-output name=DOCS_CHANGE::$(node scripts/run-for-change.js --not --type docs --exec echo 'nope')"
id: docs-change
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
with:
name: next-swc-dev-binary
......
......@@ -9,7 +9,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: yarn install --frozen-lockfile --check-files
env:
......@@ -19,7 +19,7 @@ jobs:
- run: node run-tests.js --timings --write-timings -g 1/1
- uses: actions/cache@v3
- uses: actions/cache@v4
id: cache-build
with:
path: ./*
......@@ -38,7 +38,7 @@ jobs:
matrix:
group: [1, 2, 3, 4, 5, 6]
steps:
- uses: actions/cache@v3
- uses: actions/cache@v4
id: restore-build
with:
path: ./*
......@@ -46,6 +46,6 @@ jobs:
- run: npm i -g pnpm@latest
- run: npm i -g playwright-chromium@1.22.2 && npx playwright install-deps
- run: npm i -g playwright-chromium@1.51.1 && npx playwright install-deps
- run: node run-tests.js --timings -g ${{ matrix.group }}/6
......@@ -9,7 +9,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: yarn install --frozen-lockfile --check-files
env:
......@@ -19,7 +19,7 @@ jobs:
- run: node run-tests.js --timings --write-timings -g 1/1
- uses: actions/cache@v3
- uses: actions/cache@v4
id: cache-build
with:
path: ./*
......@@ -38,7 +38,7 @@ jobs:
matrix:
group: [1, 2, 3, 4, 5, 6]
steps:
- uses: actions/cache@v3
- uses: actions/cache@v4
id: restore-build
with:
path: ./*
......@@ -46,6 +46,6 @@ jobs:
- run: npm i -g pnpm@latest
- run: npm i -g playwright-chromium@1.22.2 && npx playwright install-deps
- run: npm i -g playwright-chromium@1.51.1 && npx playwright install-deps
- run: node run-tests.js --timings -g ${{ matrix.group }}/6
......@@ -7,8 +7,8 @@ jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- name: 'Run issue validator'
......
......@@ -165,7 +165,7 @@
"npm-run-all": "4.1.5",
"nprogress": "0.2.0",
"pixrem": "5.0.0",
"playwright-chromium": "1.22.2",
"playwright-chromium": "1.51.1",
"plop": "3.0.5",
"postcss-nested": "4.2.1",
"postcss-pseudoelements": "5.0.0",
......
......@@ -27,6 +27,7 @@ import type { NextConfig } from './config-shared'
import type { DynamicRoutes, PageChecker } from './router'
import fs from 'fs'
import crypto from 'crypto'
import { join, relative, resolve, sep } from 'path'
import { IncomingMessage, ServerResponse } from 'http'
import { addRequestMeta, getRequestMeta } from './request-meta'
......@@ -247,6 +248,11 @@ export default class NextNodeServer extends BaseServer {
)
}
const randomBytes = crypto.randomBytes(8)
const middlewareSubrequestId = randomBytes.toString('hex')
;(global as any)[Symbol.for('@next/middleware-subrequest-id')] =
middlewareSubrequestId
if (!options.dev) {
// pre-warm _document and _app as these will be
// needed for most requests
......
......@@ -221,7 +221,12 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
const prevs =
init.headers.get(`x-middleware-subrequest`)?.split(':') || []
const value = prevs.concat(options.moduleName).join(':')
init.headers.set('x-middleware-subrequest', value)
init.headers.set(
'x-middleware-subrequest-id',
(global as any)[Symbol.for('@next/middleware-subrequest-id')]
)
if (!init.headers.has('user-agent')) {
init.headers.set(`user-agent`, `Next.js Middleware`)
......
......@@ -72,7 +72,12 @@ export const run = withTaggedErrors(async (params) => {
const runtime = await getRuntimeContext(params)
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
if (subrequests.includes(params.name)) {
if (
// require the subrequest-id be valid to skip
params.request.headers['x-middleware-subrequest-id'] ===
(global as any)[Symbol.for('@next/middleware-subrequest-id')] &&
subrequests.includes(params.name)
) {
return {
waitUntil: Promise.resolve(),
response: new runtime.context.Response(null, {
......
......@@ -126,7 +126,7 @@ importers:
npm-run-all: 4.1.5
nprogress: 0.2.0
pixrem: 5.0.0
playwright-chromium: 1.22.2
playwright-chromium: 1.51.1
plop: 3.0.5
postcss-nested: 4.2.1
postcss-pseudoelements: 5.0.0
......@@ -281,7 +281,7 @@ importers:
npm-run-all: 4.1.5
nprogress: 0.2.0
pixrem: 5.0.0
playwright-chromium: 1.22.2
playwright-chromium: 1.51.1
plop: 3.0.5
postcss-nested: 4.2.1
postcss-pseudoelements: 5.0.0
......@@ -14922,7 +14922,7 @@ packages:
}
engines: { node: '>=4.0' }
os: [darwin]
deprecated: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
deprecated: Upgrade to fsevents v2 to mitigate potential security issues
requiresBuild: true
dependencies:
bindings: 1.5.0
......@@ -22612,24 +22612,24 @@ packages:
}
dev: false
/playwright-chromium/1.22.2:
/playwright-chromium/1.51.1:
resolution:
{
integrity: sha512-msRcdCIJkdM2R6S+NMJZ02uyZHzJ7TIzkjzs3usDJ1Pwacp9HMhv9T/S4AxtpFXFmvMZq7UJYb0x+tCdHx9+0w==,
integrity: sha512-IQpN8lgv/fx++WRxT8ITZylDwYAamd3Fv5H9g2AWsicp0N829L1oXHziK/6l9h/TMxioSTqc4ifPiZLS8nITxw==,
}
engines: { node: '>=14' }
engines: { node: '>=18' }
hasBin: true
requiresBuild: true
dependencies:
playwright-core: 1.22.2
playwright-core: 1.51.1
dev: true
/playwright-core/1.22.2:
/playwright-core/1.51.1:
resolution:
{
integrity: sha512-w/hc/Ld0RM4pmsNeE6aL/fPNWw8BWit2tg+TfqJ3+p59c6s3B6C8mXvXrIPmfQEobkcFDc+4KirNzOQ+uBSP1Q==,
integrity: sha512-/crRMj8+j/Nq5s8QcvegseuyeZPxpQCZb6HNk3Sos3BlZyAknRjoyJPFWkpNn8v0+P3WiwqFF8P+zQo4eqiNuw==,
}
engines: { node: '>=14' }
engines: { node: '>=18' }
hasBin: true
dev: true
......
......@@ -71,6 +71,24 @@ describe('Middleware Runtime', () => {
}
function runTests({ i18n }: { i18n?: boolean }) {
it('should filter request header properly', async () => {
const res = await fetchViaHTTP(
next.url,
'/redirect-to-somewhere',
{},
{
headers: {
'x-middleware-subrequest':
'middleware:middleware:middleware:middleware:middleware',
},
redirect: 'manual',
}
)
expect(res.status).toBe(307)
expect(res.headers.get('location')).toContain('/somewhere')
})
if ((global as any).isNextDev) {
it('refreshes the page when middleware changes ', async () => {
const browser = await webdriver(next.url, `/about`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment