From b106bc9d07b1e2e38176c484d2fc04251e274aa5 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 8 May 2023 15:29:02 -0400 Subject: [PATCH] fix(@angular-devkit/build-angular): clean incoming index URL before processing in esbuild builder When using the esbuild-based browser application builder with the development server, the incoming URL for the index HTML may contain search parameters or other URL elements that can cause the index HTML content to not be found or processed incorrected by the development server. These elements are cleaned prior to comparison and the original URL is not longer passed to Vite to avoid unneeded Vite specific processing of the content. (cherry picked from commit 45e98a4f5b3baf9c1ba3ed550f33c563ca556c12) --- .../src/builders/dev-server/vite-server.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts index 537306094df6..1cb63c09617e 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts @@ -302,15 +302,27 @@ export async function setupServer( // before the built-in HTML middleware return () => server.middlewares.use(function angularIndexMiddleware(req, res, next) { - if (req.url === '/' || req.url === `/index.html`) { + if (!req.url) { + next(); + + return; + } + + // Parse the incoming request. + // The base of the URL is unused but required to parse the URL. + const parsedUrl = new URL(req.url, 'http://localhost'); + let pathname = parsedUrl.pathname; + if (serverOptions.servePath && pathname.startsWith(serverOptions.servePath)) { + pathname = pathname.slice(serverOptions.servePath.length); + if (pathname[0] !== '/') { + pathname = '/' + pathname; + } + } + if (pathname === '/' || pathname === `/index.html`) { const rawHtml = outputFiles.get('/index.html')?.contents; if (rawHtml) { server - .transformIndexHtml( - req.url, - Buffer.from(rawHtml).toString('utf-8'), - req.originalUrl, - ) + .transformIndexHtml(req.url, Buffer.from(rawHtml).toString('utf-8')) .then((processedHtml) => { res.setHeader('Content-Type', 'text/html'); res.setHeader('Cache-Control', 'no-cache');