From df3983a060de1249cdbb400df66664f837da461b Mon Sep 17 00:00:00 2001 From: James Date: Thu, 20 Mar 2025 23:28:08 +0000 Subject: [PATCH] docs(cloudflare): cache population commands remove manual commands from d1 section --- pages/cloudflare/caching.mdx | 32 ++++++++++++++++---------------- pages/cloudflare/get-started.mdx | 4 ++-- pages/cloudflare/howtos/dev.mdx | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pages/cloudflare/caching.mdx b/pages/cloudflare/caching.mdx index 8814995..92f128d 100644 --- a/pages/cloudflare/caching.mdx +++ b/pages/cloudflare/caching.mdx @@ -111,22 +111,13 @@ The binding name used in your app's worker is `NEXT_CACHE_D1`. The service bindi } ``` -##### 2. Create tables for tag revalidations - -The D1 tag cache requires two tables; one that keeps a record of the tag/path mappings, and another that tracks revalidation times. +The D1 database uses two tables; one that keeps a record of the tag/path mappings, and another that tracks revalidation times. These tables will be created when initialising the cache. For the tag mappings, the default table name is `tags`, and can be configured by setting the `NEXT_CACHE_D1_TAGS_TABLE` environment variable to a string. For the revalidation times, the default table name is `revalidations` and can be configured by setting the `NEXT_CACHE_D1_REVALIDATIONS_TABLE` environment variable to a string. -Wrangler can be used to create a table with it's [execute](https://developers.cloudflare.com/d1/wrangler-commands/#d1-execute) option. Ensure that you create a table for both your local dev database and your remote database. - -```sh -wrangler d1 execute NEXT_CACHE_D1 --command "CREATE TABLE IF NOT EXISTS tags (tag TEXT NOT NULL, path TEXT NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE)" -wrangler d1 execute NEXT_CACHE_D1 --command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE)" -``` - -##### 3. Configure the cache +##### 2. Configure the cache In your project's OpenNext config, enable the KV cache and set up a queue. The queue will send a revalidation request to a page when needed, but it will not dedupe requests. @@ -144,14 +135,23 @@ export default defineCloudflareConfig({ }); ``` -##### 4. Initialise the cache during deployments +##### 3. Initialise the cache during deployments -In order for the cache to be properly initialised with the build-time revalidation data, you need to setup a command that runs as part of your deploy step. +In order for the cache to be properly initialised with the build-time revalidation data, you need to run a command as part of your deploy step. This should be run as part of each deployment to ensure that the cache is being populated with each build's data. -OpenNext will generate an SQL file during the build that can be used to setup your D1 database. +To populate remote bindings and deploy your application at the same time, you can use the `deploy` command. Similarly, the `preview` command will populate your local bindings and start a Wrangler dev server. ```sh -wrangler d1 execute NEXT_CACHE_D1 --file .open-next/cloudflare/cache-assets-manifest.sql +# Populate remote and deploy. +opennextjs-cloudflare deploy + +# Populate local and start dev server. +opennextjs-cloudflare preview ``` -This should be run as part of each deployment to ensure that the cache is being populated with each build's initial revalidation data. +It is possible to only populate the cache without any other steps with the `populateCache` command. + +```sh +# The target is passed as an option, either `local` or `remote`. +opennextjs-cloudflare populateCache local +``` diff --git a/pages/cloudflare/get-started.mdx b/pages/cloudflare/get-started.mdx index 8a7c29e..f4a33f1 100644 --- a/pages/cloudflare/get-started.mdx +++ b/pages/cloudflare/get-started.mdx @@ -115,8 +115,8 @@ The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `. Add the following to the scripts field of your `package.json` file: ```json -"preview": "opennextjs-cloudflare && wrangler dev", -"deploy": "opennextjs-cloudflare && wrangler deploy", +"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview", +"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy", "cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts", ``` diff --git a/pages/cloudflare/howtos/dev.mdx b/pages/cloudflare/howtos/dev.mdx index a8a901f..0d5b39f 100644 --- a/pages/cloudflare/howtos/dev.mdx +++ b/pages/cloudflare/howtos/dev.mdx @@ -38,7 +38,7 @@ initOpenNextCloudflareForDev(); ### `wrangler dev` and `wrangler deploy` -After you've finished iterating on your Next.js application with `next dev`, you can convert it to a Cloudflare Worker by running the `opennextjs-cloudflare` command. This will generate the Worker code in the `.open-next` directory. +After you've finished iterating on your Next.js application with `next dev`, you can convert it to a Cloudflare Worker by running the `opennextjs-cloudflare build` command. This will generate the Worker code in the `.open-next` directory. You can then preview the app locally in the Cloudflare Workers runtime or deploy it to the Cloudflare network.