Skip to content

Commit bf128dc

Browse files
committed
fix: remote flag not working for preview command's cache population
1 parent 3f7edda commit bf128dc

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
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/populate-cache.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type PopulateCacheOptions = {
109109
environment?: string;
110110
wranglerConfigPath?: string;
111111
cacheChunkSize?: number;
112+
shouldUsePreviewNamespace?: boolean;
112113
};
113114

114115
async function populateR2IncrementalCache(
@@ -197,12 +198,21 @@ async function populateKVIncrementalCache(
197198

198199
writeFileSync(chunkPath, JSON.stringify(kvMapping));
199200

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-
});
201+
runWrangler(
202+
options,
203+
[
204+
"kv bulk put",
205+
quoteShellMeta(chunkPath),
206+
`--binding ${KV_CACHE_BINDING_NAME}`,
207+
...(populateCacheOptions.shouldUsePreviewNamespace ? ["--preview"] : ["--preview false"]),
208+
],
209+
{
210+
target: populateCacheOptions.target,
211+
environment: populateCacheOptions.environment,
212+
configPath: populateCacheOptions.wranglerConfigPath,
213+
logging: "error",
214+
}
215+
);
206216

207217
rmSync(chunkPath);
208218
}
@@ -228,6 +238,7 @@ function populateD1TagCache(
228238
"d1 execute",
229239
D1_TAG_BINDING_NAME,
230240
`--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"`,
241+
...(populateCacheOptions.shouldUsePreviewNamespace ? ["--preview"] : ["--preview false"]),
231242
],
232243
{
233244
target: populateCacheOptions.target,

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

Lines changed: 11 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+
shouldUsePreviewNamespace: args.remote,
3235
});
3336

3437
runWrangler(options, ["dev", ...args.wranglerArgs], { logging: "all" });
@@ -43,7 +46,12 @@ 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+
default: false,
53+
desc: "Run on the global Cloudflare network with access to production resources",
54+
}),
4755
(args) => previewCommand(withWranglerPassthroughArgs(args))
4856
);
4957
}

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

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

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

0 commit comments

Comments
 (0)