Skip to content

Commit a7b0b5f

Browse files
authored
[devtools] Ensure Chrome DevTools workspace can connect with proxy rewrites (#86289)
1 parent 70cdb3c commit a7b0b5f

File tree

7 files changed

+68
-4
lines changed

7 files changed

+68
-4
lines changed

packages/next/src/server/lib/chrome-devtools-workspace.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ServerResponse } from 'http'
22
import type { NextConfigComplete } from '../config-shared'
3-
import type { NextUrlWithParsedQuery } from '../request-meta'
43

54
import { randomUUID } from 'crypto'
65
import * as fs from 'fs'
@@ -11,9 +10,9 @@ import { getStorageDirectory } from '../cache-dir'
1110
let workspaceUUID: string | null = null
1211

1312
export function isChromeDevtoolsWorkspaceUrl(
14-
url: NextUrlWithParsedQuery
13+
pathname: string | undefined
1514
): boolean {
16-
return url.pathname === '/.well-known/appspecific/com.chrome.devtools.json'
15+
return pathname === '/.well-known/appspecific/com.chrome.devtools.json'
1716
}
1817

1918
export async function handleChromeDevtoolsWorkspaceRequest(

packages/next/src/server/lib/router-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ export async function initialize(opts: {
575575
)
576576
}
577577

578-
if (opts.dev && isChromeDevtoolsWorkspaceUrl(parsedUrl)) {
578+
// We want the original pathname without any basePath or proxy rewrites.
579+
if (opts.dev && isChromeDevtoolsWorkspaceUrl(req.url)) {
579580
await handleChromeDevtoolsWorkspaceRequest(res, opts, config)
580581
return
581582
}

test/e2e/chrome-devtools-workspace/chrome-devtools-workspace.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,33 @@ describe('chrome-devtools-workspace default', () => {
3030
}
3131
})
3232
})
33+
34+
describe('chrome-devtools-workspace proxy', () => {
35+
const { isNextDev, next } = nextTestSetup({
36+
files: path.join(__dirname, 'fixtures', 'proxy'),
37+
})
38+
39+
it('should be able to connect to Chrome DevTools in dev', async () => {
40+
const devtoolsResponse = await next.fetch(
41+
'/.well-known/appspecific/com.chrome.devtools.json'
42+
)
43+
if (isNextDev) {
44+
const json = await devtoolsResponse.json()
45+
expect(json).toEqual({
46+
workspace: {
47+
uuid: expect.any(String),
48+
root: next.testDir,
49+
},
50+
})
51+
52+
const pageReload = await next.fetch(
53+
'/.well-known/appspecific/com.chrome.devtools.json'
54+
)
55+
// The UUID should be stable across reloads.
56+
// Otherwise you'd have to reconnect every-time.
57+
expect(await pageReload.json()).toEqual(json)
58+
} else {
59+
expect({ status: devtoolsResponse.status }).toEqual({ status: 404 })
60+
}
61+
})
62+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default async function Page({ params }) {
2+
const { lang } = await params
3+
return `Hello, ${lang} Dave!`
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Suspense } from 'react'
2+
3+
export default function Root({ children }) {
4+
return (
5+
<html>
6+
<body>
7+
<Suspense fallback="loading">{children}</Suspense>
8+
</body>
9+
</html>
10+
)
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NextResponse } from 'next/server'
2+
3+
export function proxy(request, context) {
4+
const url = new URL(request.url)
5+
url.pathname = `/en-EN${url.pathname}`
6+
7+
return NextResponse.rewrite(url)
8+
}
9+
10+
export const config = {
11+
// Matcher ignoring `/_next/`, `/api/`, static assets, favicon, etc.
12+
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
13+
}

0 commit comments

Comments
 (0)