A guide to implementing cache tags with Astro, Sanity CMS, and Cloudflare for efficient cache invalidation.
---
// src/pages/blog/[slug].astro
import { sanityClient } from '../../lib/sanity';| # Astro + Sanity + Cloudflare Cache Tags | |
| A guide to implementing cache tags with Astro, Sanity CMS, and Cloudflare for efficient cache invalidation. | |
| ## 1. Astro page with cache tags | |
| ```astro | |
| --- | |
| // src/pages/blog/[slug].astro | |
| import { sanityClient } from '../../lib/sanity'; |
| import { z } from "zod"; | |
| export type Locale = z.input<typeof localeSchema>; | |
| const localeSchema = z.object({ | |
| name: z.string(), | |
| path: z.string(), | |
| default: z.boolean().optional(), | |
| dir: z.enum(["ltr", "rtl"]).optional().default("ltr"), | |
| metadata: z.record(z.unknown()).optional().default({}), |
| import { useFetcher } from "@remix-run/react"; | |
| import { Control, Field, Message, Root, Submit } from "@radix-ui/react-form"; | |
| import { useEffect, useState } from "react"; | |
| import { type DataFunctionArgs } from "@remix-run/node"; | |
| export default function Testing() { | |
| const { Form, data } = useFetcher<typeof action>(); | |
| const [error, setError] = useState(false); | |
| useEffect(() => { |
| {% comment %} | |
| Lazily importing css stylesheets, without blocking the document download. | |
| Accepts: | |
| - sheets: `string` of the css files seperated by `,`. | |
| Usage: | |
| {% render 'stylesheet-import', sheets: 'component.css,not-component.css' %} | |
| {% endcomment %} |
| // 1. copy and paste this in a remix project base directory. | |
| // 2. run `node routes_gen.mjs <optional routes folder default to "./app/routes"> <optional output file default to "./app/routes.ts">` | |
| // 3. you get a cool string[] of all the routes ids in a file inside your project | |
| import fs from "fs"; | |
| import path from "path"; | |
| const routesFolder = process.argv[2] || "./app/routes/"; | |
| const outputFile = process.argv[3] || "./app/routes.ts"; |
| import { sql } from "@vercel/postgres"; | |
| import { drizzle } from "drizzle-orm/vercel-postgres"; | |
| import { migrate } from "drizzle-orm/vercel-postgres/migrator"; | |
| import "dotenv/config"; | |
| async function runMigrate() { | |
| if (!process.env.POSTGRES_DATABASE) { | |
| throw new Error("POSTGRES_DATABASE is not defined"); | |
| } |