Skip to content

Commit 6eca769

Browse files
committed
feature(5.0): Upgrade to working copy of Angular 5.0 + Universal
1 parent 56f3014 commit 6eca769

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-21
lines changed

ClientApp/boot.server.PRODUCTION.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'zone.js/dist/zone-node';
2+
import './polyfills/server.polyfills';
3+
import { enableProdMode } from '@angular/core';
4+
import { createServerRenderer } from 'aspnet-prerendering';
5+
6+
// Grab the (Node) server-specific NgModule
7+
const { AppModuleNgFactory } = require('./app/app.module.server.ngfactory'); // <-- ignore this - this is Production only
8+
import { ngAspnetCoreEngine, IEngineOptions, createTransferScript } from '@nguniversal/aspnetcore-engine';
9+
10+
enableProdMode();
11+
12+
export default createServerRenderer((params) => {
13+
14+
// Platform-server provider configuration
15+
const setupOptions: IEngineOptions = {
16+
appSelector: '<app-root></app-root>',
17+
ngModule: AppModuleNgFactory,
18+
request: params,
19+
providers: [
20+
// Optional - Any other Server providers you want to pass
21+
// (remember you'll have to provide them for the Browser as well)
22+
]
23+
};
24+
25+
return ngAspnetCoreEngine(setupOptions).then(response => {
26+
27+
// Apply your transferData to response.globals
28+
response.globals.transferData = createTransferScript({
29+
someData: 'Transfer this to the client on the window.TRANSFER_CACHE {} object',
30+
fromDotnet: params.data.thisCameFromDotNET // example of data coming from dotnet, in HomeController
31+
});
32+
33+
return ({
34+
html: response.html, // our <app-root> serialized
35+
globals: response.globals // all of our styles/scripts/meta-tags/link-tags for aspnet to serve up
36+
});
37+
});
38+
});

ClientApp/tsconfig.app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"exclude": [
1010
"test.ts",
11-
"**/*.spec.ts"
11+
"**/*.spec.ts",
12+
"boot.server.PRODUCTION.ts"
1213
]
1314
}

Server/Controllers/HomeController.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class HomeController : Controller
1919
[HttpGet]
2020
public async Task<IActionResult> Index()
2121
{
22-
// var prerenderResult = await Request.BuildPrerender();
23-
24-
// ViewData["SpaHtml"] = prerenderResult.Html; // our <app-root /> from Angular
25-
// ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
26-
// ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
27-
// ViewData["Scripts"] = prerenderResult.Globals["scripts"]; // scripts (that were in our header)
28-
// ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
29-
// ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
30-
// ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
22+
var prerenderResult = await Request.BuildPrerender();
23+
24+
ViewData["SpaHtml"] = prerenderResult.Html; // our <app-root /> from Angular
25+
ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
26+
ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
27+
ViewData["Scripts"] = prerenderResult.Globals["scripts"]; // scripts (that were in our header)
28+
ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
29+
ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
30+
ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
3131

3232
return View();
3333
}

Views/Home/Index.cshtml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
<!-- @Html.Raw(ViewData["SpaHtml"]) -->
1+
<!-- Remove this if you want to remove Server-side rendering -->
2+
@Html.Raw(ViewData["SpaHtml"])
23

3-
<app-root></app-root>
4+
<!-- if you only want Client-side rendering uncomment this -->
5+
<!-- <app-root></app-root> -->
46

57
<script src="~/dist/vendor.js" asp-append-version="true"></script>
68
@section scripts {

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@angular/animations": "^5.0.0",
2020
"@angular/common": "^5.0.0",
2121
"@angular/compiler": "^5.0.0",
22-
"@angular/compiler-cli": "^5.0.0",
2322
"@angular/core": "^5.0.0",
2423
"@angular/forms": "^5.0.0",
2524
"@angular/http": "^5.0.0",
@@ -56,19 +55,20 @@
5655
"preboot": "^5.0.0",
5756
"raw-loader": "^0.5.1",
5857
"rimraf": "^2.6.2",
59-
"rxjs": "^5.4.3",
58+
"rxjs": "^5.5.6",
6059
"sass-loader": "^6.0.6",
6160
"style-loader": "^0.18.2",
6261
"to-string-loader": "^1.1.5",
63-
"typescript": "2.5.3",
62+
"typescript": "~2.5.0",
6463
"url-loader": "^0.5.7",
6564
"webpack": "^3.6.0",
6665
"webpack-hot-middleware": "^2.19.1",
6766
"webpack-merge": "^4.1.0",
6867
"zone.js": "^0.8.17"
6968
},
7069
"devDependencies": {
71-
"@angular/cli": "^1.5.0",
70+
"@angular/cli": "^1.7.0-beta.1",
71+
"@angular/compiler-cli": "^5.2.1",
7272
"@ngtools/webpack": "^1.9.0",
7373
"@types/chai": "^3.4.34",
7474
"@types/jasmine": "^2.5.37",

webpack.config.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ module.exports = (env) => {
5959
] : [
6060
// new BundleAnalyzerPlugin(),
6161
// Plugins that apply in production builds only
62-
// new webpack.optimize.UglifyJsPlugin(),
6362
new AngularCompilerPlugin({
6463
mainPath: path.join(__dirname, 'ClientApp/boot.browser.ts'),
6564
tsConfigPath: './tsconfig.json',
6665
entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
6766
exclude: ['./**/*.server.ts']
68-
})
69-
]),
67+
}),
68+
// new webpack.optimize.UglifyJsPlugin({
69+
// compress: false,
70+
// mangle: false
71+
// })
72+
]),
7073
devtool: isDevBuild ? 'cheap-eval-source-map' : false,
7174
node: {
7275
fs: "empty"
@@ -76,7 +79,10 @@ module.exports = (env) => {
7679
// Configuration for server-side (prerendering) bundle suitable for running in Node
7780
const serverBundleConfig = merge(sharedConfig, {
7881
// resolve: { mainFields: ['main'] },
79-
entry: { 'main-server': './ClientApp/boot.server.ts' },
82+
entry: {
83+
'main-server':
84+
isDevBuild ? './ClientApp/boot.server.ts' : './ClientApp/boot.server.PRODUCTION.ts'
85+
},
8086
plugins: [
8187
new webpack.DllReferencePlugin({
8288
context: __dirname,
@@ -103,7 +109,7 @@ module.exports = (env) => {
103109
// }),
104110
// Plugins that apply in production builds only
105111
new AngularCompilerPlugin({
106-
mainPath: path.join(__dirname, 'ClientApp/boot.server.ts'),
112+
mainPath: path.join(__dirname, 'ClientApp/boot.server.PRODUCTION.ts'),
107113
tsConfigPath: './tsconfig.json',
108114
entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
109115
exclude: ['./**/*.browser.ts']

0 commit comments

Comments
 (0)