|
| 1 | +import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; |
| 2 | +import { |
| 3 | + fetchRule, |
| 4 | + unstable_cacheRule, |
| 5 | +} from "@opennextjs/aws/build/patch/patchFetchCacheISR.js"; |
| 6 | +import { describe } from "vitest"; |
| 7 | + |
| 8 | +const unstable_cacheCode = ` |
| 9 | +if (// when we are nested inside of other unstable_cache's |
| 10 | + // we should bypass cache similar to fetches |
| 11 | + !isNestedUnstableCache && workStore.fetchCache !== 'force-no-store' && !workStore.isOnDemandRevalidate && !incrementalCache.isOnDemandRevalidate && !workStore.isDraftMode) { |
| 12 | + // We attempt to get the current cache entry from the incremental cache. |
| 13 | + const cacheEntry = await incrementalCache.get(cacheKey, { |
| 14 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 15 | + revalidate: options.revalidate, |
| 16 | + tags, |
| 17 | + softTags: implicitTags, |
| 18 | + fetchIdx, |
| 19 | + fetchUrl |
| 20 | + }); |
| 21 | +} |
| 22 | +else { |
| 23 | + noStoreFetchIdx += 1; |
| 24 | + // We are in Pages Router or were called outside of a render. We don't have a store |
| 25 | + // so we just call the callback directly when it needs to run. |
| 26 | + // If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in |
| 27 | + // the background. If the entry is missing or invalid we generate a new entry and return it. |
| 28 | + if (!incrementalCache.isOnDemandRevalidate) { |
| 29 | + // We aren't doing an on demand revalidation so we check use the cache if valid |
| 30 | + const implicitTags = !workUnitStore || workUnitStore.type === 'unstable-cache' ? [] : workUnitStore.implicitTags; |
| 31 | + const cacheEntry = await incrementalCache.get(cacheKey, { |
| 32 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 33 | + revalidate: options.revalidate, |
| 34 | + tags, |
| 35 | + fetchIdx, |
| 36 | + fetchUrl, |
| 37 | + softTags: implicitTags |
| 38 | + }); |
| 39 | +} |
| 40 | +`; |
| 41 | + |
| 42 | +const patchFetchCacheCodeUnMinified = ` |
| 43 | +const entry = workStore.isOnDemandRevalidate ? null : await incrementalCache.get(cacheKey, { |
| 44 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 45 | + revalidate: finalRevalidate, |
| 46 | + fetchUrl, |
| 47 | + fetchIdx, |
| 48 | + tags, |
| 49 | + softTags: implicitTags |
| 50 | + }); |
| 51 | +`; |
| 52 | + |
| 53 | +const patchFetchCacheCodeMinifiedNext15 = ` |
| 54 | +let t=P.isOnDemandRevalidate?null:await V.get(n,{kind:l.IncrementalCacheKind.FETCH,revalidate:_,fetchUrl:y,fetchIdx:X,tags:N,softTags:C}); |
| 55 | +`; |
| 56 | + |
| 57 | +describe("patchUnstableCacheForISR", () => { |
| 58 | + test("on unminified code", async () => { |
| 59 | + expect( |
| 60 | + patchCode(unstable_cacheCode, unstable_cacheRule), |
| 61 | + ).toMatchInlineSnapshot(` |
| 62 | +"if (// when we are nested inside of other unstable_cache's |
| 63 | + // we should bypass cache similar to fetches |
| 64 | + !isNestedUnstableCache && workStore.fetchCache !== 'force-no-store' && !(workStore.isOnDemandRevalidate && !globalThis.__openNextAls?.getStore()?.isISRRevalidation) && !(incrementalCache.isOnDemandRevalidate && !globalThis.__openNextAls?.getStore()?.isISRRevalidation) && !workStore.isDraftMode) { |
| 65 | + // We attempt to get the current cache entry from the incremental cache. |
| 66 | + const cacheEntry = await incrementalCache.get(cacheKey, { |
| 67 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 68 | + revalidate: options.revalidate, |
| 69 | + tags, |
| 70 | + softTags: implicitTags, |
| 71 | + fetchIdx, |
| 72 | + fetchUrl |
| 73 | + }); |
| 74 | +} |
| 75 | +else { |
| 76 | + noStoreFetchIdx += 1; |
| 77 | + // We are in Pages Router or were called outside of a render. We don't have a store |
| 78 | + // so we just call the callback directly when it needs to run. |
| 79 | + // If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in |
| 80 | + // the background. If the entry is missing or invalid we generate a new entry and return it. |
| 81 | + if (!(incrementalCache.isOnDemandRevalidate && !globalThis.__openNextAls?.getStore()?.isISRRevalidation)) { |
| 82 | + // We aren't doing an on demand revalidation so we check use the cache if valid |
| 83 | + const implicitTags = !workUnitStore || workUnitStore.type === 'unstable-cache' ? [] : workUnitStore.implicitTags; |
| 84 | + const cacheEntry = await incrementalCache.get(cacheKey, { |
| 85 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 86 | + revalidate: options.revalidate, |
| 87 | + tags, |
| 88 | + fetchIdx, |
| 89 | + fetchUrl, |
| 90 | + softTags: implicitTags |
| 91 | + }); |
| 92 | +} |
| 93 | +" |
| 94 | +`); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe("patchFetchCacheISR", () => { |
| 99 | + describe("Next 15", () => { |
| 100 | + test("on unminified code", async () => { |
| 101 | + expect( |
| 102 | + patchCode(patchFetchCacheCodeUnMinified, fetchRule), |
| 103 | + ).toMatchInlineSnapshot(` |
| 104 | +"const entry = (workStore.isOnDemandRevalidate && !globalThis.__openNextAls?.getStore()?.isISRRevalidation) ? null : await incrementalCache.get(cacheKey, { |
| 105 | + kind: _responsecache.IncrementalCacheKind.FETCH, |
| 106 | + revalidate: finalRevalidate, |
| 107 | + fetchUrl, |
| 108 | + fetchIdx, |
| 109 | + tags, |
| 110 | + softTags: implicitTags |
| 111 | + }); |
| 112 | +" |
| 113 | + `); |
| 114 | + }); |
| 115 | + |
| 116 | + test("on minified code", async () => { |
| 117 | + expect( |
| 118 | + patchCode(patchFetchCacheCodeMinifiedNext15, fetchRule), |
| 119 | + ).toMatchInlineSnapshot(` |
| 120 | +"let t=(P.isOnDemandRevalidate && !globalThis.__openNextAls?.getStore()?.isISRRevalidation)?null:await V.get(n,{kind:l.IncrementalCacheKind.FETCH,revalidate:_,fetchUrl:y,fetchIdx:X,tags:N,softTags:C}); |
| 121 | +" |
| 122 | +`); |
| 123 | + }); |
| 124 | + }); |
| 125 | + //TODO: Add test for Next 14.2.24 |
| 126 | +}); |
0 commit comments