(
req: Request,
res: Response,
config?: InvalidateConfig,
)
| 48 | } |
| 49 | |
| 50 | async invalidate( |
| 51 | req: Request, |
| 52 | res: Response, |
| 53 | config?: InvalidateConfig, |
| 54 | ): Promise<Response> { |
| 55 | const { token, urlsToInvalidate } = extractDataFromBody(req); |
| 56 | |
| 57 | if (token !== this.isrConfig.invalidateSecretToken) { |
| 58 | return res.json({ |
| 59 | status: 'error', |
| 60 | message: 'Your secret token is wrong!!!', |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | if (!urlsToInvalidate || !urlsToInvalidate.length) { |
| 65 | return res.json({ |
| 66 | status: 'error', |
| 67 | message: 'Please add `urlsToInvalidate` in the payload!', |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | const notInCache: string[] = []; |
| 72 | const urlWithErrors: Record<string, string[]> = {}; |
| 73 | |
| 74 | // Include all possible variants in the list of URLs to be invalidated including |
| 75 | // their modified request to regenerate the pages |
| 76 | const variantUrlsToInvalidate = |
| 77 | this.getVariantUrlsToInvalidate(urlsToInvalidate); |
| 78 | |
| 79 | for (const variantUrl of variantUrlsToInvalidate) { |
| 80 | const { cacheKey, url, reqSimulator } = variantUrl; |
| 81 | |
| 82 | // check if the url is in cache |
| 83 | const urlExists = await this.cache.has(cacheKey); |
| 84 | |
| 85 | if (!urlExists) { |
| 86 | notInCache.push(cacheKey); |
| 87 | continue; |
| 88 | } |
| 89 | // override url of req with the one in parameters, |
| 90 | req.url = url; |
| 91 | try { |
| 92 | const result = await this.cacheGeneration.generateWithCacheKey( |
| 93 | reqSimulator(req), |
| 94 | res, |
| 95 | cacheKey, |
| 96 | config?.providers, |
| 97 | 'generate', |
| 98 | ); |
| 99 | |
| 100 | if (result && result.errors?.length) { |
| 101 | urlWithErrors[cacheKey] = result.errors; |
| 102 | } |
| 103 | } catch (err) { |
| 104 | urlWithErrors[cacheKey] = err as string[]; |
| 105 | } |
| 106 | } |
| 107 |
no test coverage detected