-
Notifications
You must be signed in to change notification settings - Fork 483
feat(schematics): add prerendering scripts to express-schematic #1206
Conversation
modules/express-engine/schematics/install/files/root/__prerenderFileName@stripTsExtension__.ts
Outdated
Show resolved
Hide resolved
modules/express-engine/schematics/install/files/root/__prerenderFileName@stripTsExtension__.ts
Outdated
Show resolved
Hide resolved
Please see if you can add an option to the schematics to just add the prender parts - So that it can be added to an existing Universal project. |
…ously installed schematics
Added Flag |
"include": ["<%= stripTsExtension(serverFileName) %>.ts"] | ||
"include": [ | ||
"<%= stripTsExtension(serverFileName) %>.ts", | ||
"<%= stripTsExtension(prerenderFileName) %>.ts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be excluded when using skipPrerender
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot find anywhere were an existing the server.tsconfig
is being modified, since when not using skipUniversal
option the tsconfig will be created by the @schematics/angular
universal schematic.
noop() : externalSchematic('@schematics/angular', 'universal', options), |
server: './<%= stripTsExtension(serverFileName) %>.ts' | ||
server: './<%= stripTsExtension(serverFileName) %>.ts', | ||
// This is our script for Static Prerendering | ||
prerender: './<%= stripTsExtension(prerenderFileName) %>.ts' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be excluded when using skipPrerender
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it should, let me know if I did the templating incorrectly, I couldn't find many good examples within other CLI schematics! :)
<%= skipPrerender ? '' : '"<%= stripTsExtension(prerenderFileName) %>.ts"' %>
@@ -16,5 +16,7 @@ | |||
"dom" | |||
] | |||
}, | |||
"include": ["<%= stripTsExtension(serverFileName) %>.ts"] | |||
"include": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing the closing ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, fixed!
} | ||
|
||
// Writes rendered HTML to index.html, replacing the file if it already exists. | ||
previousRender = previousRender.then(_ => renderModuleFactory(AppServerModuleNgFactory, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is an error here, would this throw with unhandled promise rejection?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import {join} from 'path'; | ||
|
||
// * NOTE :: leave this as require() since this file is built Dynamically from webpack | ||
const {AppServerModuleNgFactory, LAZY_MODULE_MAP, provideModuleMap, renderModuleFactory, enableProdMode} = require('./<%= getServerDistDirectory() %>/main'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't renderModuleFactory
, enableProdMode
in @angular/platform-server
and @angular/core
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes they are but they're actually exported in the server/main.ts
file. This prevents us from having 2 copies of Angular.
Something @vikerman actually figured out!
@@ -16,5 +16,8 @@ | |||
"dom" | |||
] | |||
}, | |||
"include": ["<%= stripTsExtension(serverFileName) %>.ts"] | |||
"include": [ | |||
"<%= stripTsExtension(serverFileName) %>.ts"<%= skipPrerender ? '' : ',' %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified a bit.
"include": [
"<%= stripTsExtension(serverFileName) %>.ts"<% if (!skipPrerender) { %>,
<%= stripTsExtension(prerenderFileName) %>.ts"<% } %>
]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do if's 😆 🤦♂
I was working on this way way too early this morning!
Let me clean this up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha happens to me as well 😜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed everything thanks again Alan, great catches :)
server: './<%= stripTsExtension(serverFileName) %>.ts', | ||
<%= skipPrerender ? "" : "// This is our script for Static Prerendering" %> | ||
<%= skipPrerender ? "" : "prerender: './<%= stripTsExtension(prerenderFileName) %>.ts'" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: remove extra line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
@@ -7,7 +7,10 @@ module.exports = { | |||
mode: 'none', | |||
entry: { | |||
// This is our Express server for Dynamic universal | |||
server: './<%= stripTsExtension(serverFileName) %>.ts' | |||
server: './<%= stripTsExtension(serverFileName) %>.ts', | |||
<%= skipPrerender ? "" : "// This is our script for Static Prerendering" %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
entry: {
// This is our Express server for Dynamic universal
server: './<%= stripTsExtension(serverFileName) %>.ts'<% if(!skipPrerender) { %>,
// This is our script for Static Prerendering
"prerender": './<%= stripTsExtension(prerenderFileName) %>.ts' %><% } %>
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
@@ -194,12 +219,16 @@ function addExports(options: UniversalOptions): Rule { | |||
const mainSourceFile = getTsSourceFile(host, mainPath); | |||
let mainText = getTsSourceText(host, mainPath); | |||
const mainRecorder = host.beginUpdate(mainPath); | |||
const enableProdModeExport = generateExport(mainSourceFile, ['enableProdMode'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we should actually check if these exports already exists prior to adding them. Since, we will add these exports in the universal schematics as well
@@ -194,12 +219,16 @@ function addExports(options: UniversalOptions): Rule { | |||
const mainSourceFile = getTsSourceFile(host, mainPath); | |||
let mainText = getTsSourceText(host, mainPath); | |||
const mainRecorder = host.beginUpdate(mainPath); | |||
const enableProdModeExport = generateExport(mainSourceFile, ['enableProdMode'], | |||
'@angular/core'); | |||
const renderModuleFactoryExport = generateExport(mainSourceFile, ['renderModuleFactory'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this target version 8 or 9? Is so probable we should change renderModuleFactory
to renderModule
Some more context here: angular/angular-cli#15517
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually regarding the later two comments if we wait for version 9 you don’t need to add them because they will be adde by default in the cli version 9.
Also, for existing project I will do a migration to add these exports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually regarding the later two comments if we wait for version 9 you don’t need to add them because they will be adde by default in the cli version 9.
Also, for existing project I will do a migration to add these exports
const routeData = require('./routes.json'); | ||
|
||
// Faster server renders w/ Prod mode (dev mode never needed) | ||
enableProdMode(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still needed since it's already defined in main.server.ts?
@@ -25,6 +25,11 @@ | |||
"format": "path", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skipPrerender
doesn't seem to be defined in the schema.
Superseded by #1357 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Fixes #1201
routes.json
Routes create sitemap.xml for prerendering.
Example:
