Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default defineConfig({
markdown: {
headingLinks: false,
},
routeMiddleware: "./src/plugins/starlight/route-data.ts",
}),
liveCode({}),
icon(),
Expand Down
2 changes: 0 additions & 2 deletions src/content/docs/kv/examples/cache-data-with-workers-kv.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
type: example
summary: Cache data or API responses in Workers KV to improve application performance
tags:
- KV
pcx_content_type: configuration
title: Cache data with Workers KV
sidebar:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
type: example
summary: Use Workers KV to as a geo-distributed, low-latency configuration store for your Workers application
tags:
- KV
pcx_content_type: configuration
title: Build a distributed configuration store
sidebar:
Expand All @@ -19,7 +17,7 @@ In this example, application configuration data is used to personalize the Worke
## Write your configuration from your external application to Workers KV

In some cases, your source-of-truth for your configuration data may be stored elsewhere than Workers KV.
If this is the case, use the Workers KV REST API to write the configuration data to your Workers KV namespace.
If this is the case, use the Workers KV REST API to write the configuration data to your Workers KV namespace.

The following external Node.js application demonstrates a simple scripts that reads user data from a database and writes it to Workers KV using the REST API library.

Expand All @@ -31,63 +29,65 @@ const { Cloudflare } = require('cloudflare');
const { backOff } = require('exponential-backoff');

if(!process.env.DATABASE_CONNECTION_STRING || !process.env.CLOUDFLARE_EMAIL || !process.env.CLOUDFLARE_API_KEY || !process.env.CLOUDFLARE_WORKERS_KV_NAMESPACE_ID || !process.env.CLOUDFLARE_ACCOUNT_ID) {
console.error('Missing required environment variables.');
process.exit(1);
console.error('Missing required environment variables.');
process.exit(1);
}

// Setup Postgres connection
const sql = postgres(process.env.DATABASE_CONNECTION_STRING);

// Setup Cloudflare REST API client
const client = new Cloudflare({
apiEmail: process.env.CLOUDFLARE_EMAIL,
apiKey: process.env.CLOUDFLARE_API_KEY,
apiEmail: process.env.CLOUDFLARE_EMAIL,
apiKey: process.env.CLOUDFLARE_API_KEY,
});

// Function to sync Postgres data to Workers KV
async function syncPreviewStatus() {
console.log('Starting sync of user preview status...');

try {
// Get all users and their preview status
const users = await sql`SELECT id, preview_features_enabled FROM users`;

console.log(users);

// Create the bulk update body
const bulkUpdateBody = users.map(user => ({
key: user.id,
value: JSON.stringify({
preview_features_enabled: user.preview_features_enabled
})
}));

const response = await backOff(async () => {
console.log("trying to update")
try{
const response = await client.kv.namespaces.bulkUpdate(process.env.CLOUDFLARE_WORKERS_KV_NAMESPACE_ID, {
account_id: process.env.CLOUDFLARE_ACCOUNT_ID,
body: bulkUpdateBody
});
}
catch(e){
// Implement your error handling and logging here
console.log(e);
throw e; // Rethrow the error to retry
}
});

console.log(`Sync complete. Updated ${users.length} users.`);
} catch (error) {
console.error('Error syncing preview status:', error);
}
console.log('Starting sync of user preview status...');

try {
// Get all users and their preview status
const users = await sql`SELECT id, preview_features_enabled FROM users`;

console.log(users);

// Create the bulk update body
const bulkUpdateBody = users.map(user => ({
key: user.id,
value: JSON.stringify({
preview_features_enabled: user.preview_features_enabled
})
}));

const response = await backOff(async () => {
console.log("trying to update")
try{
const response = await client.kv.namespaces.bulkUpdate(process.env.CLOUDFLARE_WORKERS_KV_NAMESPACE_ID, {
account_id: process.env.CLOUDFLARE_ACCOUNT_ID,
body: bulkUpdateBody
});
}
catch(e){
// Implement your error handling and logging here
console.log(e);
throw e; // Rethrow the error to retry
}
});

console.log(`Sync complete. Updated ${users.length} users.`);
} catch (error) {
console.error('Error syncing preview status:', error);
}

}

