Skip to content

Commit 51322a8

Browse files
authored
fix: remote flag not working for preview command's cache population (#888)
* fix: remote flag not working for preview command's cache population * alias * add improvements * remove yargs chunk size default
1 parent f1c73cc commit 51322a8

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

.changeset/odd-colts-walk.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: remote flag not working for preview command's cache population
6+
7+
Previously, passing the `--remote` flag when running `opennextjs-cloudflare preview --remote` would not result in the remote preview binding being populated, and would throw errors due to a missing preview flag when populating Workers KV. The remote flag is now supported for the cache popoulation step when running the preview command.
8+
9+
- `opennextjs-cloudflare preview --remote` will populate the remote binding for the preview ID specified in your Wrangler config.
10+
- `opennextjs-cloudflare preview` will continue to populate the local binding in your Wrangler config.

examples/overrides/d1-tag-next/wrangler.e2e.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
{
1616
"binding": "NEXT_INC_CACHE_KV",
1717
"id": "<BINDING_ID>",
18+
"preview_id": "<BINDING_ID>",
1819
},
1920
],
2021
"d1_databases": [
2122
{
2223
"binding": "NEXT_TAG_CACHE_D1",
2324
"database_id": "NEXT_TAG_CACHE_D1",
25+
"preview_database_id": "NEXT_TAG_CACHE_D1",
2426
"database_name": "NEXT_TAG_CACHE_D1",
2527
},
2628
],

packages/cloudflare/src/cli/commands/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
*
2020
* @param args
2121
*/
22-
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
22+
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: number }>): Promise<void> {
2323
printHeaders("deploy");
2424

2525
const { config } = await retrieveCompiledConfig();
@@ -39,6 +39,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3939
environment: args.env,
4040
wranglerConfigPath: args.wranglerConfigPath,
4141
cacheChunkSize: args.cacheChunkSize,
42+
shouldUsePreviewId: false,
4243
});
4344

