Skip to content

Commit 89cd2fe

Browse files
committedJun 8, 2017
Integrate a new feature called "blacklisting" which allows you to explicitly specify routes that you wish to render inside the Route declaration itself ({server: true})
1 parent fc897af commit 89cd2fe

File tree

10 files changed

+42
-16
lines changed

10 files changed

+42
-16
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ssr",
3-
"version": "0.10.7",
3+
"version": "0.10.9",
44
"description": "Angular server-side rendering implementation",
55
"main": "build/index.js",
66
"typings": "build/index.d.ts",

‎source/application/builder/builder.ts

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export interface ApplicationBuilder<V> {
5757
// your application bootstraps.
5858
preboot(preboot?: PrebootConfiguration | boolean): void;
5959

60+
// Enable or disable blacklisting (all server-rendered routes must be marked with server: true
61+
blacklist(blacklist?: boolean): void;
62+
6063
// Configure how long we will wait for the application to stabilize itself before assuming it
6164
// never will stabilize and failing the render operation. For build-time rendering, this can
6265
// be a comfortably high number. For on-demand rendering, you should set this very low so

‎source/application/builder/impl/application.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ export class ApplicationImpl<V, M> implements Application<V> {
3434
this.render.routes = renderableRoutes(await this.discoverRoutes());
3535
}
3636

37+
if (this.render.blacklist) { // route blacklisting
38+
this.render.routes = this.render.routes.filter((r: Route & {server?: boolean}) => r.server === true);
39+
}
40+
3741
if (this.render.routes.length === 0) {
3842
observe.complete();
3943
}
4044
else {
41-
this.renderToStream(this.render).subscribe(
42-
observe.next.bind(observe),
43-
observe.error.bind(observe),
44-
observe.complete.bind(observe));
45+
this.renderToStream(this.render)
46+
.subscribe(
47+
observe.next.bind(observe),
48+
observe.error.bind(observe),
49+
observe.complete.bind(observe));
4550
}
4651
});
4752
}

‎source/application/builder/impl/builder.ts

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export class ApplicationBuilderImpl<V> implements ApplicationBuilder<V> {
7272
this.operation.preboot = config;
7373
}
7474

75+
blacklist(enable?: boolean) {
76+
if (enable != null) {
77+
this.operation.blacklist = enable;
78+
}
79+
}
80+
7581
stateReader<R>(stateReader?: ApplicationStateReader<R>) {
7682
this.operation.stateReader = stateReader;
7783
}

‎source/application/operation.ts

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export interface RenderOperation {
7272

7373
// Do not fail the entire render process if some routes fail to render
7474
pessimistic?: boolean;
75+
76+
// Enable 'blacklist by default' route rendering behaviour (each route you wish to render must be marked with `server: true')
77+
blacklist?: boolean;
7578
}
7679

7780
// A render operation is an operation that forks into multiple concurrent suboperations,

‎source/bin/options/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {FileReference, PrebootConfiguration, Project, OutputProducer} from '../../index';
22

33
export interface CommandLineOptions {
4+
blacklist?: boolean;
45
debug: boolean;
56
output: OutputProducer;
67
preboot: PrebootConfiguration | boolean;

‎source/bin/options/parse.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ export const parseCommandLineOptions = (): CommandLineOptions => {
8282
preboot,
8383
project,
8484
templateDocument: template.content(),
85-
webpack
85+
webpack,
86+
blacklist
8687
};
8788
};
8889

90+
// Enable preboot integration
8991
let enablePreboot: boolean = false;
9092

93+
// Inline CSS resources in the compiled HTML output
9194
let enableInline: boolean = true;
9295

