Skip to content

Commit f32bf1f

Browse files
authored
Use API cache endpoint and service binding in Cloudflare (#2952)
1 parent 6cda2dd commit f32bf1f

File tree

33 files changed

+354
-475
lines changed

33 files changed

+354
-475
lines changed

.changeset/sour-spiders-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/cache-tags': minor
3+
---
4+
5+
Export function `getCacheTagForURL` to easily get the cache tag for a URL.

.github/composite/deploy-cloudflare/action.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ runs:
4444
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
4545
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
4646
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
47+
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
4748
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
4849
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
4950
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY

.github/composite/deploy-vercel/action.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ runs:
4949
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
5050
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
5151
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
52+
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
5253
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
5354
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
5455
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY

.prettierignore

Lines changed: 0 additions & 17 deletions
This file was deleted.

.prettierrc.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

biome.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"**/.vercel/**/*",
1919
"**/.cache/**/*",
2020
"**/.wrangler/**/*",
21-
"packages/openapi-parser/src/fixtures/**/*"
21+
"packages/openapi-parser/src/fixtures/**/*",
22+
"packages/emoji-codepoints/index.ts",
23+
"packages/icons/src/data/*.json"
2224
]
2325
},
2426
"formatter": {

bun.lock

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"name": "@gitbook/cache-tags",
2727
"version": "0.1.0",
2828
"dependencies": {
29-
"@gitbook/api": "0.96.1",
29+
"@gitbook/api": "*",
3030
"assert-never": "^1.2.1",
3131
},
3232
"devDependencies": {
@@ -49,9 +49,9 @@
4949
},
5050
"packages/gitbook": {
5151
"name": "gitbook",
52-
"version": "0.7.2",
52+
"version": "0.7.3",
5353
"dependencies": {
54-
"@gitbook/api": "0.96.1",
54+
"@gitbook/api": "*",
5555
"@gitbook/cache-do": "workspace:*",
5656
"@gitbook/cache-tags": "workspace:*",
5757
"@gitbook/colors": "workspace:*",
@@ -134,7 +134,7 @@
134134
"name": "gitbook-v2",
135135
"version": "0.2.0",
136136
"dependencies": {
137-
"@gitbook/api": "0.96.1",
137+
"@gitbook/api": "*",
138138
"@gitbook/cache-tags": "workspace:*",
139139
"@sindresorhus/fnv1a": "^3.1.0",
140140
"next": "^15.2.1",
@@ -144,15 +144,15 @@
144144
"warn-once": "^0.1.1",
145145
},
146146
"devDependencies": {
147-
"@opennextjs/cloudflare": "https://pkg.pr.new/opennextjs/opennextjs-cloudflare/@opennextjs/cloudflare@236c84d",
147+
"@opennextjs/cloudflare": "^0.5.10",
148148
"gitbook": "*",
149149
"postcss": "^8",
150150
"tailwindcss": "^3.4.0",
151151
},
152152
},
153153
"packages/icons": {
154154
"name": "@gitbook/icons",
155-
"version": "0.1.0",
155+
"version": "0.2.0",
156156
"bin": {
157157
"gitbook-icons": "./bin/gitbook-icons.js",
158158
},
@@ -194,9 +194,9 @@
194194
},
195195
"packages/react-contentkit": {
196196
"name": "@gitbook/react-contentkit",
197-
"version": "0.6.1",
197+
"version": "0.6.2",
198198
"dependencies": {
199-
"@gitbook/api": "0.96.1",
199+
"@gitbook/api": "*",
200200
"@gitbook/icons": "workspace:*",
201201
"classnames": "^2.5.1",
202202
},
@@ -226,7 +226,7 @@
226226
},
227227
"packages/react-openapi": {
228228
"name": "@gitbook/react-openapi",
229-
"version": "1.1.2",
229+
"version": "1.1.3",
230230
"dependencies": {
231231
"@gitbook/openapi-parser": "workspace:*",
232232
"@scalar/api-client-react": "^1.1.36",
@@ -254,6 +254,7 @@
254254
},
255255
"overrides": {
256256
"@codemirror/state": "6.4.1",
257+
"@gitbook/api": "0.97.0",
257258
"react": "18.3.1",
258259
"react-dom": "18.3.1",
259260
},
@@ -626,7 +627,7 @@
626627

627628
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
628629

629-
"@gitbook/api": ["@gitbook/api@0.96.1", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-E1hpaFNFoEyc8E4rIQyC6UD3kErGXAI0lKt04zYQQYOBLjRIQ57D4oNIIJBjWn4hHeSxRxWvW3NcxzvOGMmIqQ=="],
630+
"@gitbook/api": ["@gitbook/api@0.97.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-bBTy7nprwlwTW/v8S6A9wGFWatU+F33232xF5Tt4GmSBTZSrAKha5wD205Hr+618va+/zipA8o4hdAGPyUNj0A=="],
630631

631632
"@gitbook/cache-do": ["@gitbook/cache-do@workspace:packages/cache-do"],
632633

@@ -792,7 +793,7 @@
792793

793794
"@opennextjs/aws": ["@opennextjs/aws@https://pkg.pr.new/@opennextjs/aws@756", { "dependencies": { "@aws-sdk/client-cloudfront": "3.398.0", "@aws-sdk/client-dynamodb": "^3.398.0", "@aws-sdk/client-lambda": "^3.398.0", "@aws-sdk/client-s3": "^3.398.0", "@aws-sdk/client-sqs": "^3.398.0", "@node-minify/core": "^8.0.6", "@node-minify/terser": "^8.0.6", "@tsconfig/node18": "^1.0.1", "aws4fetch": "^1.0.18", "chalk": "^5.3.0", "esbuild": "0.19.2", "express": "5.0.1", "path-to-regexp": "^6.3.0", "urlpattern-polyfill": "^10.0.0" }, "bin": { "open-next": "./dist/index.js" } }],
794795

795-
"@opennextjs/cloudflare": ["@opennextjs/cloudflare@https://pkg.pr.new/opennextjs/opennextjs-cloudflare/@opennextjs/cloudflare@236c84d", { "dependencies": { "@ast-grep/napi": "^0.34.1", "@dotenvx/dotenvx": "1.31.0", "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@756", "enquirer": "^2.4.1", "glob": "^11.0.0", "yaml": "^2.7.0" }, "peerDependencies": { "wrangler": "^3.111.0" }, "bin": { "opennextjs-cloudflare": "dist/cli/index.js" } }],
796+
"@opennextjs/cloudflare": ["@opennextjs/cloudflare@0.5.10", "", { "dependencies": { "@ast-grep/napi": "^0.34.1", "@dotenvx/dotenvx": "1.31.0", "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@756", "enquirer": "^2.4.1", "glob": "^11.0.0", "yaml": "^2.7.0" }, "peerDependencies": { "wrangler": "^3.111.0" }, "bin": { "opennextjs-cloudflare": "dist/cli/index.js" } }, "sha512-L/D472YT5OW1LwpFtD/aVXHJYcVPbFVX7XdphlUjCR4+2osSQIDnsuNgfDRydHMDJZMKxeZDc251ZBzUVKpCqw=="],
796797

797798
"@opentelemetry/api": ["@opentelemetry/[email protected]", "", {}, "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg=="],
798799

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"overrides": {
1212
"@codemirror/state": "6.4.1",
1313
"react": "18.3.1",
14-
"react-dom": "18.3.1"
14+
"react-dom": "18.3.1",
15+
"@gitbook/api": "0.97.0"
1516
},
1617
"private": true,
1718
"scripts": {

packages/cache-tags/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"version": "0.1.0",
1212
"dependencies": {
13-
"@gitbook/api": "0.96.1",
13+
"@gitbook/api": "*",
1414
"assert-never": "^1.2.1"
1515
},
1616
"devDependencies": {

packages/cache-tags/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ export function getCacheTag(
111111
}
112112
}
113113

114+
/**
115+
* Get the cache tag for a given URL.
116+
*/
117+
export function getCacheTagForURL(url: string | URL) {
118+
const parsedURL = url instanceof URL ? url : new URL(url);
119+
return getCacheTag({
120+
tag: 'url',
121+
hostname: parsedURL.hostname,
122+
});
123+
}
124+
114125
/**
115126
* Get the tags for a computed content source.
116127
*/

packages/gitbook-v2/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
"next": "^15.2.1",
77
"react": "^19.0.0",
88
"react-dom": "^19.0.0",
9-
"@gitbook/api": "0.96.1",
9+
"@gitbook/api": "*",
1010
"@gitbook/cache-tags": "workspace:*",
1111
"@sindresorhus/fnv1a": "^3.1.0",
1212
"server-only": "^0.0.1",
1313
"warn-once": "^0.1.1"
1414
},
1515
"devDependencies": {
1616
"gitbook": "*",
17-
"@opennextjs/cloudflare": "https://pkg.pr.new/opennextjs/opennextjs-cloudflare/@opennextjs/cloudflare@236c84d",
17+
"@opennextjs/cloudflare": "^0.5.10",
1818
"tailwindcss": "^3.4.0",
1919
"postcss": "^8"
2020
},
@@ -26,6 +26,7 @@
2626
"start": "next start",
2727
"build:v2:cloudflare": "opennextjs-cloudflare",
2828
"dev:v2:cloudflare": "wrangler dev --port 8771",
29-
"unit": "bun test"
29+
"unit": "bun test",
30+
"typecheck": "tsc --noEmit"
3031
}
3132
}

packages/gitbook-v2/src/app/~gitbook/env/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type NextRequest, NextResponse } from 'next/server';
22

33
import {
4+
GITBOOK_API_PUBLIC_URL,
45
GITBOOK_API_TOKEN,
56
GITBOOK_API_URL,
67
GITBOOK_APP_URL,
@@ -22,6 +23,7 @@ export async function GET(_req: NextRequest) {
2223
GITBOOK_URL,
2324
GITBOOK_APP_URL,
2425
GITBOOK_API_URL,
26+
GITBOOK_API_PUBLIC_URL,
2527
GITBOOK_ASSETS_URL,
2628
GITBOOK_ICONS_URL,
2729
GITBOOK_USER_AGENT,

packages/gitbook-v2/src/app/~space/[spaceId]/pdf.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
fetchSpaceContextByIds,
55
} from '@v2/lib/context';
66
import { createDataFetcher } from '@v2/lib/data';
7-
import { GITBOOK_API_URL } from '@v2/lib/env';
87
import { createLinker } from '@v2/lib/links';
98
import { getAPITokenFromMiddleware } from '@v2/lib/middleware';
109

@@ -26,7 +25,6 @@ export async function getSpacePDFContext(
2625
});
2726
const dataFetcher = createDataFetcher({
2827
apiToken: apiToken,
29-
apiEndpoint: GITBOOK_API_URL,
3028
});
3129

3230
const baseContext: GitBookBaseContext = {

packages/gitbook-v2/src/lib/context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
import { type GitBookDataFetcher, createDataFetcher, throwIfDataError } from '@v2/lib/data';
1717
import { redirect } from 'next/navigation';
1818
import { assert } from 'ts-essentials';
19-
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_URL } from './env';
19+
import { GITBOOK_URL } from './env';
2020
import { type ImageResizer, createImageResizer } from './images';
2121
import { type GitBookSpaceLinker, createLinker } from './links';
2222

@@ -113,8 +113,7 @@ export function getBaseContext(input: {
113113
const urlMode = input.urlMode;
114114

115115
const dataFetcher = createDataFetcher({
116-
apiToken: input.apiToken ?? GITBOOK_API_TOKEN,
117-
apiEndpoint: GITBOOK_API_URL,
116+
apiToken: input.apiToken ?? null,
118117
});
119118

120119
const linker = getLinkerForSiteURL({

0 commit comments

Comments
 (0)