Unverified Commit a7b0b5fa authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[devtools] Ensure Chrome DevTools workspace can connect with proxy rewrites (#86289)

parent 70cdb3c5
import type { ServerResponse } from 'http'
import type { NextConfigComplete } from '../config-shared'
import type { NextUrlWithParsedQuery } from '../request-meta'
import { randomUUID } from 'crypto'
import * as fs from 'fs'
......@@ -11,9 +10,9 @@ import { getStorageDirectory } from '../cache-dir'
let workspaceUUID: string | null = null
export function isChromeDevtoolsWorkspaceUrl(
url: NextUrlWithParsedQuery
pathname: string | undefined
): boolean {
return url.pathname === '/.well-known/appspecific/com.chrome.devtools.json'
return pathname === '/.well-known/appspecific/com.chrome.devtools.json'
}
export async function handleChromeDevtoolsWorkspaceRequest(
......
......@@ -575,7 +575,8 @@ export async function initialize(opts: {
)
}
if (opts.dev && isChromeDevtoolsWorkspaceUrl(parsedUrl)) {
// We want the original pathname without any basePath or proxy rewrites.
if (opts.dev && isChromeDevtoolsWorkspaceUrl(req.url)) {
await handleChromeDevtoolsWorkspaceRequest(res, opts, config)
return
}
......
......@@ -30,3 +30,33 @@ describe('chrome-devtools-workspace default', () => {
}
})
})
describe('chrome-devtools-workspace proxy', () => {
const { isNextDev, next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'proxy'),
})
it('should be able to connect to Chrome DevTools in dev', async () => {
const devtoolsResponse = await next.fetch(
'/.well-known/appspecific/com.chrome.devtools.json'
)
if (isNextDev) {
const json = await devtoolsResponse.json()
expect(json).toEqual({
workspace: {
uuid: expect.any(String),
root: next.testDir,
},
})
const pageReload = await next.fetch(
'/.well-known/appspecific/com.chrome.devtools.json'
)
// The UUID should be stable across reloads.
// Otherwise you'd have to reconnect every-time.
expect(await pageReload.json()).toEqual(json)
} else {
expect({ status: devtoolsResponse.status }).toEqual({ status: 404 })
}
})
})
export default async function Page({ params }) {
const { lang } = await params
return `Hello, ${lang} Dave!`
}
import { Suspense } from 'react'
export default function Root({ children }) {
return (
<html>
<body>
<Suspense fallback="loading">{children}</Suspense>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { NextResponse } from 'next/server'
export function proxy(request, context) {
const url = new URL(request.url)
url.pathname = `/en-EN${url.pathname}`
return NextResponse.rewrite(url)
}
export const config = {
// Matcher ignoring `/_next/`, `/api/`, static assets, favicon, etc.
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}
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