Visitar URL original
feat(@angular/build): add `prerenderFormat` option to prerender route… · angular/angular-cli@a6dd90a · GitHub
Skip to content

Commit a6dd90a

Browse files
committed
feat(@angular/build): add prerenderFormat option to prerender routes as <route>.html
Prerendered routes are written to `<route>/index.html`. Static hosts serve such a file under `/<route>/`, so a request to `/<route>` is first redirected to the URL with a trailing slash, which the Angular router then removes again. The new `prerenderFormat` option of the application builder accepts `directory` (default, unchanged behavior) and `file`, like the `build.format` option of Astro. With `file`, routes are written to `<route>.html`, which avoids the redirect on hosts that serve `<route>.html` for `/<route>`. The root route (after removing the `baseHref` option) is still written to `index.html`. Static redirect pages follow the same layout. A route that cannot be written safely to `<route>.html` keeps `<route>/index.html` and the build reports a warning: routes whose last segment is `index` in any casing, routes whose file would be the index output of the application, and routes whose file name is already used by another route. `file` is only considered when the build does not produce a server (for example with `outputMode: "static"`), because the `@angular/ssr` runtime looks up prerendered pages as `<route>/index.html`. When the build produces a server, a warning is reported and routes are written to `<route>/index.html`. Closes #29173
1 parent e023a8e commit a6dd90a

7 files changed

Lines changed: 527 additions & 9 deletions

File tree

‎goldens/public-api/angular/build/index.api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type ApplicationBuilderOptions = {
5555
poll?: number;
5656
polyfills?: string[];
5757
prerender?: PrerenderUnion;
58+
prerenderFormat?: PrerenderFormat;
5859
preserveSymlinks?: boolean;
5960
progress?: boolean;
6061
scripts?: ScriptElement[];

‎packages/angular/build/src/builders/application/execute-post-bundle.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function executePostBundleSteps(
7171
outputMode,
7272
serverEntryPoint,
7373
prerenderOptions,
74+
prerenderFormat,
7475
appShellOptions,
7576
publicPath,
7677
workspaceRoot,
@@ -168,6 +169,8 @@ export async function executePostBundleSteps(
168169
[...outputFiles, ...additionalOutputFiles],
169170
assetFiles,
170171
outputMode,
172+
prerenderFormat,
173+
indexHtmlOptions.output,
171174
sourcemapOptions.scripts,
172175
maxWorkers,
173176
);

‎packages/angular/build/src/builders/application/options.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
OutputMode,
3333
OutputPathClass,
3434
Platform,
35+
PrerenderFormat,
3536
} from './schema';
3637

3738
/**
@@ -342,6 +343,20 @@ export async function normalizeOptions(
342343
options.outputMode === OutputMode.Static,
343344
};
344345

346+
let prerenderFormat = options.prerenderFormat ?? PrerenderFormat.Directory;
347+
if (prerenderFormat === PrerenderFormat.File && !outputOptions.ignoreServer) {
348+
// The server runtime of '@angular/ssr' looks up prerendered pages as '<route>/index.html'.
349+
// The warning is only relevant when pages are actually prerendered, which the dev-server skips.
350+
if ((prerenderOptions || appShellOptions) && !(options.partialSSRBuild || usePartialSsrBuild)) {
351+
context.logger.warn(
352+
'The "prerenderFormat" option set to "file" is not considered when the build produces a ' +
353+
'server ("outputMode" set to "server", or "ssr" without "outputMode").',
354+
);
355+
}
356+
357+
prerenderFormat = PrerenderFormat.Directory;
358+
}
359+
345360
const outputNames = {
346361
bundles:
347362
options.outputHashing === OutputHashing.All || options.outputHashing === OutputHashing.Bundles
@@ -492,6 +507,7 @@ export async function normalizeOptions(
492507
subresourceIntegrity,
493508
serverEntryPoint,
494509
prerenderOptions,
510+
prerenderFormat,
495511
appShellOptions,
496512
outputMode,
497513
ssrOptions,

‎packages/angular/build/src/builders/application/schema.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,12 @@
587587
}
588588
]
589589
},
590+
"prerenderFormat": {
591+
"type": "string",
592+
"description": "Defines the file layout of prerendered (SSG) pages. 'directory': '/foo' is written to 'foo/index.html'. 'file': '/foo' is written to 'foo.html', which some hosting services serve for '/foo' without a redirect to '/foo/'. The root route of the application and of each locale is always written to 'index.html'. Routes that cannot be written safely to '<route>.html' keep '<route>/index.html' with a warning. Only considered when the build does not produce a server.",
593+
"enum": ["directory", "file"],
594+
"default": "directory"
595+
},
590596
"ssr": {
591597
"description": "Server side render (SSR) pages of your application during runtime.",
592598
"default": false,

0 commit comments

Comments
 (0)