It was working in angular 7
When prerendering routes, this error is thrown:
this._zone.onMicrotaskEmpty.subscribe({ next: (/**
^
TypeError: Cannot read property 'subscribe' of undefined
at new core_ApplicationRef (/Users/jamieperkins/Web-projects/ng8ssr/dist/server/main.js:73198:37)
Error is reproduced in this repo: https://github.com/inorganik/ngx-countup-with-ssr
All I did was:
ng new ng8ssr with v8 of the CLIng add @nguniversal/express-engine --clientProject ng8ssrif (process.env.PRERENDER) {
const routes = ['/'];
const template = readFileSync(join(__dirname, DIST_FOLDER, 'index.html')).toString();
Promise.all(
routes.map(route =>
renderModuleFactory(AppServerModuleNgFactory, {
document: template,
url: route,
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP)
]
}).then(html => [route, html])
)
).then(results => {
results.forEach(([route, html]) => {
const fullPath = join('./public', route);
if (!existsSync(fullPath)) { mkdirSync(fullPath); }
writeFileSync(join(fullPath, 'index.html'), html);
});
process.exit();
});
} else {
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
}
After running npm run build:ssr, run npm run prerender:ssr, and it will throw the error.
this._zone.onMicrotaskEmpty.subscribe({ next: (/**
^
TypeError: Cannot read property 'subscribe' of undefined
at new core_ApplicationRef (/Users/jamieperkins/Web-projects/ng8ssr/dist/server/main.js:73198:37)
Package Version
--------------------------------------------------------------------
@angular-devkit/architect 0.803.24
@angular-devkit/build-angular 0.803.24
@angular-devkit/build-optimizer 0.803.24
@angular-devkit/build-webpack 0.803.24
@angular-devkit/core 8.3.24
@angular-devkit/schematics 8.3.24
@angular/cli 8.3.24
@ngtools/webpack 8.3.24
@nguniversal/express-engine 8.2.6
@nguniversal/module-map-ngfactory-loader 8.2.6
@schematics/angular 8.3.24
@schematics/update 0.803.24
rxjs 6.4.0
typescript 3.5.3
webpack 4.39.2
node 10.9.0
Hi, the problem is that you are requiring @angular/platform-server directly in your server.ts. What you'd need to do is to export renderModuleFactory from main.server.ts and import it via the JS example
const {AppServerModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap, renderModuleFactory} = require('./dist/server/main');
Also, in version 9 we have a better pre-rendering experience via an Angular CLI builder.
You can read more about the changes in version 9 in this article https://trilonio-git-blog-angular-universal-v9.trilon--io.now.sh/blog/angular-universal-v9-whats-new which was created by one of our collaborators @MarkPieszak.
Thanks, it worked! Also exciting stuff in that article, thanks for sharing!
@inorganik glad it worked out 馃榾
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
Hi, the problem is that you are requiring
@angular/platform-serverdirectly in yourserver.ts. What you'd need to do is to exportrenderModuleFactoryfrommain.server.tsand import it via the JS exampleAlso, in version 9 we have a better pre-rendering experience via an Angular CLI builder.
You can read more about the changes in version 9 in this article https://trilonio-git-blog-angular-universal-v9.trilon--io.now.sh/blog/angular-universal-v9-whats-new which was created by one of our collaborators @MarkPieszak.