Answer the question
In order to leave comments, you need to log in
SSR: what should be the principle of state caching in angular universal?
There is a web server on express, angular universal is washed down on it. It works in such a way that for each incoming server rendering request, it creates a new instance of ngModuleRef, bootstrap it, waits for the application to stabilize (isStable=true) and renders. The essence of the problem: the stores store data sets, requests that receive data from the API on the grid go for a long time (volumes ~ 2-3 mb), but could be reused as on the client, that is, download 1 time and then take it from the store (page 5 is rendered -6 sec). We started digging ngExpressEngine , reworked this piece of code:
const moduleRefPromise = setupOptions.aot ?
platformServer(extraProviders).bootstrapModuleFactory(moduleFactory as NgModuleFactory<{}>) :
platformDynamicServer(extraProviders).bootstrapModule(moduleFactory as Type<{}>);
moduleRefPromise.then((moduleRef: NgModuleRef<{}>) => {
handleModuleRef(moduleRef, callback);
});
if (!moduleR) {
const moduleRefPromise = setupOptions.aot ?
platformServer(extraProviders).bootstrapModuleFactory(<NgModuleFactory<{}>>moduleFactory) :
platformDynamicServer(extraProviders).bootstrapModule(<Type<{}>>moduleFactory);
moduleRefPromise.then((moduleRef: NgModuleRef<{}>) => {
moduleR = moduleRef;
handleModuleRef(moduleR, callback, options.req, options.res);
});
} else {
handleModuleRef(moduleR, callback, options.req, options.res);
}
Answer the question
In order to leave comments, you need to log in
Digging and debugging helped to come up with this handleModuleRef code
function handleModuleRef(moduleRef: NgModuleRef<{}>, callback: Function, req, res) {
const state = moduleRef.injector.get(PlatformState);
const appRef = moduleRef.injector.get(ApplicationRef);
const router = appRef.components[0].instance.router;
const zone = appRef.components[0].instance.zone;
zone.run(() => {
router.navigateByUrl(req.originalUrl);
});
appRef.isStable
.filter((isStable: boolean) => isStable)
.first()
.subscribe((stable) => {
const bootstrap = moduleRef.instance['ngOnBootstrap'];
bootstrap && bootstrap();
if (!res || !res.finished) callback(null, state.renderToString());
});
}
look here https://github.com/ozknemoy/a4 the meaning is that a global variable is stored in the html header (it is stuffed with requests to the database, everything is automatic), then it is read out by the service on the front. ngExpressEngine is also used. since when they have not yet filed it into a separate module
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question