// Run the sync function
syncPreviewStatus()
.catch(console.error)
.finally(() => process.exit(0));
```
.catch(console.error)
.finally(() => process.exit(0));

````
</TabItem>
<TabItem label=".env">
```md title=".env"
Expand Down Expand Up @@ -225,7 +225,7 @@ This code will use the path within the URL and find the file associated to the p

## Optimize performance for configuration

To optimize performance, you may opt to consolidate values in fewer key-value pairs. By doing so, you may benefit from higher caching efficiency and lower latency.
To optimize performance, you may opt to consolidate values in fewer key-value pairs. By doing so, you may benefit from higher caching efficiency and lower latency.

For example, instead of storing each user's configuration in a separate key-value pair, you may store all users' configurations in a single key-value pair. This approach may be suitable for use-cases where the configuration data is small and can be easily managed in a single key-value pair (the [size limit for a Workers KV value is 25 MiB](/kv/platform/limits/)).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
---
type: example
summary: Use Workers KV to store A/B testing configuration data and analyze the performance of different versions of your website
tags:
- KV
pcx_content_type: configuration
title: A/B testing with Workers KV
external_link: /reference-architecture/diagrams/serverless/a-b-testing-using-workers/
sidebar:
order: 6
---
---
4 changes: 0 additions & 4 deletions src/content/docs/kv/examples/routing-with-workers-kv.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
type: example
summary: Store routing data in Workers KV to route requests across various web servers with Workers
tags:
- KV
pcx_content_type: configuration
title: Route requests across various web servers
sidebar:
Expand All @@ -16,7 +14,6 @@ Using Workers KV to store routing data to route requests across various web serv

Routing can be helpful to route requests coming into a single Cloudflare Worker application to different web servers based on the request's path, hostname, or other request attributes.


In single-tenant applications, this can be used to route requests to various origin servers based on the business domain (for example, requests to `/admin` routed to administration server, `/store` routed to storefront server, `/api` routed to the API server).

In multi-tenant applications, requests can be routed to the tenant's respective origin resources (for example, requests to `tenantA.your-worker-hostname.com` routed to server for Tenant A, `tenantB.your-worker-hostname.com` routed to server for Tenant B).
Expand Down Expand Up @@ -142,7 +139,6 @@ In this example, the Cloudflare Worker receives a request and extracts the store
The storefront ID is used to look up the origin server URL from Workers KV using the `get()` method.
The request is then forwarded to the origin server, and the response is modified to include custom headers before being returned to the client.


## Related resources

- [Rust support in Workers](/workers/languages/rust/).
Expand Down
2 changes: 0 additions & 2 deletions src/content/docs/kv/examples/workers-kv-to-serve-assets.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
type: example
summary: Store static assets in Workers KV and serve them from a Worker application with low-latency and high-throughput
tags:
- KV
pcx_content_type: configuration
title: Store and retrieve static assets
sidebar:
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/starlight/route-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
import { tags as allowedTags } from "~/schemas/tags";

