66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9+ import assert from 'node:assert' ;
910import { randomUUID } from 'node:crypto' ;
1011import { mkdirSync , readFileSync , writeFileSync } from 'node:fs' ;
1112import { join } from 'node:path' ;
1213import type { Connect } from 'vite' ;
1314
15+ type DevToolsJson = {
16+ workspace : {
17+ root : string ;
18+ uuid : string ;
19+ } ;
20+ } ;
21+
1422const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json' ;
1523
1624export function createChromeDevtoolsMiddleware (
1725 cacheDir : string ,
1826 projectRoot : string ,
1927) : Connect . NextHandleFunction {
20- let devtoolsConfig : string ;
28+ let devtoolsConfig : string | undefined ;
2129 const devtoolsConfigPath = join ( cacheDir , 'com.chrome.devtools.json' ) ;
2230
2331 return function chromeDevtoolsMiddleware ( req , res , next ) {
@@ -27,22 +35,27 @@ export function createChromeDevtoolsMiddleware(
2735 return ;
2836 }
2937
30- // We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
31- try {
32- devtoolsConfig ??= readFileSync ( devtoolsConfigPath , 'utf-8' ) ;
33- } catch {
34- const devtoolsConfigJson = {
35- workspace : {
36- root : projectRoot ,
37- uuid : randomUUID ( ) ,
38- } ,
39- } ;
40-
41- devtoolsConfig = JSON . stringify ( devtoolsConfigJson , undefined , 2 ) ;
38+ if ( ! devtoolsConfig ) {
39+ // We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
4240 try {
43- mkdirSync ( cacheDir , { recursive : true } ) ;
44- writeFileSync ( devtoolsConfigPath , devtoolsConfig ) ;
45- } catch { }
41+ const devtoolsConfig = readFileSync ( devtoolsConfigPath , 'utf-8' ) ;
42+ const devtoolsConfigJson : DevToolsJson = JSON . parse ( devtoolsConfig ) ;
43+ assert . equal ( projectRoot , devtoolsConfigJson ?. workspace . root ) ;
44+ } catch {
45+ const devtoolsConfigJson : DevToolsJson = {
46+ workspace : {
47+ root : projectRoot ,
48+ uuid : randomUUID ( ) ,
49+ } ,
50+ } ;
51+
52+ devtoolsConfig = JSON . stringify ( devtoolsConfigJson , undefined , 2 ) ;
53+
54+ try {
55+ mkdirSync ( cacheDir , { recursive : true } ) ;
56+ writeFileSync ( devtoolsConfigPath , devtoolsConfig ) ;
57+ } catch { }
58+ }
4659 }
4760
4861 res . setHeader ( 'Content-Type' , 'application/json' ) ;
0 commit comments