diff --git a/modules/builders/src/prerender/index.spec.ts b/modules/builders/src/prerender/index.spec.ts index 238d5e2a4..9690b524c 100644 --- a/modules/builders/src/prerender/index.spec.ts +++ b/modules/builders/src/prerender/index.spec.ts @@ -211,13 +211,11 @@ describe('Prerender Builder', () => { ]); }); - it('should write to "index/index.html" for route "/"', async () => { + it('should save index.html as index.original.html when caching route "/"', async () => { await PrerenderModule._renderUniversal(options, context, browserResult, serverResult); - expect(mkdirSyncSpy.calls.allArgs()).toEqual([ - ['dist/browser/index', { recursive: true }], - ]); expect(writeFileSyncSpy.calls.allArgs()).toEqual([ - ['dist/browser/index/index.html', RENDERED_HTML], + ['dist/browser/index.original.html', INITIAL_HTML], + ['dist/browser/index.html', RENDERED_HTML], ]); }); @@ -234,6 +232,7 @@ describe('Prerender Builder', () => { mkdirSyncSpy.and.callFake(() => { throw new Error('Test mkdirSync error.'); }); + spyOn(context.logger, 'error'); await expectAsync( PrerenderModule._renderUniversal( options, @@ -243,7 +242,7 @@ describe('Prerender Builder', () => { ) ).not.toBeRejected(); expect(mkdirSyncSpy).toHaveBeenCalled(); - expect(writeFileSyncSpy).not.toHaveBeenCalled(); + expect(context.logger.error).toHaveBeenCalled(); }); }); diff --git a/modules/builders/src/prerender/index.ts b/modules/builders/src/prerender/index.ts index cc4bcac1f..db29d06c6 100644 --- a/modules/builders/src/prerender/index.ts +++ b/modules/builders/src/prerender/index.ts @@ -55,10 +55,15 @@ export async function _renderUniversal( }; const html = await renderModuleFn(AppServerModuleDef, renderOpts); - const outputFolderName = route === '/' ? 'index' : route; - const outputFolderPath = path.join(outputPath, outputFolderName); + const outputFolderPath = path.join(outputPath, route); const outputIndexPath = path.join(outputFolderPath, 'index.html'); + // This case happens when we are prerendering "/". + if (browserIndexOutputPath === outputIndexPath) { + const browserIndexOutputPathOriginal = path.join(outputPath, 'index.original.html'); + fs.writeFileSync(browserIndexOutputPathOriginal, indexHtml); + } + // There will never conflicting output folders // because items in options.routes must be unique. try {