4445
runWrangler(

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,28 @@ export function getCacheAssets(opts: BuildOptions): CacheAsset[] {
105105
}
106106

107107
type PopulateCacheOptions = {
108+
/**
109+
* Whether to populate the local or remote cache.
110+
*/
108111
target: WranglerTarget;
112+
/**
113+
* Wrangler environment to use.
114+
*/
109115
environment?: string;
116+
/**
117+
* Path to the Wrangler config file.
118+
*/
110119
wranglerConfigPath?: string;
120+
/**
121+
* Chunk sizes to use when populating KV cache. Ignored for R2.
122+
*
123+
* @default 25 for KV
124+
*/
111125
cacheChunkSize?: number;
126+
/**
127+
* Instructs Wrangler to use the preview namespace or ID defined in the Wrangler config for the remote target.
128+
*/
129+
shouldUsePreviewId: boolean;
112130
};
113131

114132
async function populateR2IncrementalCache(
@@ -197,12 +215,21 @@ async function populateKVIncrementalCache(
197215

198216
writeFileSync(chunkPath, JSON.stringify(kvMapping));
199217

200-
runWrangler(options, ["kv bulk put", quoteShellMeta(chunkPath), `--binding ${KV_CACHE_BINDING_NAME}`], {
201-
target: populateCacheOptions.target,
202-
environment: populateCacheOptions.environment,
203-
configPath: populateCacheOptions.wranglerConfigPath,
204-
logging: "error",
205-
});
218+
runWrangler(
219+
options,
220+
[
221+
"kv bulk put",
222+
quoteShellMeta(chunkPath),
223+
`--binding ${KV_CACHE_BINDING_NAME}`,
224+
`--preview ${populateCacheOptions.shouldUsePreviewId}`,
225+
],
226+
{
227+
target: populateCacheOptions.target,
228+
environment: populateCacheOptions.environment,
229+
configPath: populateCacheOptions.wranglerConfigPath,
230+
logging: "error",
231+
}
232+
);
206233

207234
rmSync(chunkPath);
208235
}
@@ -228,6 +255,7 @@ function populateD1TagCache(
228255
"d1 execute",
229256
D1_TAG_BINDING_NAME,
230257
`--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
258+
`--preview ${populateCacheOptions.shouldUsePreviewId}`,
231259
],
232260
{
233261
target: populateCacheOptions.target,
@@ -301,7 +329,7 @@ export async function populateCache(
301329
*/
302330
async function populateCacheCommand(
303331
target: "local" | "remote",
304-
args: WithWranglerArgs<{ cacheChunkSize: number }>
332+
args: WithWranglerArgs<{ cacheChunkSize?: number }>
305333
) {
306334
printHeaders(`populate cache - ${target}`);
307335

@@ -315,6 +343,7 @@ async function populateCacheCommand(
315343
environment: args.env,
316344
wranglerConfigPath: args.wranglerConfigPath,
317345
cacheChunkSize: args.cacheChunkSize,
346+
shouldUsePreviewId: false,
318347
});
319348
}
320349

@@ -345,7 +374,6 @@ export function addPopulateCacheCommand<T extends yargs.Argv>(y: T) {
345374
export function withPopulateCacheOptions<T extends yargs.Argv>(args: T) {
346375
return withWranglerOptions(args).options("cacheChunkSize", {
347376
type: "number",
348-
default: 25,
349377
desc: "Number of entries per chunk when populating the cache",
350378
});
351379
}

packages/cloudflare/src/cli/commands/preview.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
*
1717
* @param args
1818
*/
19-
export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
19+
export async function previewCommand(
20+
args: WithWranglerArgs<{ cacheChunkSize?: number; remote: boolean }>
21+
): Promise<void> {
2022
printHeaders("preview");
2123

2224
const { config } = await retrieveCompiledConfig();
@@ -25,10 +27,11 @@ export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: nu
2527
const wranglerConfig = readWranglerConfig(args);
2628

2729
await populateCache(options, config, wranglerConfig, {
28-
target: "local",
30+
target: args.remote ? "remote" : "local",
2931
environment: args.env,
3032
wranglerConfigPath: args.wranglerConfigPath,
3133
cacheChunkSize: args.cacheChunkSize,
34+
shouldUsePreviewId: args.remote,
3235
});
3336

3437
runWrangler(options, ["dev", ...args.wranglerArgs], { logging: "all" });
@@ -43,7 +46,13 @@ export function addPreviewCommand<T extends yargs.Argv>(y: T) {
4346
return y.command(
4447
"preview",
4548
"Preview a built OpenNext app with a Wrangler dev server",
46-
(c) => withPopulateCacheOptions(c),
49+
(c) =>
50+
withPopulateCacheOptions(c).option("remote", {
51+
type: "boolean",
52+
alias: "r",
53+
default: false,
54+
desc: "Run on the global Cloudflare network with access to production resources",
55+
}),
4756
(args) => previewCommand(withWranglerPassthroughArgs(args))
4857
);
4958
}

packages/cloudflare/src/cli/commands/upload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
*
2020
* @param args
2121
*/
22-
export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
22+
export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize?: number }>): Promise<void> {
2323
printHeaders("upload");
2424

2525
const { config } = await retrieveCompiledConfig();
@@ -39,6 +39,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3939
environment: args.env,
4040
wranglerConfigPath: args.wranglerConfigPath,
4141
cacheChunkSize: args.cacheChunkSize,
42+
shouldUsePreviewId: false,
4243
});
4344

4445
runWrangler(

packages/cloudflare/src/cli/commands/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ type WranglerInputArgs = {
130130
configPath: string | undefined;
131131
config: string | undefined;
132132
env: string | undefined;
133+
remote?: boolean | undefined;
133134
};
134135

135136
/**
@@ -153,6 +154,7 @@ function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }):
153154
...(args.configPath ? ["--config", args.configPath] : []),
154155
...(args.config ? ["--config", args.config] : []),
155156
...(args.env ? ["--env", args.env] : []),
157+
...(args.remote ? ["--remote"] : []),
156158
// Note: the first args in `_` will be the commands.
157159
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
158160
];

0 commit comments

Comments
 (0)