-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Deploy docs through Cloudflare Pages #1342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+132
−128
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| addEventListener('fetch', (event) => { | ||
| event.respondWith(proxyDocs(event.request)); | ||
| }); | ||
|
|
||
| const ALLOWED_METHODS = new Set(['GET', 'HEAD']); | ||
| const FORWARDED_REQUEST_HEADERS = [ | ||
| 'accept', | ||
| 'accept-encoding', | ||
| 'accept-language', | ||
| 'cache-control', | ||
| 'if-match', | ||
| 'if-modified-since', | ||
| 'if-none-match', | ||
| 'if-unmodified-since', | ||
| 'range', | ||
| 'user-agent', | ||
| ]; | ||
|
|
||
| async function proxyDocs(request) { | ||
| const incoming = new URL(request.url); | ||
|
|
||
| if (!isDocsRoute(incoming.pathname)) { | ||
| return fetch(request); | ||
| } | ||
|
|
||
| if (!ALLOWED_METHODS.has(request.method)) { | ||
| return new Response(null, { | ||
| status: 405, | ||
| headers: { allow: 'GET, HEAD' }, | ||
| }); | ||
| } | ||
|
|
||
| const upstream = new URL( | ||
| incoming.pathname + incoming.search, | ||
| 'https://openspec-docs.pages.dev', | ||
| ); | ||
|
|
||
| const headers = new Headers(); | ||
| for (const name of FORWARDED_REQUEST_HEADERS) { | ||
| const value = request.headers.get(name); | ||
| if (value !== null) { | ||
| headers.set(name, value); | ||
| } | ||
| } | ||
|
|
||
| const init = { | ||
| method: request.method, | ||
| headers, | ||
| redirect: 'manual', | ||
| }; | ||
|
|
||
| const response = await fetch(upstream.toString(), init); | ||
| const responseHeaders = new Headers(response.headers); | ||
| const location = responseHeaders.get('location'); | ||
|
|
||
| if (location) { | ||
| const redirected = new URL(location, upstream); | ||
| if (redirected.hostname === 'openspec-docs.pages.dev') { | ||
| redirected.protocol = incoming.protocol; | ||
| redirected.host = incoming.host; | ||
| responseHeaders.set('location', redirected.toString()); | ||
| } | ||
| } | ||
|
|
||
| return new Response(response.body, { | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| headers: responseHeaders, | ||
| }); | ||
| } | ||
|
|
||
| function isDocsRoute(pathname) { | ||
| return ( | ||
| pathname === '/docs' || | ||
| pathname.startsWith('/docs/') || | ||
| pathname.startsWith('/_next/') || | ||
| pathname === '/api/search' || | ||
| pathname === '/api/search/' || | ||
| pathname === '/og/docs' || | ||
| pathname.startsWith('/og/docs/') || | ||
| pathname === '/llms.txt' || | ||
| pathname === '/llms-full.txt' || | ||
| pathname === '/llms.mdx/docs' || | ||
| pathname.startsWith('/llms.mdx/docs/') || | ||
| pathname === '/icon.svg' | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "openspec-docs-router", | ||
| "main": "worker.js", | ||
| "compatibility_date": "2026-07-10", | ||
| "routes": [ | ||
| { "pattern": "openspec.dev/docs*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/docs/*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/_next/*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/api/search*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/og/docs/*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/llms*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/llms.mdx/docs/*", "zone_name": "openspec.dev" }, | ||
| { "pattern": "openspec.dev/icon.svg*", "zone_name": "openspec.dev" } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 424
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 2890
🌐 Web query:
Cloudflare Workers route pattern "/path/*" match exact "/path" without trailing slash💡 Result:
In Cloudflare Workers, a route pattern with a trailing asterisk, such as
/path/*, does not match an exact path without a trailing slash (e.g.,/path) [1]. The wildcard operator (*) in Cloudflare route patterns matches zero or more characters [1]. For the pattern/path/*to trigger, there must be content after the/path/prefix. Consequently: -/path/*matches/path/and any path starting with/path/(e.g.,/path/something). -/path/*does not match the exact path/pathbecause the wildcard expects a subsequent character (or a trailing slash) to satisfy the pattern [1]. To match both the exact path/pathand any sub-paths under it, you must define two separate route patterns: 1./path2./path/*Cloudflare routing is deterministic: if multiple patterns match, the system selects the most specific one [1][2]. By explicitly defining both, you ensure all variations of that path are covered [1].Citations:
Add
openspec.dev/og/docsfor the bare path.openspec.dev/og/docs/*doesn’t cover/og/docs, sopathname === '/og/docs'inwebsite/cloudflare/router/worker.jsis unreachable unlesswebsite/cloudflare/router/wrangler.jsoncadds the exact route.🤖 Prompt for AI Agents