Skip to content

Commit 0078eee

Browse files
committed
fix(@angular/build): serve emitted HTML assets in dev-server
Previously, `assets-middleware` excluded all `.html` files from being served from virtual output files, causing servable HTML files (such as those emitted via `file` loader when imported from TypeScript) to fall through to `html-fallback-middleware` and redirect to `/index.html`. This commit updates the check in `assets-middleware` so that servable HTML assets (excluding `/index.html` and `/index.csr.html`) are served directly. Fixes #34166
1 parent d968259 commit 0078eee

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/angular/build/src/builders/dev-server/tests/behavior/build-assets_spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,34 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
235235
expect(result?.success).toBeTrue();
236236
expect(response?.status).toBe(404);
237237
});
238+
239+
it('serves an HTML asset emitted via file loader', async () => {
240+
const htmlFileContent = '<h1>Custom HTML Asset</h1>';
241+
await harness.writeFile('src/test.html', htmlFileContent);
242+
await harness.writeFile(
243+
'src/types.d.ts',
244+
'declare module "*.html" { const url: string; export default url; }',
245+
);
246+
await harness.writeFile(
247+
'src/main.ts',
248+
'import testHtml from "./test.html";\n console.log(testHtml);',
249+
);
250+
251+
setupTarget(harness, {
252+
loader: {
253+
'.html': 'file',
254+
},
255+
});
256+
257+
harness.useTarget('serve', {
258+
...BASE_OPTIONS,
259+
});
260+
261+
const { result, response } = await executeOnceAndFetch(harness, 'media/test.html');
262+
expect(result?.success).toBeTrue();
263+
expect(response?.status).toBe(200);
264+
expect(response?.headers.get('content-type')).toContain('text/html');
265+
expect(await response?.text()).toBe(htmlFileContent);
266+
});
238267
});
239268
});

‎packages/angular/build/src/tools/vite/middlewares/assets-middleware.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function createAngularAssetsMiddleware(
9898
// Resource files are handled directly.
9999
// Global stylesheets (CSS files) are currently considered resources to workaround
100100
// dev server sourcemap issues with stylesheets.
101-
if (extension !== '.js' && extension !== '.html') {
101+
if (extension !== '.js' && pathname !== '/index.html' && pathname !== '/index.csr.html') {
102102
const outputFile = outputFiles.get(pathname);
103103
if (outputFile?.servable) {
104104
let data: Uint8Array | string = outputFile.contents;

0 commit comments

Comments
 (0)