96+
// Enable 'blacklist by default' route rendering behaviour (each route you wish to render must be marked with `server: true')
97+
let blacklist: boolean = false;
98+
9399
const createOutput = (options): OutputProducer =>
94100
options['ipc']
95101
? createInterprocessOutput(options)
@@ -124,12 +130,15 @@ const parseCommandLine = () => {
124130
.option('-a, --application <applicationID>', 'Optional application ID if your CLI configuration contains multiple apps')
125131
.option('-P, --preboot [boolean | json-file | json-text]', 'Enable or disable preboot with optional configuration file or JSON text (otherwise automatically find the root element and use defaults)')
126132
.option('-i, --inline [boolean]', 'Inline of resources referenced in links')
127-
.option('-I, --ipc', 'Send rendered documents to parent process through IPC instead of writing them to disk', false);
133+
.option('-I, --ipc', 'Send rendered documents to parent process through IPC instead of writing them to disk', false)
134+
.option('-b, --blacklist [boolean]', 'Blacklist all routes by default such that all routes which should be rendered must be specially marked with "server: true" in the route definition', false)
128135

129-
options.on('preboot', value => enablePreboot = value || true);
136+
options.on('preboot', value => enablePreboot = value == null ? true : value);
130137

131138
options.on('inline', value => enableInline = value == null ? true : value);
132139

140+
options.on('blacklist', value => blacklist = value == null ? false : value);
141+
133142
return options.parse(process.argv);
134143
};
135144

‎source/bin/render.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const builder = applicationBuilderFromSource(options.project, options.templateDo
2828

2929
builder.preboot(options.preboot);
3030

31+
builder.blacklist(options.blacklist);
32+
3133
// Since we are doing rendering at build time, there is no need to enforce quick zone stabilization.
3234
// We can bump it up so that the process does not fail if an HTTP request takes a long time or
3335
// something along those lines prevents the app zone from stabilizing quickly.

‎source/route/extract.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {NgModuleRef} from '@angular/core';
22

33
import {Location} from '@angular/common';
44

5-
import {Router, Routes} from '@angular/router';
5+
import {Router, Routes, Route as AngularRoute} from '@angular/router';
66

77
import {Route} from './route';
88
import {ApplicationFallbackOptions} from '../static';
@@ -58,12 +58,12 @@ export const extractRoutesFromRouter = (router: Router, location: Location): Arr
5858

5959
const flatten = (parent: Array<string>, routes: Routes): Array<Route> =>
6060
routes.reduce(
61-
(prev, route) => {
61+
(prev, route: AngularRoute & {server?: boolean}) => {
6262
const prepared = location.prepareExternalUrl(parent.concat(route.path || []).join('/'));
6363

6464
const path = prepared.replace(/(^\.|\*\*?)/g, String()).split(/\//g).filter(v => v);
6565

66-
return prev.concat({path}, flatten(path, route.children || []));
66+
return prev.concat({path, server: route.server}, flatten(path, route.children || []));
6767
},
6868
empty);
6969

@@ -72,7 +72,7 @@ export const extractRoutesFromRouter = (router: Router, location: Location): Arr
7272

7373
const singleRoute: Route = {path: []};
7474

75-
export const extractRoutesFromModule = <M>(moduleRef: NgModuleRef<M>): Array<Route> => {
75+
const extractRoutesFromModule = <M>(moduleRef: NgModuleRef<M>): Array<Route> => {
7676
const router: Router = moduleRef.injector.get(Router, null);
7777
if (router == null) {
7878
return [singleRoute];
@@ -83,10 +83,6 @@ export const extractRoutesFromModule = <M>(moduleRef: NgModuleRef<M>): Array<Rou
8383
return extractRoutesFromRouter(router, location);
8484
};
8585

86-
export const applicationRenderableRoutes = async <M>(operation: RouteExtractionOperation<M>): Promise<Array<Route>> => {
87-
return renderableRoutes(await applicationRoutes(operation));
88-
};
89-
9086
export const renderableRoutes = (routes: Array<Route>): Array<Route> => {
9187
const isParameter = (segment: string) => segment.startsWith(':');
9288

‎source/route/route.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface Route {
22
path: Array<string>;
3+
server?: boolean;
34
parameters?: Map<string, string>;
45
queryString?: string;
56
}

0 commit comments

Comments
 (0)
Failed to load comments.