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

[devtools] Implement default `/.well-known/appspecific/com.chrome.devtools.json`…

[devtools] Implement default `/.well-known/appspecific/com.chrome.devtools.json` endpoint in dev (#80260)
parent 9fd385a8
...@@ -697,5 +697,6 @@ ...@@ -697,5 +697,6 @@
"696": "Next DevTools: Can't dispatch %s in this environment. This is a bug in Next.js", "696": "Next DevTools: Can't dispatch %s in this environment. This is a bug in Next.js",
"697": "Next DevTools: Can't render in this environment. This is a bug in Next.js", "697": "Next DevTools: Can't render in this environment. This is a bug in Next.js",
"698": "Next DevTools: App Dev Overlay is already mounted. This is a bug in Next.js", "698": "Next DevTools: App Dev Overlay is already mounted. This is a bug in Next.js",
"699": "Next DevTools: Pages Dev Overlay is already mounted. This is a bug in Next.js" "699": "Next DevTools: Pages Dev Overlay is already mounted. This is a bug in Next.js",
"700": "Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload."
} }
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'
import * as path from 'path'
import { getStorageDirectory } from '../cache-dir'
// Keep the uuid in memory as it should never change during the lifetime of the server.
let workspaceUUID: string | null = null
export function isChromeDevtoolsWorkspaceUrl(
url: NextUrlWithParsedQuery
): boolean {
return url.pathname === '/.well-known/appspecific/com.chrome.devtools.json'
}
export async function handleChromeDevtoolsWorkspaceRequest(
response: ServerResponse,
opts: { dir: string },
config: NextConfigComplete
): Promise<void> {
response.setHeader('Content-Type', 'application/json')
response.end(
JSON.stringify(
await getChromeDevtoolsWorkspace(opts.dir, config.distDir),
null,
2
)
)
}
/**
* https://developer.chrome.com/docs/devtools/workspaces#generate-json
*/
interface ChromeDevtoolsWorkspace {
workspace: {
uuid: string
root: string
}
}
/**
* For https://developer.chrome.com/docs/devtools/workspaces
*/
async function getChromeDevtoolsWorkspace(
root: string,
configDistDir: string
): Promise<ChromeDevtoolsWorkspace> {
if (workspaceUUID === null) {
const distDir = path.join(root, configDistDir)
const cacheBaseDir = getStorageDirectory(distDir)
if (cacheBaseDir === undefined) {
workspaceUUID = randomUUID()
} else {
const cachedUUIDPath = path.join(
cacheBaseDir,
'chrome-devtools-workspace-uuid'
)
try {
workspaceUUID = await fs.promises.readFile(cachedUUIDPath, 'utf8')
} catch {
// TODO: Why does this need to be v4 and not v5?
// With v5 we could base it off of the `distDir` and `root` which would
// allow us to persist the workspace across .next wipes.
workspaceUUID = randomUUID()
try {
await fs.promises.writeFile(cachedUUIDPath, workspaceUUID, 'utf8')
} catch (cause) {
console.warn(
new Error(
'Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload.',
{ cause }
)
)
}
}
}
}
return {
workspace: {
uuid: workspaceUUID,
root,
},
}
}
...@@ -55,6 +55,10 @@ import { ...@@ -55,6 +55,10 @@ import {
RouterServerContextSymbol, RouterServerContextSymbol,
routerServerGlobal, routerServerGlobal,
} from './router-utils/router-server-context' } from './router-utils/router-server-context'
import {
handleChromeDevtoolsWorkspaceRequest,
isChromeDevtoolsWorkspaceUrl,
} from './chrome-devtools-workspace'
const debug = setupDebug('next:router-server:main') const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) => const isNextFont = (pathname: string | null) =>
...@@ -570,6 +574,11 @@ export async function initialize(opts: { ...@@ -570,6 +574,11 @@ export async function initialize(opts: {
) )
} }
if (opts.dev && isChromeDevtoolsWorkspaceUrl(parsedUrl)) {
await handleChromeDevtoolsWorkspaceRequest(res, opts, config)
return
}
// 404 case // 404 case
res.setHeader( res.setHeader(
'Cache-Control', 'Cache-Control',
......
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('chrome-devtools-workspace basePath', () => {
const { isNextDev, next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'base-path'),
})
it('should be able to connect to Chrome DevTools in dev with a configured basePath', async () => {
const devtoolsResponse = await next.fetch(
// Chrome DevTools always requests `/`.
'/.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,
},
})
} else {
expect({ status: devtoolsResponse.status }).toEqual({ status: 404 })
}
})
})
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('chrome-devtools-workspace custom disable', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'custom-disable'),
})
it('can be disabled by providing your own endpoint', async () => {
const devtoolsResponse = await next.fetch(
'/.well-known/appspecific/com.chrome.devtools.json'
)
const json = await devtoolsResponse.json()
expect(json).toEqual('Go away, Chrome DevTools!')
})
})
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('chrome-devtools-workspace default', () => {
const { isNextDev, next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'default'),
})
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 function Root({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
export default function Page() {
return 'Hello, Dave!'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
basePath: '/base-path',
}
module.exports = nextConfig
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return 'Hello, Dave!'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return 'Hello, Dave!'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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