I added server side rendering to my Angular project by following the Angular documentation here.
I found out that the commands to run the static pre-rendering npm run build:prerender
and npm run serve:prerender
were not here after using the CLI command:
ng add @nguniversal/express-engine --clientProject [angular.projet]
I am wondering if the static pre-rendering is still supported in Universal? The code generated is all about dynamic SSR.
That's weird because I found these commands on the universal-starter.
Anyone have information about that? Also how can I add the static pre-rendering in my angular project?
To reproduce, run in the terminal :
ng new foo
to start a new projectng add @nguniversal/express-engine --clientProject foo
to add universalThanks for help.
This is an Express Engine for running Angular Apps on the server for server side rendering.
Prerendering dynamic routeslink An example of a dynamic route is product/:id , where id is dynamically provided. To prerender dynamic routes, choose one from the following options: Provide extra routes in the command line. Provide routes using a file.
Angular Interview Q & A series Server side Rendering (SSR) is a modern technique to convert a Single Page Application (SPA) running in the browser into a server based application. Usually, in SPA, the server returns a simple index. html file with the reference to the JavaScript based SPA app.
Yes, it will be a production build if your angular. json specifies so.
I found a way to add the static pre-rendering manually.
For those interested, all the steps to manually add the static pre-rendering to the ng add @nguniversal/express-engine
:
ng add @nguniversal/express-engine --clientProject [your project name]
to initialize the server-side app module, app.server.module.ts
Create the files prerender.ts
and static.paths.ts
at the root level of your project next to server.ts
Copy the content of https://github.com/angular/universal-starter/blob/master/prerender.ts in your prerender.ts
file
Create your routes in your static.paths.ts
following the example https://github.com/angular/universal-starter/blob/master/static.paths.ts
Add the prerender entry in your webpack.server.config.js
:
module.exports = {
mode: 'none',
entry: {
server: './server.ts',
prerender: './prerender.ts'
},
Add the prerender scripts in your package.json
:
"scripts": {
...
"build:prerender": "npm run build:client-and-server-bundles && npm run compile:server && npm run generate:prerender",
"generate:prerender": "cd dist && node prerender",
"serve:prerender": "cd dist/browser && http-server"
}
Edit the line 17 of your prerender.ts
by
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');
Install the package http-server to serve the prerender build:
npm install http-server --save-dev
You are now ready to go!
npm run build:prerender && npm run serve:prerender
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.0.10:8080
Hit CTRL-C to stop the server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With