Skip to content

Commit f5264d2

Browse files
james-elicxvicb
andauthored
feat: use getPlatformProxy for cache population prefix vars (#622)
* feat: use `getPlatformProxy` for cache population prefix vars * add the var to e2es * Apply suggestions from code review Co-authored-by: Victor Berchet <victor@suumit.com> --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 8ffc02d commit f5264d2

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.changeset/thirty-camels-reflect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
feat: use `getPlatformProxy` for cache population prefix vars

examples/e2e/app-pages-router/wrangler.jsonc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"binding": "WORKER_SELF_REFERENCE",
2020
"service": "app-pages-router"
2121
}
22-
]
22+
],
23+
"vars": {
24+
"NEXT_INC_CACHE_R2_PREFIX": "custom_prefix"
25+
}
2326
}

examples/playground14/wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
],
1717
"vars": {
1818
"hello": "Hello World from the cloudflare context!",
19-
"PROCESS_ENV_VAR": "process.env"
19+
"PROCESS_ENV_VAR": "process.env",
20+
"NEXT_INC_CACHE_KV_PREFIX": "custom_prefix"
2021
}
2122
}

packages/cloudflare/src/cli/commands/populate-cache.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
1313
import { globSync } from "glob";
1414
import { tqdm } from "ts-tqdm";
15-
import { unstable_readConfig } from "wrangler";
15+
import { getPlatformProxy, unstable_readConfig } from "wrangler";
1616

1717
import {
1818
BINDING_NAME as KV_CACHE_BINDING_NAME,
@@ -92,13 +92,14 @@ export function getCacheAssets(opts: BuildOptions): CacheAsset[] {
9292
return assets;
9393
}
9494

95-
function populateR2IncrementalCache(
95+
async function populateR2IncrementalCache(
9696
options: BuildOptions,
9797
populateCacheOptions: { target: WranglerTarget; environment?: string }
9898
) {
9999
logger.info("\nPopulating R2 incremental cache...");
100100

101101
const config = unstable_readConfig({ env: populateCacheOptions.environment });
102+
const proxy = await getPlatformProxy<CloudflareEnv>(populateCacheOptions);
102103

103104
const binding = config.r2_buckets.find(({ binding }) => binding === R2_CACHE_BINDING_NAME);
104105
if (!binding) {
@@ -114,7 +115,7 @@ function populateR2IncrementalCache(
114115

115116
for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
116117
const cacheKey = computeCacheKey(key, {
117-
prefix: process.env[R2_CACHE_PREFIX_ENV_NAME],
118+
prefix: proxy.env[R2_CACHE_PREFIX_ENV_NAME],
118119
buildId,
119120
cacheType: isFetch ? "fetch" : "cache",
120121
});
@@ -130,13 +131,14 @@ function populateR2IncrementalCache(
130131
logger.info(`Successfully populated cache with ${assets.length} assets`);
131132
}
132133

133-
function populateKVIncrementalCache(
134+
async function populateKVIncrementalCache(
134135
options: BuildOptions,
135136
populateCacheOptions: { target: WranglerTarget; environment?: string }
136137
) {
137138
logger.info("\nPopulating KV incremental cache...");
138139

139140
const config = unstable_readConfig({ env: populateCacheOptions.environment });
141+
const proxy = await getPlatformProxy<CloudflareEnv>(populateCacheOptions);
140142

141143
const binding = config.kv_namespaces.find(({ binding }) => binding === KV_CACHE_BINDING_NAME);
142144
if (!binding) {
@@ -147,7 +149,7 @@ function populateKVIncrementalCache(
147149

148150
for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
149151
const cacheKey = computeCacheKey(key, {
150-
prefix: process.env[KV_CACHE_PREFIX_ENV_NAME],
152+
prefix: proxy.env[KV_CACHE_PREFIX_ENV_NAME],
151153
buildId,
152154
cacheType: isFetch ? "fetch" : "cache",
153155
});
@@ -220,10 +222,10 @@ export async function populateCache(
220222
const name = await resolveCacheName(incrementalCache);
221223
switch (name) {
222224
case R2_CACHE_NAME:
223-
populateR2IncrementalCache(options, populateCacheOptions);
225+
await populateR2IncrementalCache(options, populateCacheOptions);
224226
break;
225227
case KV_CACHE_NAME:
226-
populateKVIncrementalCache(options, populateCacheOptions);
228+
await populateKVIncrementalCache(options, populateCacheOptions);
227229
break;
228230
case STATIC_ASSETS_CACHE_NAME:
229231
populateStaticAssetsIncrementalCache(options);

0 commit comments

Comments
 (0)