MCPcopy
hub / github.com/rx-angular/rx-angular / serveFromCache

Method serveFromCache

libs/isr/server/src/isr-handler.ts:166–232  ·  view source on GitHub ↗
(
    req: Request,
    res: Response,
    next: NextFunction,
    config?: ServeFromCacheConfig,
  )

Source from the content-addressed store, hash-verified

164 }
165
166 async serveFromCache(
167 req: Request,
168 res: Response,
169 next: NextFunction,
170 config?: ServeFromCacheConfig,
171 ): Promise<Response | void> {
172 try {
173 const variant = getVariant(req, this.isrConfig.variants);
174 const cacheKey = this.cacheGeneration.getCacheKey(
175 req.url,
176 this.isrConfig.allowedQueryParams,
177 variant,
178 );
179 const cacheData = await this.cache.get(cacheKey);
180 const { html, options: cacheConfig, createdAt } = cacheData;
181
182 const cacheHasBuildId =
183 cacheConfig.buildId !== null && cacheConfig.buildId !== undefined;
184
185 if (cacheHasBuildId && cacheConfig.buildId !== this.isrConfig.buildId) {
186 // Cache is from a different build. Serve user using SSR
187 next();
188 return;
189 }
190
191 // Cache exists. Send it.
192 this.logger.log(`Page was retrieved from cache: `, cacheKey);
193 let finalHtml = html;
194
195 // if the cache is expired, we will regenerate it
196 if (cacheConfig.revalidate && cacheConfig.revalidate > 0) {
197 const lastCacheDateDiff = (Date.now() - createdAt) / 1000; // in seconds
198
199 if (lastCacheDateDiff > cacheConfig.revalidate) {
200 const generate = () => {
201 return this.cacheGeneration.generateWithCacheKey(
202 req,
203 res,
204 cacheKey,
205 config?.providers,
206 'regenerate',
207 );
208 };
209
210 try {
211 // regenerate the page without awaiting, so the user gets the cached page immediately
212 if (this.isrConfig.backgroundRevalidation) {
213 generate();
214 } else {
215 const result = await generate();
216 if (result?.html) {
217 finalHtml = result.html;
218 }
219 }
220 } catch (error) {
221 console.error('Error generating html', error);
222 next();
223 }

Callers 1

appFunction · 0.95

Calls 6

getVariantFunction · 0.90
nowMethod · 0.80
nextFunction · 0.50
getMethod · 0.45
logMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected