-
Notifications
You must be signed in to change notification settings - Fork 4k
External middleware in cloudflare #3254
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
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4ca6955
default initial implementation of an external middleware
93a5ffb
force deploy the server first
adaf25d
add dev config
0aeda50
Only upload and fix for preview
39182c4
fix gh action
4c3cc1f
fix script
4c0c7bc
use worker version overrides
a8fe220
Revert "use worker version overrides"
971bad2
fix url
a976931
make overrides compatible with the external middleware
d28ff61
gh action to deploy middleware and server in sync
7304e34
add different inspector port
c1ee8eb
Merge branch 'main' into cloudflare/fully-external-middleware
conico974 2a9d1db
add try catch around tag cache
042011a
temp disable tag and queue in the server
c8a4994
test without the binding
2c9431e
remove GITBOOK_API binding
0585bf0
use DO tag cache in the server
96d5017
Merge branch 'main' into cloudflare/fully-external-middleware
81e223b
remove useless DO migration
b5422b3
Merge branch 'main' into cloudflare/fully-external-middleware
875c39f
review
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 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,83 @@ | ||
name: Gradual Deploy to Cloudflare | ||
description: Use gradual deployment to deploy to Cloudflare. This action will upload the middleware and server versions to Cloudflare and kept them bound together | ||
inputs: | ||
apiToken: | ||
description: 'Cloudflare API token' | ||
required: true | ||
accountId: | ||
description: 'Cloudflare account ID' | ||
required: true | ||
environment: | ||
description: 'Cloudflare environment to deploy to (staging, production, preview)' | ||
required: true | ||
middlewareVersionId: | ||
description: 'Middleware version ID to deploy' | ||
required: true | ||
serverVersionId: | ||
description: 'Server version ID to deploy' | ||
required: true | ||
outputs: | ||
deployment-url: | ||
description: "Deployment URL" | ||
value: ${{ steps.deploy_middleware.outputs.deployment-url }} | ||
runs: | ||
using: 'composite' | ||
steps: | ||
- id: wrangler_status | ||
name: Check wrangler deployment status | ||
uses: cloudflare/wrangler-action@v3.14.0 | ||
with: | ||
apiToken: ${{ inputs.apiToken }} | ||
accountId: ${{ inputs.accountId }} | ||
workingDirectory: ./ | ||
wranglerVersion: '4.10.0' | ||
environment: ${{ inputs.environment }} | ||
command: deployments status --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc | ||
|
||
# This step is used to get the version ID that is currently deployed to Cloudflare. | ||
- id: extract_current_version | ||
name: Extract current version | ||
shell: bash | ||
run: | | ||
version_id=$(echo "${{ steps.wrangler_status.outputs.command-output }}" | grep -A 3 "(100%)" | grep -oP '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') | ||
echo "version_id=$version_id" >> $GITHUB_OUTPUT | ||
|
||
- id: deploy_server | ||
name: Deploy server to Cloudflare at 0% | ||
uses: cloudflare/wrangler-action@v3.14.0 | ||
with: | ||
apiToken: ${{ inputs.apiToken }} | ||
accountId: ${{ inputs.accountId }} | ||
workingDirectory: ./ | ||
wranglerVersion: '4.10.0' | ||
environment: ${{ inputs.environment }} | ||
command: versions deploy ${{ steps.extract_current_version.outputs.version_id }}@100% ${{ inputs.serverVersionId }}@0% -y --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc | ||
|
||
# Since we use version overrides headers, we can directly deploy the middleware to 100%. | ||
- id: deploy_middleware | ||
name: Deploy middleware to Cloudflare at 100% | ||
uses: cloudflare/wrangler-action@v3.14.0 | ||
with: | ||
apiToken: ${{ inputs.apiToken }} | ||
accountId: ${{ inputs.accountId }} | ||
workingDirectory: ./ | ||
wranglerVersion: '4.10.0' | ||
environment: ${{ inputs.environment }} | ||
command: versions deploy ${{ inputs.middlewareVersionId }}@100% -y --config ./packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc | ||
|
||
- name: Deploy server to Cloudflare at 100% | ||
uses: cloudflare/wrangler-action@v3.14.0 | ||
with: | ||
apiToken: ${{ inputs.apiToken }} | ||
accountId: ${{ inputs.accountId }} | ||
workingDirectory: ./ | ||
wranglerVersion: '4.10.0' | ||
environment: ${{ inputs.environment }} | ||
command: versions deploy ${{ inputs.serverVersionId }}@100% -y --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc | ||
|
||
- name: Outputs | ||
shell: bash | ||
env: | ||
DEPLOYMENT_URL: ${{ steps.deploy_middleware.outputs.deployment-url }} | ||
run: | | ||
echo "URL: ${{ steps.deploy_middleware.outputs.deployment-url }}" |
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import { defineCloudflareConfig } from '@opennextjs/cloudflare'; | ||
import doShardedTagCache from '@opennextjs/cloudflare/overrides/tag-cache/do-sharded-tag-cache'; | ||
import { | ||
softTagFilter, | ||
withFilter, | ||
} from '@opennextjs/cloudflare/overrides/tag-cache/tag-cache-filter'; | ||
import type { OpenNextConfig } from '@opennextjs/cloudflare'; | ||
|
||
export default defineCloudflareConfig({ | ||
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default), | ||
tagCache: withFilter({ | ||
tagCache: doShardedTagCache({ | ||
baseShardSize: 12, | ||
regionalCache: true, | ||
shardReplication: { | ||
numberOfSoftReplicas: 2, | ||
numberOfHardReplicas: 1, | ||
}, | ||
}), | ||
// We don't use `revalidatePath`, so we filter out soft tags | ||
filterFn: softTagFilter, | ||
}), | ||
queue: () => import('./openNext/queue').then((m) => m.default), | ||
|
||
// Performance improvements as we don't use PPR | ||
enableCacheInterception: true, | ||
}); | ||
export default { | ||
default: { | ||
override: { | ||
wrapper: 'cloudflare-node', | ||
converter: 'edge', | ||
proxyExternalRequest: 'fetch', | ||
queue: () => import('./openNext/queue/server').then((m) => m.default), | ||
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default), | ||
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default), | ||
}, | ||
}, | ||
middleware: { | ||
external: true, | ||
override: { | ||
wrapper: 'cloudflare-edge', | ||
converter: 'edge', | ||
proxyExternalRequest: 'fetch', | ||
queue: () => import('./openNext/queue/middleware').then((m) => m.default), | ||
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default), | ||
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default), | ||
}, | ||
}, | ||
dangerous: { | ||
enableCacheInterception: true, | ||
}, | ||
edgeExternals: ['node:crypto'], | ||
} satisfies OpenNextConfig; |
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,15 @@ | ||
import { runWithCloudflareRequestContext } from '../../.open-next/cloudflare/init.js'; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
return runWithCloudflareRequestContext(request, env, ctx, async () => { | ||
// We can't move the handler import to the top level, otherwise the runtime will not be properly initialized | ||
const { handler } = await import( | ||
'../../.open-next/server-functions/default/handler.mjs' | ||
); | ||
|
||
// - `Request`s are handled by the Next server | ||
return handler(request, env, ctx); | ||
}); | ||
}, | ||
}; |
116 changes: 116 additions & 0 deletions
116
packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc
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,116 @@ | ||
{ | ||
"main": "default.js", | ||
"name": "gitbook-open-v2-server", | ||
"compatibility_date": "2025-04-14", | ||
"compatibility_flags": [ | ||
"nodejs_compat", | ||
"allow_importable_env", | ||
"global_fetch_strictly_public" | ||
], | ||
"observability": { | ||
"enabled": true | ||
}, | ||
"vars": { | ||
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true" | ||
}, | ||
"env": { | ||
"dev": { | ||
"vars": { | ||
"STAGE": "dev" | ||
}, | ||
"r2_buckets": [ | ||
{ | ||
"binding": "NEXT_INC_CACHE_R2_BUCKET", | ||
"bucket_name": "gitbook-open-v2-cache-preview" | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"binding": "WORKER_SELF_REFERENCE", | ||
"service": "gitbook-open-v2-server-dev" | ||
} | ||
] | ||
}, | ||
"preview": { | ||
"vars": { | ||
"STAGE": "preview", | ||
// Just as a test for the preview environment to check that everything works | ||
"NEXT_PRIVATE_DEBUG_CACHE": "true" | ||
}, | ||
"r2_buckets": [ | ||
{ | ||
"binding": "NEXT_INC_CACHE_R2_BUCKET", | ||
"bucket_name": "gitbook-open-v2-cache-preview" | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"binding": "WORKER_SELF_REFERENCE", | ||
"service": "gitbook-open-v2-server-preview" | ||
} | ||
] | ||
// No durable objects on preview, as they block the generation of preview URLs | ||
// and we don't need tags invalidation on preview | ||
}, | ||
"staging": { | ||
"r2_buckets": [ | ||
{ | ||
"binding": "NEXT_INC_CACHE_R2_BUCKET", | ||
"bucket_name": "gitbook-open-v2-cache-staging" | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"binding": "WORKER_SELF_REFERENCE", | ||
"service": "gitbook-open-v2-server-staging" | ||
} | ||
], | ||
"durable_objects": { | ||
"bindings": [ | ||
// We do not need to define migrations for external DOs, | ||
// In fact, defining migrations for external DOs will crash | ||
{ | ||
"name": "NEXT_TAG_CACHE_DO_SHARDED", | ||
"class_name": "DOShardedTagCache", | ||
"script_name": "gitbook-open-v2-staging" | ||
} | ||
] | ||
}, | ||
"tail_consumers": [ | ||
{ | ||
"service": "gitbook-x-staging-tail" | ||
} | ||
] | ||
}, | ||
"production": { | ||
"r2_buckets": [ | ||
{ | ||
"binding": "NEXT_INC_CACHE_R2_BUCKET", | ||
"bucket_name": "gitbook-open-v2-cache-production" | ||
} | ||
], | ||
"services": [ | ||
{ | ||
"binding": "WORKER_SELF_REFERENCE", | ||
"service": "gitbook-open-v2-server-production" | ||
} | ||
], | ||
"durable_objects": { | ||
"bindings": [ | ||
{ | ||
// We do not need to define migrations for external DOs, | ||
// In fact, defining migrations for external DOs will crash | ||
"name": "NEXT_TAG_CACHE_DO_SHARDED", | ||
"class_name": "DOShardedTagCache", | ||
"script_name": "gitbook-open-v2-production" | ||
} | ||
] | ||
}, | ||
"tail_consumers": [ | ||
{ | ||
"service": "gitbook-x-prod-tail" | ||
} | ||
] | ||
} | ||
} | ||
} |
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,41 @@ | ||
import { WorkerEntrypoint } from 'cloudflare:workers'; | ||
import { runWithCloudflareRequestContext } from '../../.open-next/cloudflare/init.js'; | ||
|
||
import { handler as middlewareHandler } from '../../.open-next/middleware/handler.mjs'; | ||
|
||
export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js'; | ||
|
||
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js'; | ||
|
||
export default class extends WorkerEntrypoint { | ||
async fetch(request) { | ||
return runWithCloudflareRequestContext(request, this.env, this.ctx, async () => { | ||
// - `Request`s are handled by the Next server | ||
const reqOrResp = await middlewareHandler(request, this.env, this.ctx); | ||
if (reqOrResp instanceof Response) { | ||
return reqOrResp; | ||
} | ||
|
||
if (this.env.STAGE !== 'preview') { | ||
reqOrResp.headers.set( | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'Cloudflare-Workers-Version-Overrides', | ||
`gitbook-open-v2-${this.env.STAGE}="${this.env.WORKER_VERSION_ID}"` | ||
); | ||
return this.env.DEFAULT_WORKER?.fetch(reqOrResp, { | ||
cf: { | ||
cacheEverything: false, | ||
}, | ||
}); | ||
} | ||
// If we are in preview mode, we need to send the request to the preview URL | ||
const modifiedUrl = new URL(reqOrResp.url); | ||
modifiedUrl.hostname = this.env.PREVIEW_HOSTNAME; | ||
const nextRequest = new Request(modifiedUrl, reqOrResp); | ||
return fetch(nextRequest, { | ||
cf: { | ||
cacheEverything: false, | ||
}, | ||
}); | ||
}); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.