Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ export async function setupServer(
templateUpdates,
ssrMode,
resetComponentUpdates: () => templateUpdates.clear(),
projectRoot: serverOptions.projectRoot,
}),
createRemoveIdPrefixPlugin(externalMetadata.explicitBrowser),
await createAngularSsrTransformPlugin(serverOptions.workspaceRoot),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { randomUUID } from 'node:crypto';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import type { Connect } from 'vite';

const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json';

export function createChromeDevtoolsMiddleware(
cacheDir: string,
projectRoot: string,
): Connect.NextHandleFunction {
let devtoolsConfig: string;
const devtoolsConfigPath = join(cacheDir, 'com.chrome.devtools.json');

return function chromeDevtoolsMiddleware(req, res, next) {
if (req.url !== CHROME_DEVTOOLS_ROUTE) {
next();

return;
}

// We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
try {
devtoolsConfig ??= readFileSync(devtoolsConfigPath, 'utf-8');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, but what happens if you relocate the project folder? That doesn't seem to trigger deletion of the cached file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, we could add a check here to validate that the path is the same otherwise we could re-generate the entry.

} catch {
const devtoolsConfigJson = {
workspace: {
root: projectRoot,
uuid: randomUUID(),
},
};

devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
try {
mkdirSync(cacheDir, { recursive: true });
writeFileSync(devtoolsConfigPath, devtoolsConfig);
} catch {}
}

res.setHeader('Content-Type', 'application/json');
res.end(devtoolsConfig);
};
}
1 change: 1 addition & 0 deletions packages/angular/build/src/tools/vite/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export {
} from './ssr-middleware';
export { createAngularHeadersMiddleware } from './headers-middleware';
export { createAngularComponentMiddleware } from './component-middleware';
export { createChromeDevtoolsMiddleware } from './chrome-devtools-middleware';
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createAngularIndexHtmlMiddleware,
createAngularSsrExternalMiddleware,
createAngularSsrInternalMiddleware,
createChromeDevtoolsMiddleware,
} from '../middlewares';
import { AngularMemoryOutputFiles, AngularOutputAssets } from '../utils';

Expand Down Expand Up @@ -54,6 +55,7 @@ interface AngularSetupMiddlewaresPluginOptions {
templateUpdates: Map<string, string>;
ssrMode: ServerSsrMode;
resetComponentUpdates: () => void;
projectRoot: string;
}

async function createEncapsulateStyle(): Promise<
Expand Down Expand Up @@ -99,6 +101,10 @@ export function createAngularSetupMiddlewaresPlugin(
),
);

server.middlewares.use(
createChromeDevtoolsMiddleware(server.config.cacheDir, options.projectRoot),
);

extensionMiddleware?.forEach((middleware) => server.middlewares.use(middleware));

// Returning a function, installs middleware after the main transform middleware but
Expand Down