|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | + |
| 3 | +import { patchCode } from "../ast/util.js"; |
| 4 | +import { ruleForMinifiedCode, ruleForNonMinifiedCode } from "./fetch-cache-wait-until.js"; |
| 5 | + |
| 6 | +describe("patchFetchCacheSetMissingWaitUntil", () => { |
| 7 | + test("on minified code", () => { |
| 8 | + const code = ` |
| 9 | +{ |
| 10 | + let [o4, a2] = (0, d2.cloneResponse)(e3); |
| 11 | + return o4.arrayBuffer().then(async (e4) => { |
| 12 | + var a3; |
| 13 | + let i4 = Buffer.from(e4), s3 = { headers: Object.fromEntries(o4.headers.entries()), body: i4.toString("base64"), status: o4.status, url: o4.url }; |
| 14 | + null == $ || null == (a3 = $.serverComponentsHmrCache) || a3.set(n2, s3), F && await H.set(n2, { kind: c2.CachedRouteKind.FETCH, data: s3, revalidate: t5 }, { fetchCache: true, revalidate: r4, fetchUrl: _, fetchIdx: q, tags: A2 }); |
| 15 | + }).catch((e4) => console.warn("Failed to set fetch cache", u4, e4)).finally(X), a2; |
| 16 | +}`; |
| 17 | + |
| 18 | + expect(patchCode(code, ruleForMinifiedCode)).toMatchInlineSnapshot(` |
| 19 | + "{ |
| 20 | + let [o4, a2] = (0, d2.cloneResponse)(e3); |
| 21 | + globalThis.__openNextAls?.getStore()?.waitUntil?.(o4.arrayBuffer().then(async (e4) => { |
| 22 | + var a3; |
| 23 | + let i4 = Buffer.from(e4), s3 = { headers: Object.fromEntries(o4.headers.entries()), body: i4.toString("base64"), status: o4.status, url: o4.url }; |
| 24 | + null == $ || null == (a3 = $.serverComponentsHmrCache) || a3.set(n2, s3), F && await H.set(n2, { kind: c2.CachedRouteKind.FETCH, data: s3, revalidate: t5 }, { fetchCache: true, revalidate: r4, fetchUrl: _, fetchIdx: q, tags: A2 }); |
| 25 | + }).catch((e4) => console.warn("Failed to set fetch cache", u4, e4)).finally(X)); |
| 26 | + return a2; |
| 27 | +
|
| 28 | + }" |
| 29 | + `); |
| 30 | + }); |
| 31 | + |
| 32 | + test("on non-minified code", () => { |
| 33 | + const code = ` |
| 34 | + // We're cloning the response using this utility because there |
| 35 | + // exists a bug in the undici library around response cloning. |
| 36 | + // See the following pull request for more details: |
| 37 | + // https://github.com/vercel/next.js/pull/73274 |
| 38 | + const [cloned1, cloned2] = (0, _cloneresponse.cloneResponse)(res); |
| 39 | + // We are dynamically rendering including dev mode. We want to return |
| 40 | + // the response to the caller as soon as possible because it might stream |
| 41 | + // over a very long time. |
| 42 | + cloned1.arrayBuffer().then(async (arrayBuffer)=>{ |
| 43 | + var _requestStore_serverComponentsHmrCache; |
| 44 | + const bodyBuffer = Buffer.from(arrayBuffer); |
| 45 | + const fetchedData = { |
| 46 | + headers: Object.fromEntries(cloned1.headers.entries()), |
| 47 | + body: bodyBuffer.toString('base64'), |
| 48 | + status: cloned1.status, |
| 49 | + url: cloned1.url |
| 50 | + }; |
| 51 | + requestStore == null ? void 0 : (_requestStore_serverComponentsHmrCache = requestStore.serverComponentsHmrCache) == null ? void 0 : _requestStore_serverComponentsHmrCache.set(cacheKey, fetchedData); |
| 52 | + if (isCacheableRevalidate) { |
| 53 | + await incrementalCache.set(cacheKey, { |
| 54 | + kind: _responsecache.CachedRouteKind.FETCH, |
| 55 | + data: fetchedData, |
| 56 | + revalidate: normalizedRevalidate |
| 57 | + }, { |
| 58 | + fetchCache: true, |
| 59 | + revalidate: externalRevalidate, |
| 60 | + fetchUrl, |
| 61 | + fetchIdx, |
| 62 | + tags |
| 63 | + }); |
| 64 | + } |
| 65 | + }).catch((error)=>console.warn(\`Failed to set fetch cache\`, input, error)).finally(handleUnlock); |
| 66 | + return cloned2; |
| 67 | + `; |
| 68 | + |
| 69 | + expect(patchCode(code, ruleForNonMinifiedCode)).toMatchInlineSnapshot(` |
| 70 | + "// We're cloning the response using this utility because there |
| 71 | + // exists a bug in the undici library around response cloning. |
| 72 | + // See the following pull request for more details: |
| 73 | + // https://github.com/vercel/next.js/pull/73274 |
| 74 | + const [cloned1, cloned2] = (0, _cloneresponse.cloneResponse)(res); |
| 75 | + // We are dynamically rendering including dev mode. We want to return |
| 76 | + // the response to the caller as soon as possible because it might stream |
| 77 | + // over a very long time. |
| 78 | + globalThis.__openNextAls?.getStore()?.waitUntil?.(cloned1.arrayBuffer().then(async (arrayBuffer)=>{ |
| 79 | + var _requestStore_serverComponentsHmrCache; |
| 80 | + const bodyBuffer = Buffer.from(arrayBuffer); |
| 81 | + const fetchedData = { |
| 82 | + headers: Object.fromEntries(cloned1.headers.entries()), |
| 83 | + body: bodyBuffer.toString('base64'), |
| 84 | + status: cloned1.status, |
| 85 | + url: cloned1.url |
| 86 | + }; |
| 87 | + requestStore == null ? void 0 : (_requestStore_serverComponentsHmrCache = requestStore.serverComponentsHmrCache) == null ? void 0 : _requestStore_serverComponentsHmrCache.set(cacheKey, fetchedData); |
| 88 | + if (isCacheableRevalidate) { |
| 89 | + await incrementalCache.set(cacheKey, { |
| 90 | + kind: _responsecache.CachedRouteKind.FETCH, |
| 91 | + data: fetchedData, |
| 92 | + revalidate: normalizedRevalidate |
| 93 | + }, { |
| 94 | + fetchCache: true, |
| 95 | + revalidate: externalRevalidate, |
| 96 | + fetchUrl, |
| 97 | + fetchIdx, |
| 98 | + tags |
| 99 | + }); |
| 100 | + } |
| 101 | + }).catch((error)=>console.warn(\`Failed to set fetch cache\`, input, error)).finally(handleUnlock)); |
| 102 | +
|
| 103 | + return cloned2; |
| 104 | + " |
| 105 | + `); |
| 106 | + }); |
| 107 | +}); |
0 commit comments