[Workers] add best practices guide#28295
Conversation
|
This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:
|
…c Assets, environments, routing, and observability guidance
…nd request body size limits
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
Co-authored-by: irvinebroque <irvinebroque@users.noreply.github.com>
|
The change looks clean. Here is what I did:
This addresses @irvinebroque's feedback: Tail Workers should not be recommended as a best practice for the general case. Workers Observability (Logs + Traces) is the right recommendation, and that is already covered by the preceding section. Tail Workers are better discussed in the context of Workers for Platforms. |
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
|
|
||
| </TypeScriptExample> | ||
|
|
||
| ### Do not use passThroughOnException as error handling |
There was a problem hiding this comment.
/bonk please add this to the reference docs on the passThroughOnException()` API, if we do not already call this out. If you are able to, do this in a separate pull request
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
….mdx Co-authored-by: Brendan Irvine-Broque <birvine-broque@cloudflare.com>
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
|
/bonk address this feedback:
|
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
|
All four changes have been applied:
|
|
Now I see the issue clearly. Let me look at the concat example more carefully — the Here are my suggestions for the review thread: Good catch. The concat example at lines 228-263 actually has this exact bug right now — the Here are three suggestions for improving this section: 1. Fix the existing concat example to use The 2. Add a callout bridging the streaming and After the concat example (around line 265), add a note like:
This explicitly connects the two concepts that are currently presented as independent topics. 3. Add a dedicated The current async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const { readable, writable } = new TransformStream();
// The pipeline writes to the stream after the response is returned.
// Without waitUntil, the Worker can be terminated before the stream is complete.
ctx.waitUntil(
(async () => {
const writer = writable.getWriter();
for (const chunk of await getChunks(env)) {
await writer.write(new TextEncoder().encode(chunk));
}
await writer.close();
})()
);
return new Response(readable, {
headers: { "Content-Type": "text/plain" },
});
}This makes the point that The most important of these is #1 — the existing concat example is actively misleading without |
|
/bonk please review the content in this pull request, extract out the topics and guidance, and then perform a thorough audit of the existing developer docs, to identify specific topics that are covered here, but are not addressed within our existing developer docs. Do not yet attempt to address these, just leave a comment here on this pull request that details your findings, and provides a prioritized list of recommendations for content additions and modifications to the existing developer docs |
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
Docs Coverage Audit: Workers Best Practices GuideI performed a thorough audit of the existing developer docs against every topic covered in this best practices guide. Below is a summary of what the guide covers that is not adequately addressed in the existing docs, followed by a prioritized list of recommended content actions. MethodologyI extracted 21 distinct topics/rules from the guide across 7 sections, then searched the full Topics with GOOD existing coverage (no action needed)These topics are already well-documented in existing reference and product docs. The best practices guide provides a useful consolidated view but does not introduce net-new guidance:
Topics with SIGNIFICANT gaps — Priority actionsP0: Critical inconsistencies and contradictions1.
2.
3.
P1: Missing guidance not covered anywhere4.
5. Structured JSON logging — inconsistent examples in observability docs
6. Prefer Workers-compatible libraries — no general guidance exists
7. Set cache headers explicitly — no best-practice guidance
8. Handle nullable binding returns — implicit but never called out
P2: Getting started guide gaps9. Getting started guide is silent on several best practices
10. Observability framing — no "enable before production" recommendation
P3: Minor inconsistencies11.
12. Wrangler configuration reference is incomplete for observability
13. Compatibility dates example uses hardcoded old date
14.
15. Environments page does not warn about accidental root deployments
Summary — top 5 actions by impact
|
|
Done. I've posted a detailed audit comment on PR #28295: #28295 (comment) Here's a summary of what I found: 7 topics in the best practices guide are already well-covered in existing docs (DO WebSockets, Hyperdrive, service bindings, Queues/Workflows, compatibility dates, Vitest testing, wrangler.jsonc). 15 gaps were identified, organized by priority:
|
src/content/docs/workers/best-practices/workers-best-practices.mdx
Outdated
Show resolved
Hide resolved
Co-authored-by: irvinebroque <irvinebroque@users.noreply.github.com>
|
The change addresses both pieces of feedback:
|
….mdx Co-authored-by: Brendan Irvine-Broque <birvine-broque@cloudflare.com>
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
|
The Here is a summary of the change: Expanded the "Use Queues and Workflows" section to address @irvinebroque's feedback that it lacked guidance on when to use one vs the other. The section now includes:
This fills a significant gap — the research confirmed that the only existing Queues-vs-Workflows comparison table in the entire docs is buried in the Agents SDK API reference, which most developers would never find. |
This comment was marked as outdated.
This comment was marked as outdated.
|
Bonk workflow was cancelled. |
|
Reviewed the current state against all PR comments:
|
|
|
||
| [Workers Static Assets](/workers/static-assets/) is the recommended way to deploy static sites, single-page applications, and full-stack apps on Cloudflare. If you are starting a new project, use Workers instead of Pages. Pages continues to work, but new features and optimizations are focused on Workers. | ||
|
|
||
| For a purely static site, point `assets.directory` at your build output. No Worker script is needed. For a full-stack app, add a `main` entry point and an `ASSETS` binding to serve static files alongside your API. |
There was a problem hiding this comment.
/bonk please evalulate if you recommend that this section also include guidance around run_worker_first. https://developers.cloudflare.com/workers/static-assets/binding/#run_worker_first
Adds a new "Workers Best Practices" page covering 21 rules across 7 sections, with code examples for each. Closes #28291.
The guide is informed by common issues from cloudflare/workers-sdk and cloudflare/cloudflare-docs over the past year, the code-review skill's common-patterns reference, and the existing Durable Objects and Workflows best practices guides.
Covers the 9 explicitly requested topics — compatibility dates, nodejs_compat, observability/tracing with sampling, waitUntil and Tail Workers, streaming, wrangler secret, wrangler types, bindings over REST APIs, Queues/Workflows for background work — plus 12 more from issue/PR research:
Format mirrors "Rules of Durable Objects" and "Rules of Workflows" — each rule has concise prose followed by a TypeScriptExample or WranglerConfig showing the correct pattern (and anti-pattern where useful).
All code examples validated against latest
@cloudflare/workers-typesand wrangler config schema via the code-review skill. 0 critical/high/medium issues.New files:
src/content/docs/workers/best-practices/index.mdxsrc/content/docs/workers/best-practices/workers-best-practices.mdx