export const onRequest = defineRouteMiddleware(({ locals }) => {
const { entry } = locals.starlightRoute;
const { tags } = entry.data;

if (tags) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an additional check for empty tag arrays to avoid unnecessary processing:

Suggested change
if (tags) {
if (tags && tags.length > 0) {

const transformed = tags.map((tag) => {
const values = Object.values(allowedTags).flat();

const match = values.find(
(val) =>
val.label.toLowerCase() === tag.toLowerCase() ||
val.variants?.find((v) => v.toLowerCase() === tag.toLowerCase()),
);

if (!match) {
throw new Error(
`Invalid tag on ${entry.id}: ${tag}, please refer to the allowlist in /src/schemas/tags.ts`,
);
}

return match.label;
});

entry.data.tags = transformed;
}
});
26 changes: 16 additions & 10 deletions src/schemas/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ in our page frontmatter. Refer to https://developers.cloudflare.com/style-guide/
and https://developers.cloudflare.com/style-guide/frontmatter/tags/ for more details.
*/

const data_structures: Array<object> = [
type Tag = { label: string; variants?: string[] };

const data_structures: Array<Tag> = [
{ label: "JSON" },
{ label: "TOML" },
{ label: "XML" },
{ label: "YAML" },
];

const frameworks: Array<object> = [
const frameworks: Array<Tag> = [
{ label: "Angular" },
{ label: "Astro" },
{ label: "Hono" },
Expand All @@ -24,7 +26,7 @@ const frameworks: Array<object> = [
{ label: "Vue.js", variants: ["vue", "vuejs"] },
];

const integrations: Array<object> = [
const integrations: Array<Tag> = [
{ label: "Azure", variants: ["Microsoft Azure", "MS Azure"] },
{ label: "AWS", variants: ["Amazon Web Services"] },
{ label: "GCP", variants: ["Google Cloud", "Google Cloud Platform"] },
Expand All @@ -36,7 +38,8 @@ const integrations: Array<object> = [
variants: ["AzureAD", "Azure Active Directory", "MS Entra ID", "Entra ID"],
},
{ label: "Microsoft" },
{ label: "Postgres", variants: ["PostgresSQL"] },
{ label: "MotherDuck" },
{ label: "Postgres", variants: ["PostgreSQL"] },
{ label: "S3" },
{ label: "Sentry" },
{ label: "Stripe" },
Expand All @@ -45,7 +48,7 @@ const integrations: Array<object> = [
{ label: "WordPress" },
];

const languages: Array<object> = [
const languages: Array<Tag> = [
{ label: "Go" },
{ label: "GraphQL" },
{ label: "JavaScript", variants: ["js"] },
Expand All @@ -60,22 +63,22 @@ const languages: Array<object> = [
{ label: "WebAssembly", variants: ["Web Assembly", "wasm"] },
];

const operating_systems: Array<object> = [
const operating_systems: Array<Tag> = [
{ label: "Android", variants: ["ChromeOS"] },
{ label: "iOS" },
{ label: "Linux" },
{ label: "MacOS", variants: ["OS X"] },
{ label: "Windows", variants: ["ms windows"] },
];

const presentation: Array<object> = [{ label: "Video" }];
const presentation: Array<Tag> = [{ label: "Video" }];

const product_features: Array<object> = [
const product_features: Array<Tag> = [
{ label: "Web Crypto", variants: ["webcrypto"] },
{ label: "RPC" },
];

const protocols: Array<object> = [
const protocols: Array<Tag> = [
{ label: "FTP", variants: ["file transfer protocol", "ftps"] },
{ label: "ICMP" },
{ label: "IPsec" },
Expand All @@ -100,7 +103,7 @@ const protocols: Array<object> = [
{ label: "Wireguard" },
];

const use_cases: Array<object> = [
const use_cases: Array<Tag> = [
{ label: "AI" },
{ label: "Authentication", variants: ["auth"] },
{ label: "A/B testing", variants: ["ab test"] },
Expand All @@ -110,6 +113,7 @@ const use_cases: Array<object> = [
{ label: "CORS" },
{ label: "Debugging", variants: ["debug", "troubleshooting"] },
{ label: "Forms" },
{ label: "Full stack", variants: ["full-stack"] },
{ label: "Geolocation" },
{ label: "Headers", variants: ["header"] },
{ label: "Localization" },
Expand All @@ -122,6 +126,8 @@ const use_cases: Array<object> = [
{ label: "Response modification", variants: ["response"] },
{ label: "RPC" },
{ label: "Security" },
{ label: "SPA" },
{ label: "SSG" },
{ label: "URL rewrite", variants: ["rewrite"] },
];

Expand Down
Loading