Compare commits

...

178 Commits

Author SHA1 Message Date
Samy Pessé b647538e41 Version Packages (#3033)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-04-01 16:35:09 +02:00
Samy Pessé e50797a5c1 Adapt new computed content and add tag for translation (#3070) 2025-04-01 15:10:55 +02:00
Nolann B. 5b1e01c786 Support for x-stability property (#3064) 2025-03-31 13:49:37 +02:00
Nolann B. 813b2af06b Support for x-enumDescriptions and x-gitbook-enum (#3066) 2025-03-31 12:18:57 +02:00
Nolann B. 6f71da88d8 Fix padding in OpenAPI schemas (#3067) 2025-03-31 12:03:53 +02:00
Samy Pessé fa91eb7eaf Fix dark mode PDF (#3065) 2025-03-28 18:00:33 +01:00
Zeno Kapitein c756761591 Add breadcrumbs to search results (#2772) 2025-03-28 13:29:05 +01:00
Samy Pessé 434af90366 Fix image resizing when using proxy by passing signing identifier in site URL data (#3063)
Co-authored-by: Taran Vohra <taranvohra@outlook.com>
2025-03-28 12:28:12 +01:00
Valentino Hudhra 0f41e194cb Fix dismissed annoucement banner ignored when switching sites (#3057) 2025-03-28 10:56:18 +01:00
Nolann B. bd353489e9 Fix missing alternative schemas (#3058) 2025-03-28 06:53:28 +01:00
Samy Pessé 77fd393f15 Fix click on search items when embedded in an iframe and track announcement links (#3061) 2025-03-27 20:13:14 +01:00
Taran Vohra 8701b5e078 Use the site canonical URL when rewriting in the middleware (#3060) 2025-03-27 19:39:39 +01:00
Nolann B. 61db166f92 Add OpenAPI write-only indicator (#3059) 2025-03-27 18:21:38 +01:00
Zeno Kapitein 0d9b9cea0f Fix announcement banner margin (#3056) 2025-03-27 15:59:47 +00:00
Nolann B. 7bb37c7f0a Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser (#3054) 2025-03-27 15:53:41 +01:00
Taran Vohra 5b2bf828e3 Use stable site URL data for route rewrite in the middleware (#3053) 2025-03-27 18:26:06 +05:30
Samy Pessé c85d3f9752 Serve a markdown version of the page when adding a .md extension (#3055)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-03-27 12:35:18 +00:00
Zeno Kapitein 4d7088bbf0 Stop variant selector from growing too large (#3043) 2025-03-27 10:42:18 +01:00
Samy Pessé dd6ab6044c Fix crash when using user mention or accessing invalid change requests (#3051) 2025-03-27 01:04:00 +01:00
Samy Pessé d236bf029c Fix flash for site with dark default mode (#3050) 2025-03-26 18:12:45 +01:00
Zeno Kapitein d70d566abe Support site announcement banner (#3008)
Co-authored-by: Valentino Hudhra <v.hudhra@gmail.com>
2025-03-26 16:37:05 +01:00
Nolann B. a25fdedea9 Replace $ref with $reference in json-decycle (#3049) 2025-03-26 14:16:03 +00:00
Nolann B. 373183a2cd Safe parse OpenAPI JSON schema (#3046) 2025-03-26 13:55:22 +01:00
Samy Pessé e9fa50dfb5 Trim search query before doing search (#3042) 2025-03-26 10:18:18 +00:00
Samy Pessé 57ca4e0e88 Fix crash when integration is not installed on the space (#3040) 2025-03-26 10:19:57 +01:00
Samy Pessé bc90adb73a Allow indexation of images to ensure favicons display on Google (#3041) 2025-03-26 10:17:57 +01:00
Nolann B. 1505ddbd5a Fix multiple request examples selector not showing (#3039) 2025-03-25 20:48:57 +01:00
Greg Bergé cd99ed57fb Fix OpenAPI spec rendering (#3037) 2025-03-25 15:13:18 +00:00
Samy Pessé 27de1cd01d Default to the first ID as being active in scroll detection (#3036) 2025-03-25 15:39:45 +01:00
Nolann B. ae78fc5394 Fix XML in code sample (#3030) 2025-03-25 15:11:45 +01:00
Samy Pessé 40e8e69c6d Disallow crawling by web-robots of search/ask URLs (#3035) 2025-03-25 13:51:09 +00:00
Nolann B. e84a46a558 Fix OpenAPI tabs indicator overflow (#3032) 2025-03-25 10:03:58 +01:00
Samy Pessé 18a72d4466 v2: improve caching (#3031) 2025-03-25 09:40:54 +01:00
Zeno Kapitein d7aa5fa003 Remove loading state for recommended questions (#3029) 2025-03-24 19:19:12 +01:00
Samy Pessé 27532a5f41 Version Packages (#2988)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-03-24 17:43:16 +01:00
Nolann B. 48c18c03b9 Add option to pass scalar plugins (#3027) 2025-03-24 14:11:14 +01:00
Samy Pessé bba2e52e24 Fix support for site redirects that include section/variant paths (#3024) 2025-03-24 10:21:26 +01:00
Valentino Hudhra 54ee0149e1 Add support for custom fonts (#2981) 2025-03-24 08:20:58 +01:00
Greg Bergé 8fda9d2fab Update Argos (#3025) 2025-03-22 23:00:25 +01:00
Samy Pessé 77a72d79ce Update next.js to latest v14 and v15 (#3026) 2025-03-22 20:02:15 +01:00
Nolann B. 73e2b472ef Fix write only properties in request example (#3023) 2025-03-21 19:09:25 +01:00
Samy Pessé 2f28cc1323 v2: improve link and navigation between content in a same site (#3020) 2025-03-21 18:18:34 +01:00
Greg Bergé f5516d4e21 Improve OpenAPI TOC display (#3022) 2025-03-21 14:29:12 +00:00
Greg Bergé d2facb2db1 Mark OpenAPI properties as optional if not required (#3021) 2025-03-21 14:28:53 +00:00
Greg Bergé 6eae764b7c Add support for multiple body content types / examples (#2991) 2025-03-21 14:23:42 +01:00
Greg Bergé 970ef8f886 Add missing token (#3019) 2025-03-21 11:04:57 +01:00
Nolann B. ed0720699e Fix OpenAPI path overflow on mobile (#3018) 2025-03-21 11:02:32 +01:00
Greg Bergé ad94d00d5f Improve markdown parsing (#3017) 2025-03-21 10:53:01 +01:00
Greg Bergé c7843f7f52 Remove bun cache (#3016) 2025-03-21 10:42:57 +01:00
Greg Bergé 1fe3286bdb Fix OpenAPI overflow issue (#3015) 2025-03-21 10:42:09 +01:00
Samy Pessé ce030fdf76 v2: Fix redirect to strip jwt_token in url-host mode (#3014) 2025-03-21 00:46:39 +01:00
Samy Pessé 9ba8783952 v2: Fix the redirection when stripping the jwt_token from url (#3013) 2025-03-21 00:25:34 +01:00
Greg Bergé 2f3966cc53 Fix icons flaky (#3012) 2025-03-20 21:08:46 +00:00
Samy Pessé 8f65a22e13 Accept API token in query parameters for normal v2 routes (#3011) 2025-03-20 19:54:55 +01:00
Greg Bergé 2787d884cd Stabilize images & icons (#3009) 2025-03-20 19:40:42 +01:00
Nolann B. fc00b51683 Remove default value in generateSchemaExample (#3007) 2025-03-20 17:24:23 +01:00
Samy Pessé f80c3a7b37 v2: Fix display of integration blocks (#3010) 2025-03-20 15:56:31 +00:00
Samy Pessé 4e5d6c7487 v2: use static route for VA / adaptive content (#2998) 2025-03-20 15:41:17 +01:00
Greg Bergé 96f84b5917 Fix visual tests v2 (#3006) 2025-03-20 11:34:04 +01:00
Greg Bergé 6b8a5fdd1b Trigger tests (#3005) 2025-03-20 10:00:09 +00:00
Greg Bergé 89083ac790 Stabilize visual tests (#3001) 2025-03-20 09:22:09 +01:00
Nolann B. 70be2c6b2b Stringify default value (#3003) 2025-03-19 21:28:40 +01:00
Samy Pessé bcf877841b Update to latest version of the GitBook API (#3002) 2025-03-19 20:11:44 +01:00
Nolann B. a84b06bb19 Update resolveDescription and add minItems/maxItems (#3000) 2025-03-19 17:21:35 +01:00
Zeno Kapitein 24b7808d9a Fix prominent search bar width on md screens (#2996) 2025-03-19 17:14:07 +01:00
Greg Bergé 8364ee988e Fix Argos flakies (#2999) 2025-03-19 16:04:45 +01:00
Samy Pessé 07f6e2d233 Pass page.computedSeed to getComputedDocument (#2995) 2025-03-19 15:02:49 +01:00
Greg Bergé 7212973f34 Update Scalar client (#2993) 2025-03-18 16:02:40 +01:00
Greg Bergé af44e0436b fix typo (#2992) 2025-03-18 14:20:23 +01:00
Greg Bergé 3f9c35d505 Fix emoji display on other platforms than Mac OS (#2990) 2025-03-17 17:49:13 +01:00
Zeno Kapitein 2c13f8b95c Fix horizontal overscroll issue on large screens (#2989) 2025-03-17 15:38:44 +01:00
Zeno Kapitein 4dab1c56e5 Fix alignment of prominent search bar on full-width pages (#2987) 2025-03-17 15:02:23 +01:00
Zeno Kapitein 4723f03ef1 Restyle section group dropdown (#2985) 2025-03-17 13:22:54 +01:00
Samy Pessé 278689e0a0 Version Packages (#2984)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-03-17 11:06:18 +01:00
Nolann B. 886e204e68 Update OpenAPI operation path design (#2972) 2025-03-17 10:18:24 +01:00
Greg Bergé c59947ab39 Remove Sentry (#2986) 2025-03-17 09:00:58 +01:00
Zeno Kapitein 4f0a772a21 Override tint lightness if supplied color is out of bounds (#2983) 2025-03-14 18:24:25 +01:00
Samy Pessé 903f962673 Version Packages (#2964)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-03-14 18:01:38 +01:00
Nolann B. 31d800e48b Changeset for OpenAPI schemas (#2982) 2025-03-14 17:08:21 +01:00
Nolann B. c1c58eb919 Render OpenAPISchemas block (#2959) 2025-03-14 16:24:09 +01:00
Zeno Kapitein eec3eed100 Support prominent search bar style (#2936)
Co-authored-by: Nolann Biron <biron.nolann@gmail.com>
2025-03-14 15:11:21 +01:00
Samy Pessé 99da8df3d1 Resize and optimize ogimage and favicons (#2980) 2025-03-13 18:45:14 +01:00
Steven H c9ea23919e Fix an issue where iframe aspect ratio was not respected. (#2979) 2025-03-13 14:23:50 +00:00
Zeno Kapitein 16292def79 Display sidebar on no-TOC pages (#2942) 2025-03-13 10:28:12 +01:00
Taran Vohra 9108c56ca1 Fix an issue where ContentKit webframes weren't sized properly (#2978) 2025-03-13 13:29:29 +05:30
Samy Pessé b011ea0af8 Simplify deferred codeblock highlighting and fix streaming (#2977) 2025-03-13 00:25:48 +01:00
spastorelli d33171b779 Fix getVisitorAuthCookieMaxAge returning 60s even when token expiry is much later (#2974) 2025-03-12 16:17:16 +01:00
Steven H 6aaeae269e Fix an issue where ContentKit iframes were not resizing. (#2967) 2025-03-12 14:52:27 +00:00
Samy Pessé e5f0cc8663 Respond with all tags of all cache used for page generation (#2973) 2025-03-12 10:30:47 +01:00
Nolann B. 9b5f971aa1 Transparent background for OpenAPI path block (#2963) 2025-03-11 13:32:35 +00:00
Nolann B. ff3b708ad3 Remove read-only properties from codesample (#2965) 2025-03-11 13:32:10 +00:00
Nolann B. e996904013 Update target in shiki javascript engine (#2968) 2025-03-11 14:21:07 +01:00
Samy Pessé f32bf1f15d Use API cache endpoint and service binding in Cloudflare (#2952) 2025-03-11 13:06:06 +01:00
Zeno Kapitein 6cda2dd57b Fix hint hover style (#2966) 2025-03-11 11:00:28 +00:00
Nolann B. c60e9ba27f Handle OpenAPI read-only properties (#2962) 2025-03-11 11:37:11 +01:00
Zeno Kapitein 9bc3d504c8 Info hint background and link color fixes (#2953) 2025-03-11 11:30:28 +01:00
Greg Bergé b5e4fd75e9 Upgrade shiki and bring back support to C/C++ (#2961) 2025-03-11 10:53:51 +01:00
Samy Pessé a92cb49445 Version Packages (#2958)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-03-11 09:38:39 +00:00
Greg Bergé 88f64b0d63 Support Node.js v20 with @gitbook/icons (#2960) 2025-03-11 09:27:26 +00:00
Nolann B. 844059f9a5 Fix spacing in OpenAPISecurities (#2954) 2025-03-11 09:37:34 +01:00
Nolann B. eaf2d68308 OpenAPI operation title fallback in sections (#2955) 2025-03-11 09:37:00 +01:00
Nolann B. 12bdbf988d Fix react-openapi tests (#2956) 2025-03-11 09:20:24 +01:00
Samy Pessé 29bab79005 v2: PDF export for sites and spaces (#2949) 2025-03-10 00:08:33 +01:00
Samy Pessé 8d5de97ddf v2: fix fallback and VA redirects (#2948) 2025-03-08 11:21:49 +01:00
Samy Pessé d9b5a898ab Version Packages (#2947)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-08 00:29:16 +01:00
Nolann B. f127d28675 Rename OpenAPIModels to OpenAPISchemas (#2946) 2025-03-07 22:28:37 +01:00
Samy Pessé 65c1bdc455 Version Packages (#2945)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-07 19:37:41 +01:00
Samy Pessé 0711a171d4 Fix deployment of 2c to production 2025-03-07 18:26:51 +01:00
Samy Pessé c58c551f9e Simplify envs management for deployments (#2943) 2025-03-07 18:17:44 +01:00
Greg Bergé f574858b9b Fix OpenAPI example (#2944) 2025-03-07 17:54:35 +01:00
Samy Pessé 3e1592e16b Fix configuration for v2 staging (#2940) 2025-03-07 14:46:45 +01:00
Samy Pessé 90604c4c3b Version Packages (#2917)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-07 14:03:59 +01:00
Claire Chabas 9ffc3b606d Fix content overflowing out of their container in lists (#2939) 2025-03-07 11:19:02 +01:00
Samy Pessé e00c87d3e6 Output a x-gitbook-route-type header to distinguish static/dynamic (#2938) 2025-03-07 10:29:50 +01:00
Nolann B. bb3ca9c165 Implement OpenAPI models blocks (#2908) 2025-03-07 09:44:14 +01:00
Samy Pessé 8ee9757132 v2: cleanup deployment to Cloudflare (#2937) 2025-03-07 09:15:43 +01:00
Samy Pessé 946a463979 v2: Fix deployment to Cloudflare and improve CI (#2935) 2025-03-07 00:01:07 +01:00
Samy Pessé 7aad9f7ff8 v2: render code block with syntax highlighting on the server (#2932) 2025-03-06 15:07:06 +01:00
Greg Bergé 3fe8f7c219 Fix circular refs OpenAPI (#2934) 2025-03-06 10:32:49 +00:00
Greg Bergé 0278a14e8e Upgrade Scalar client & fix opening endpoint (#2933) 2025-03-06 11:28:51 +01:00
Samy Pessé e5366d3a49 v2: fix error handling in middleware and data fetcher (#2925) 2025-03-06 10:53:17 +01:00
Samy Pessé 38e84ffe1a Persist visitor auth token in cookie for as long as the token is valid (#2931) 2025-03-06 09:45:25 +01:00
Greg Bergé 052e07a936 Support references pointing to invalid files (#2930) 2025-03-05 21:37:46 +01:00
Samy Pessé 7e67ebc11d Fix condition for public content cache (#2929) 2025-03-05 21:22:22 +01:00
Samy Pessé c9dea94717 V2: Add back redirect for trailing slash (#2928) 2025-03-05 21:06:33 +01:00
Samy Pessé 1344754ca9 v2: enable skipTrailingSlashRedirect in the middleware 2025-03-05 19:27:48 +01:00
Samy Pessé 0f4759fb89 Fix condition for VERCEL_URL 2025-03-05 16:30:59 +01:00
Samy Pessé 6d77b647a4 Skip requests to assets/main/vercel hosts in middleware (#2927) 2025-03-05 16:22:20 +01:00
Zeno Kapitein 5907bd9af4 Adjust hint spacing (#2873) 2025-03-05 15:59:58 +01:00
Samy Pessé b66d04c9c5 v2: fix resolution of revalidation requests (#2926) 2025-03-05 14:48:07 +01:00
Nolann B. 3173d8ec2d Remove top level circular refs in alternatives (#2924) 2025-03-05 14:08:34 +01:00
Samy Pessé 31e381035e Dynamic routing for v2: customization preview and visitor auth (#2922) 2025-03-05 11:25:44 +01:00
Greg Bergé 259074b81f Fix OpenAPI spec recursion (#2920) 2025-03-05 09:25:23 +01:00
Samy Pessé 9a2f5b9295 Fix host used for image resizing in v2 (#2923) 2025-03-04 23:42:15 +01:00
Greg Bergé 58cd70b0c1 Put back logs (#2919) 2025-03-04 20:33:29 +00:00
Samy Pessé 821899a767 Fix image resizing in v2 when url is being passed as x-gitbook-url (#2921) 2025-03-04 18:57:49 +01:00
Samy Pessé bfcc119ff3 Remove support for x-gitbook-host in v2 (#2918) 2025-03-04 18:36:43 +01:00
Samy Pessé 76c797498e Implement route to invalidate data cache (#2916) 2025-03-04 13:52:02 +01:00
Samy Pessé 13514bf6fa Version Packages (#2907)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-04 13:42:32 +01:00
Samy Pessé 05ffd0e321 Proper data cache tagging for computed content (#2915) 2025-03-04 12:26:09 +01:00
Samy Pessé 701eaad92a Fix use of server actions with proxied domains by using middleware (#2913) 2025-03-04 10:00:08 +01:00
Greg Bergé c66ef5dd10 Fix OpenAPI tabs selection (#2914) 2025-03-03 21:10:24 +00:00
Greg Bergé f774f9b3a0 Fix all tabs behaviour in OpenAPI blocks (#2912) 2025-03-03 17:11:54 +00:00
Samy Pessé 29dc7ccb1a Fix next.js configuration for serverActions 2025-03-03 17:05:46 +01:00
Samy Pessé 80237c34f9 Allow all origins (including sub-domains) for server actions 2025-03-03 16:54:16 +01:00
Samy Pessé 979233c5c0 Allow all origins for serverActions in v2 2025-03-03 16:21:05 +01:00
Samy Pessé 2f03e6acee Define GITBOOK_URL as serverActions.allowedOrigins (#2911) 2025-03-03 15:24:31 +01:00
Greg Bergé c492f09a19 Fix outline not visible on OpenAPI blocks (#2910) 2025-03-03 14:56:19 +01:00
Greg Bergé 2c3af5e73c Improve scrolling with lot of highlighted blocks (#2909) 2025-03-03 13:47:30 +00:00
Samy Pessé 6597f8508d V2: search and ask AI (#2889) 2025-03-03 11:53:32 +01:00
Nolann B. 8beb5d6bcf Add input elements to ContentKit (#2882) 2025-02-28 17:50:20 +00:00
Greg Bergé 53f5dbea26 Fix typing of shouldIgnoreEntity (#2906) 2025-02-28 17:38:58 +00:00
Samy Pessé 725952a106 Version Packages (#2881)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-02-28 17:22:32 +00:00
Greg Bergé 3319375e9a Support OpenAPI operation block (#2902) 2025-02-28 18:12:08 +01:00
spastorelli c5a4619020 Fix multi-id/preview mode not using context id for api cache calls (#2905) 2025-02-28 18:04:16 +01:00
Greg Bergé 989fc69abe Format package.json after running changeset (#2904) 2025-02-28 15:56:44 +00:00
Greg Bergé 0924259217 Expose OpenAPI helper shouldIgnoreEntity (#2903) 2025-02-28 16:22:20 +01:00
Taran Vohra 05affac34f Add support for proxy sites using x-gitbook-url in v2 (#2901) 2025-02-28 20:03:05 +05:30
Samy Pessé 453a459566 Fix content ref resolution for pages of other spaces (#2900) 2025-02-28 13:00:54 +01:00
Nolann B. 722f02ea09 Fix recursion in OpenAPISchemaAlternative (#2892) 2025-02-28 10:07:56 +00:00
Samy Pessé 5bcea2fe3b Fix signature verification with image resizing on v1 (#2899) 2025-02-28 10:36:10 +01:00
Samy Pessé 73afd1f5d0 Improve error message for image resizing (#2898) 2025-02-28 10:23:17 +01:00
Greg Bergé 9b8a0d3e54 Fix CI tests (#2897) 2025-02-28 10:14:09 +01:00
Samy Pessé 75bd98d66c V2: robots.txt, sitemap.xml, image resizing, etc (#2895) 2025-02-28 09:52:56 +01:00
Taran Vohra 9b914d10bd Fix getProxyModeBasePath (#2896) 2025-02-28 12:27:25 +05:30
Brett Jephson 3e11678d8d fix: section groups went missing (#2891) 2025-02-28 00:00:39 +01:00
Samy Pessé 6e03638130 Remove loading.tsx for static rendering in v2 (#2894) 2025-02-27 18:23:06 +01:00
Greg Bergé 23f884efb7 Fix the fonts display on the site (#2893) 2025-02-27 17:27:17 +01:00
Taran Vohra 2ae76f999b Change how a site in proxy mode is resolved (#2890) 2025-02-27 21:43:29 +05:30
Samy Pessé 685325cdc7 Deploy v2 to staging from main using a composite action (#2888) 2025-02-27 11:12:02 +01:00
Samy Pessé ab132eadb7 Cache OpenAPI using next "use cache" for v2 (#2887) 2025-02-27 09:39:08 +01:00
Samy Pessé 14504e43b5 V2: embeds and CSP (#2885) 2025-02-27 08:44:32 +01:00
Taran Vohra 4ed79574e8 change proxy mode site lookup url (#2886) 2025-02-27 12:38:55 +05:30
Samy Pessé 31bcd1a386 Improve detection of code path not supported in v2, add visual tests (#2884) 2025-02-26 20:18:38 +01:00
Samy Pessé 270e5ef7ad Improve linking to other sections/variants in development (#2883) 2025-02-26 19:14:27 +01:00
Samy Pessé 1f4733355e Remove eslint/prettier and switch to biome (#2880) 2025-02-26 17:59:34 +01:00
Zeno Kapitein 027a859ef4 Add link styles (#2861) 2025-02-26 13:03:14 +00:00
Samy Pessé 59f50eaee7 Fix production deployments to Vercel (#2879) 2025-02-26 13:47:24 +01:00
491 changed files with 14840 additions and 9513 deletions
-1
View File
@@ -1 +0,0 @@
1.2.1
@@ -0,0 +1,72 @@
name: 'Deploy cloudflare'
description: 'Deploy GitBook to Cloudflare'
inputs:
opItem:
description: '1Password item to load secrets from'
required: true
opServiceAccount:
description: '1Password service account token'
required: true
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
deploy:
description: 'Deploy as main version for all traffic instead of uploading versions'
required: true
outputs:
deployment-url:
description: "Deployment URL"
value: ${{ steps.deploy.outputs.deployment-url }}
runs:
using: 'composite'
steps:
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
shell: bash
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Load secret
uses: 1password/load-secrets-action@v2
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ inputs.opServiceAccount }}
GITBOOK_URL: ${{ inputs.opItem }}/GITBOOK_URL
GITBOOK_ICONS_URL: ${{ inputs.opItem }}/GITBOOK_ICONS_URL
GITBOOK_ICONS_TOKEN: ${{ inputs.opItem }}/GITBOOK_ICONS_TOKEN
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ inputs.opItem }}/NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
GITBOOK_FONTS_URL: ${{ inputs.opItem }}/GITBOOK_FONTS_URL
- name: Build worker
run: bun run turbo build:v2:cloudflare
shell: bash
- id: deploy
name: Deploy to Cloudflare
uses: cloudflare/wrangler-action@v3.14.0
with:
apiToken: ${{ inputs.apiToken }}
accountId: ${{ inputs.accountId }}
workingDirectory: ./
wranglerVersion: '3.112.0'
environment: ${{ inputs.environment }}
command: ${{ fromJSON(inputs.deploy) == true && 'deploy' || 'versions upload' }} --config ./packages/gitbook-v2/wrangler.toml
- name: Outputs
shell: bash
env:
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
@@ -0,0 +1,78 @@
name: 'Deploy vercel'
description: 'Deploy GitBook to Vercel'
inputs:
vercelOrg:
description: 'Vercel organization'
required: true
vercelProject:
description: 'Vercel project'
required: true
vercelToken:
description: 'Vercel token'
required: true
opItem:
description: '1Password item to load secrets from'
required: true
opServiceAccount:
description: '1Password service account token'
required: true
environment:
description: 'Environment to deploy to'
required: true
outputs:
deployment-url:
description: "Deployment URL"
value: ${{ steps.deploy.outputs.deployment-url }}
runs:
using: 'composite'
steps:
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
shell: bash
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Pull Vercel Environment Information
run: bun run vercel pull --yes --environment=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
shell: bash
env:
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
- name: Load secret
uses: 1password/load-secrets-action@v2
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ inputs.opServiceAccount }}
GITBOOK_URL: ${{ inputs.opItem }}/GITBOOK_URL
GITBOOK_ICONS_URL: ${{ inputs.opItem }}/GITBOOK_ICONS_URL
GITBOOK_ICONS_TOKEN: ${{ inputs.opItem }}/GITBOOK_ICONS_TOKEN
GITBOOK_SECRET: ${{ inputs.opItem }}/GITBOOK_SECRET
GITBOOK_APP_URL: ${{ inputs.opItem }}/GITBOOK_APP_URL
GITBOOK_API_URL: ${{ inputs.opItem }}/GITBOOK_API_URL
GITBOOK_API_PUBLIC_URL: ${{ inputs.opItem }}/GITBOOK_API_PUBLIC_URL
GITBOOK_API_TOKEN: ${{ inputs.opItem }}/GITBOOK_API_TOKEN
GITBOOK_INTEGRATIONS_HOST: ${{ inputs.opItem }}/GITBOOK_INTEGRATIONS_HOST
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_SIGNING_KEY
GITBOOK_IMAGE_RESIZE_URL: ${{ inputs.opItem }}/GITBOOK_IMAGE_RESIZE_URL
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
GITBOOK_FONTS_URL: ${{ inputs.opItem }}/GITBOOK_FONTS_URL
- name: Build Project Artifacts
run: bun run vercel build --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }}
shell: bash
env:
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
- name: Deploy Project Artifacts to Vercel
id: deploy
shell: bash
run: |
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --target=${{ inputs.environment }} --token=${{ inputs.vercelToken }})
echo "deployment-url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
env:
VERCEL_ORG_ID: ${{ inputs.vercelOrg }}
VERCEL_PROJECT_ID: ${{ inputs.vercelProject }}
- name: Outputs
shell: bash
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
+1 -10
View File
@@ -3,16 +3,7 @@ description: 'Install Bun and cache dependencies'
runs: runs:
using: 'composite' using: 'composite'
steps: steps:
# We cache dependencies even if Bun is fast to avoid downloading fontawesome too many times
# and impacting bandwidth there.
- name: Cache bun
uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}-${{ hashFiles('**/bun.lock') }}
restore-keys: |
${{ runner.os }}-bun-
- name: Setup bun - name: Setup bun
uses: oven-sh/setup-bun@v2 uses: oven-sh/setup-bun@v2
with: with:
bun-version-file: '.bun-version' bun-version-file: 'package.json'
+4 -304
View File
@@ -7,300 +7,10 @@ on:
env: env:
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }} NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
jobs: jobs:
deploy:
name: Deploy to Cloudflare Pages
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
issues: write
pull-requests: write
checks: write
statuses: write
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
deployment-alias-url: ${{ steps.deploy.outputs.deployment-alias-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Sets env vars for production
run: |
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
echo "GITBOOK_ASSETS_PREFIX=https://static.gitbook.com" >> $GITHUB_ENV
if: startsWith(github.ref, 'refs/heads/main')
- name: Sets env vars for preview
run: |
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
if: 1 && !startsWith(github.ref, 'refs/heads/main')
- name: Build Next.js with next-on-pages
run: bun run turbo gitbook#build:cloudflare
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
SENTRY_DSN: ${{ vars.SENTRY_DSN }}
SENTRY_RELEASE: ${{ github.sha }}
- id: deploy
name: Deploy to Cloudflare
uses: cloudflare/wrangler-action@v3.14.0
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
workingDirectory: ./
wranglerVersion: '3.82.0'
command: pages deploy ./packages/gitbook/.vercel/output/static --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.ref == 'refs/heads/main' && 'main' || format('pr{0}', github.event.pull_request.number) }}
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
echo "Alias URL: ${{ steps.deploy.outputs.deployment-alias-url }}"
- name: Archive build output
uses: actions/upload-artifact@v4
with:
name: build-output
path: .vercel/
# Until https://github.com/cloudflare/wrangler-action/issues/301 is done
- name: Update Deployment Status to Success
env:
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
run: |
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
-d '{"state": "success", "target_url": "${{ steps.deploy.outputs.deployment-url }}", "description": "Deployed Preview URL for commit", "context": "cloudflare/preview"}' \
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }}
deploy-v2-vercel:
name: Deploy v2 to Vercel
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
issues: write
pull-requests: write
checks: write
statuses: write
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Sets env vars for production
run: |
echo "VERCEL_ENVIRONMENT=production" >> $GITHUB_ENV
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
echo "GITBOOK_URL=https://open-2v.gitbook.com" >> $GITHUB_ENV
echo "GITBOOK_ASSETS_PREFIX=https://static-2v.gitbook.com" >> $GITHUB_ENV
if: startsWith(github.ref, 'refs/heads/main')
- name: Sets env vars for preview
run: |
echo "VERCEL_ENVIRONMENT=preview" >> $GITHUB_ENV
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
if: 1 && !startsWith(github.ref, 'refs/heads/main')
- name: Pull Vercel Environment Information
run: bun run vercel pull --yes --environment=$VERCEL_ENVIRONMENT --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: bun run vercel build --token=${{ secrets.VERCEL_TOKEN }}
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
SENTRY_DSN: ${{ vars.SENTRY_DSN }}
SENTRY_RELEASE: ${{ github.sha }}
- name: Deploy Project Artifacts to Vercel
id: deploy
run: |
DEPLOYMENT_URL=$(bun run vercel deploy --prebuilt --target=$VERCEL_ENVIRONMENT $([ "$VERCEL_ENVIRONMENT" = "production" ] && echo "--prod") --token=${{ secrets.VERCEL_TOKEN }})
echo "deployment-url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
deploy-v2-cloudflare:
name: Deploy v2 to Cloudflare Worker
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
issues: write
pull-requests: write
checks: write
statuses: write
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Sets env vars for production
run: |
echo "SENTRY_ENVIRONMENT=production" >> $GITHUB_ENV
echo "GITBOOK_URL=https://open-2c.gitbook.com" >> $GITHUB_ENV
echo "GITBOOK_ASSETS_PREFIX=https://static-2c.gitbook.com" >> $GITHUB_ENV
if: startsWith(github.ref, 'refs/heads/main')
- name: Sets env vars for preview
run: |
echo "SENTRY_ENVIRONMENT=preview" >> $GITHUB_ENV
if: 1 && !startsWith(github.ref, 'refs/heads/main')
- name: Build worker
run: bun run turbo build:v2:cloudflare
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
SENTRY_DSN: ${{ vars.SENTRY_DSN }}
SENTRY_RELEASE: ${{ github.sha }}
- id: deploy
name: Deploy to Cloudflare
uses: cloudflare/wrangler-action@v3.14.0
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
workingDirectory: ./
wranglerVersion: '3.82.0'
command: versions upload --config ./packages/gitbook-v2/wrangler.toml
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
comment-deployments:
runs-on: ubuntu-latest
name: Comment Deployments
if: always() && !startsWith(github.ref, 'refs/heads/main')
needs:
- deploy
- deploy-v2-vercel
- deploy-v2-cloudflare
steps:
- name: Find GitHub Comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'Summary of the deployments'
- name: Create or update GitHub comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Summary of the deployments:
### Version 1 (production)
| Version | URL | Status |
| --- | --- | --- |
| Latest commit | [${{ needs.deploy.outputs.deployment-url }}](${{ needs.deploy.outputs.deployment-url }}) | ${{ needs.deploy.result == 'success' && '✅' || '❌' }} |
| PR | [${{ needs.deploy.outputs.deployment-alias-url }}](${{ needs.deploy.outputs.deployment-alias-url }}) | ${{ needs.deploy.result == 'success' && '✅' || '❌' }} |
### Version 2 (experimental)
| Version | URL | Status |
| --- | --- | --- |
| Vercel | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}](${{ needs.deploy-v2-vercel.outputs.deployment-url }}) | ${{ needs.deploy-v2-vercel.result == 'success' && '✅' || '❌' }} |
| Cloudflare | [${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}](${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}) | ${{ needs.deploy-v2-cloudflare.result == 'success' && '✅' || '❌' }} |
### Test content
| Site | v1 | v2 |
| --- | --- | --- |
| GitBook | [${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com](${{ needs.deploy.outputs.deployment-url }}/docs.gitbook.com) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com) |
| E2E | [${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) |
edit-mode: replace
visual-testing:
runs-on: ubuntu-latest
name: Visual Testing
needs: deploy
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e
env:
BASE_URL: ${{needs.deploy.outputs.deployment-url}}
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-test-results
path: packages/gitbook/test-results/
retention-days: 3
visual-testing-customers:
runs-on: ubuntu-latest
name: Visual Testing Customers
needs: deploy
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e-customers
env:
BASE_URL: ${{needs.deploy.outputs.deployment-url}}
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
ARGOS_BUILD_NAME: 'customers'
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-test-results-customers
path: packages/gitbook/test-results/
retention-days: 3
pagespeed-testing:
runs-on: ubuntu-latest
name: PageSpeed Testing
needs: deploy
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Run pagespeed tests
run: bun ./packages/gitbook/tests/pagespeed-testing.ts $DEPLOYMENT_URL
env:
DEPLOYMENT_URL: ${{needs.deploy.outputs.deployment-url}}
PAGESPEED_API_KEY: ${{ secrets.PAGESPEED_API_KEY }}
format: format:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Format name: Format
timeout-minutes: 6
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -311,22 +21,10 @@ jobs:
env: env:
PUPPETEER_SKIP_DOWNLOAD: 1 PUPPETEER_SKIP_DOWNLOAD: 1
- run: bun format:check - run: bun format:check
lint:
runs-on: ubuntu-latest
name: Lint
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- run: bun lint --no-cache
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Test name: Test
timeout-minutes: 6
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -341,6 +39,7 @@ jobs:
# CI to check that the repository builds correctly on a machine without the credentials # CI to check that the repository builds correctly on a machine without the credentials
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Build (Open Source) name: Build (Open Source)
timeout-minutes: 6
env: env:
NPM_TOKEN_READONLY: '' NPM_TOKEN_READONLY: ''
steps: steps:
@@ -356,6 +55,7 @@ jobs:
typecheck: typecheck:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Typecheck name: Typecheck
timeout-minutes: 6
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
+245
View File
@@ -0,0 +1,245 @@
name: Preview
on:
pull_request:
push:
branches:
- main
env:
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
jobs:
deploy-v1-cloudflare:
name: Deploy v1 to Cloudflare Pages
runs-on: ubuntu-latest
environment:
name: ${{ github.ref == 'refs/heads/main' && '1c-production' || '1c-preview' }}
url: ${{ steps.deploy.outputs.deployment-url }}
permissions:
contents: read
deployments: write
issues: write
pull-requests: write
checks: write
statuses: write
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
deployment-alias-url: ${{ steps.deploy.outputs.deployment-alias-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Sets env vars for production
if: github.ref == 'refs/heads/main'
run: |
echo "GITBOOK_ASSETS_PREFIX=https://static.gitbook.com" >> $GITHUB_ENV
- name: Build Next.js with next-on-pages
run: bun run turbo gitbook#build:cloudflare
env:
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: ${{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}
- id: deploy
name: Deploy to Cloudflare
uses: cloudflare/wrangler-action@v3.14.0
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
workingDirectory: ./
wranglerVersion: '3.112.0'
command: pages deploy ./packages/gitbook/.vercel/output/static --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --branch=${{ github.ref == 'refs/heads/main' && 'main' || format('pr{0}', github.event.pull_request.number) }}
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
echo "Alias URL: ${{ steps.deploy.outputs.deployment-alias-url }}"
deploy-v2-vercel:
name: Deploy v2 to Vercel (preview)
runs-on: ubuntu-latest
environment:
name: 2v-preview
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy to Vercel
id: deploy
uses: ./.github/composite/deploy-vercel
with:
environment: preview
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
vercelToken: ${{ secrets.VERCEL_TOKEN }}
opItem: op://gitbook-open/2v-preview
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
deploy-v2-cloudflare:
name: Deploy v2 to Cloudflare Worker (preview)
runs-on: ubuntu-latest
environment:
name: 2c-preview
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy to Cloudflare
id: deploy
uses: ./.github/composite/deploy-cloudflare
with:
environment: preview
deploy: ${{ github.ref == 'refs/heads/main' }}
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
opItem: op://gitbook-open/2c-preview
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
comment-deployments:
runs-on: ubuntu-latest
name: Comment Deployments (preview)
if: always() && !startsWith(github.ref, 'refs/heads/main')
needs:
- deploy-v1-cloudflare
- deploy-v2-vercel
- deploy-v2-cloudflare
steps:
- name: Find GitHub Comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'Summary of the deployments'
- name: Create or update GitHub comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Summary of the deployments:
### Version 1 (production)
| Version | URL | Status |
| --- | --- | --- |
| Latest commit | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}) | ${{ needs.deploy-v1-cloudflare.result == 'success' && '✅' || '❌' }} |
| PR | [${{ needs.deploy-v1-cloudflare.outputs.deployment-alias-url }}](${{ needs.deploy-v1-cloudflare.outputs.deployment-alias-url }}) | ${{ needs.deploy-v1-cloudflare.result == 'success' && '✅' || '❌' }} |
### Version 2 (experimental)
| Version | URL | Status |
| --- | --- | --- |
| Vercel | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}](${{ needs.deploy-v2-vercel.outputs.deployment-url }}) | ${{ needs.deploy-v2-vercel.result == 'success' && '✅' || '❌' }} |
| Cloudflare | [${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}](${{ needs.deploy-v2-cloudflare.outputs.deployment-url }}) | ${{ needs.deploy-v2-cloudflare.result == 'success' && '✅' || '❌' }} |
### Test content
| Site | v1 | v2 |
| --- | --- | --- |
| GitBook | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/docs.gitbook.com](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/docs.gitbook.com) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/docs.gitbook.com) |
| E2E | [${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}/gitbook.gitbook.io/test-gitbook-open) | [${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open](${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/gitbook.gitbook.io/test-gitbook-open) |
edit-mode: replace
visual-testing-v1:
runs-on: ubuntu-latest
name: Visual Testing v1
needs: deploy-v1-cloudflare
timeout-minutes: 8
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e
env:
BASE_URL: ${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
visual-testing-v2-vercel:
runs-on: ubuntu-latest
name: Visual Testing v2
needs: deploy-v2-vercel
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e
env:
BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
ARGOS_BUILD_NAME: 'v2-vercel'
visual-testing-customers-v1:
runs-on: ubuntu-latest
name: Visual Testing Customers v1
needs: deploy-v1-cloudflare
timeout-minutes: 6
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e-customers
env:
BASE_URL: ${{ needs.deploy-v1-cloudflare.outputs.deployment-url }}
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
ARGOS_BUILD_NAME: 'customers-v1'
visual-testing-customers-v2:
runs-on: ubuntu-latest
name: Visual Testing Customers v2
needs: deploy-v2-vercel
timeout-minutes: 6
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Run Playwright tests
run: bun e2e-customers
env:
BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}
SITE_BASE_URL: ${{ needs.deploy-v2-vercel.outputs.deployment-url }}/url/
ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }}
ARGOS_BUILD_NAME: 'customers-v2'
pagespeed-testing-v1:
runs-on: ubuntu-latest
name: PageSpeed Testing v1
needs: deploy-v1-cloudflare
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: ./.github/composite/setup-bun
- name: Install dependencies
run: bun install --frozen-lockfile
env:
PUPPETEER_SKIP_DOWNLOAD: 1
- name: Run pagespeed tests
run: bun ./packages/gitbook/tests/pagespeed-testing.ts
env:
BASE_URL: ${{needs.deploy-v1-cloudflare.outputs.deployment-url}}
PAGESPEED_API_KEY: ${{ secrets.PAGESPEED_API_KEY }}
+53
View File
@@ -0,0 +1,53 @@
name: Production
on:
push:
branches:
- main
env:
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
jobs:
deploy-v2-vercel:
name: Deploy v2 to Vercel (production)
runs-on: ubuntu-latest
environment:
name: 2v-production
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy staging
id: deploy
uses: ./.github/composite/deploy-vercel
with:
environment: production
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
vercelToken: ${{ secrets.VERCEL_TOKEN }}
opItem: op://gitbook-open/2v-production
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
deploy-v2-cloudflare:
name: Deploy v2 to Cloudflare Worker (production)
runs-on: ubuntu-latest
environment:
name: 2c-production
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy
id: deploy
uses: ./.github/composite/deploy-cloudflare
with:
environment: production
deploy: true
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
opItem: op://gitbook-open/2c-production
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
+53
View File
@@ -0,0 +1,53 @@
name: Staging
on:
push:
branches:
- main
env:
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
jobs:
deploy-v2-vercel:
name: Deploy v2 to Vercel (staging)
runs-on: ubuntu-latest
environment:
name: 2v-staging
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy staging
id: deploy
uses: ./.github/composite/deploy-vercel
with:
environment: staging
vercelOrg: ${{ secrets.VERCEL_ORG_ID }}
vercelProject: ${{ secrets.VERCEL_PROJECT_ID }}
vercelToken: ${{ secrets.VERCEL_TOKEN }}
opItem: op://gitbook-open/2v-staging
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
deploy-v2-cloudflare:
name: Deploy v2 to Cloudflare Worker (staging)
runs-on: ubuntu-latest
environment:
name: 2c-staging
url: ${{ steps.deploy.outputs.deployment-url }}
outputs:
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Deploy
id: deploy
uses: ./.github/composite/deploy-cloudflare
with:
environment: staging
deploy: true
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
opItem: op://gitbook-open/2c-staging
opServiceAccount: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
- name: Outputs
run: |
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
+4
View File
@@ -5,6 +5,9 @@ on:
branches: branches:
- main - main
env:
NPM_TOKEN_READONLY: ${{ secrets.NPM_TOKEN_READONLY }}
concurrency: ${{ github.workflow }}-${{ github.ref }} concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs: jobs:
@@ -28,6 +31,7 @@ jobs:
uses: changesets/action@v1 uses: changesets/action@v1
with: with:
publish: npm run release publish: npm run release
version: npm run changeset-version
env: env:
# Using a PAT instead of GITHUB_TOKEN because we need to run workflows when releases are created # Using a PAT instead of GITHUB_TOKEN because we need to run workflows when releases are created
# https://github.com/orgs/community/discussions/26875#discussioncomment-3253761 # https://github.com/orgs/community/discussions/26875#discussioncomment-3253761
-17
View File
@@ -1,17 +0,0 @@
.next
.vercel
.open-next
# Generated
packages/emoji-codepoints/index.ts
packages/gitbook/public/~gitbook/static/
packages/icons/src/data/*.json
# Build files
dist/
README.md
CHANGELOG.md
# v2
packages/gitbook-v2/public/~gitbook/static/
-5
View File
@@ -1,5 +0,0 @@
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4
}
+3
View File
@@ -0,0 +1,3 @@
{
"recommendations": ["biomejs.biome"]
}
+10 -1
View File
@@ -7,5 +7,14 @@
["style \\=([^;]*);", "\"([^\"]*)\""], ["style \\=([^;]*);", "\"([^\"]*)\""],
["style \\=([^;]*);", "\\`([^\\`]*)\\`"] ["style \\=([^;]*);", "\\`([^\\`]*)\\`"]
], ],
"tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"] "tailwindCSS.classAttributes": ["class", "className", "style", ".*Style"],
"prettier.enable": false,
"editor.defaultFormatter": "biomejs.biome",
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "explicit",
"source.fixAll.biome": "explicit"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
} }
+175
View File
@@ -0,0 +1,175 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": [
"**/node_modules/**/*",
"**/dist/**/*",
"**/build/**/*",
"**/public/**/*",
"**/.next/**/*",
"**/.open-next/**/*",
"**/.turbo/**/*",
"**/.vercel/**/*",
"**/.cache/**/*",
"**/.wrangler/**/*",
"packages/openapi-parser/src/fixtures/**/*",
"packages/emoji-codepoints/index.ts",
"packages/icons/src/data/*.json"
]
},
"formatter": {
"enabled": true,
"useEditorconfig": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 4,
"lineEnding": "lf",
"lineWidth": 100,
"attributePosition": "auto",
"bracketSpacing": true
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"performance": {
"noDelete": "warn"
},
"security": {
"noDangerouslySetInnerHtml": "off"
},
"complexity": {
"noForEach": "off",
"noUselessFragments": "warn",
"noBannedTypes": "warn"
},
"correctness": {
"noUndeclaredVariables": "error",
"noUnusedVariables": "error",
"useArrayLiterals": "error",
"useHookAtTopLevel": "error",
"noUnusedImports": "error",
"noVoidElementsWithChildren": "warn",
"useJsxKeyInIterable": "warn",
"useExhaustiveDependencies": "warn",
"noUnknownFunction": "warn"
},
"style": {
"noNonNullAssertion": "warn",
"noParameterAssign": "off",
"useThrowOnlyError": "error"
},
"suspicious": {
"noConsole": {
"level": "warn",
"options": {
"allow": ["assert", "error", "warn"]
}
},
"noExplicitAny": "warn",
"noImplicitAnyLet": "warn",
"noConfusingVoidType": "warn",
"noControlCharactersInRegex": "warn",
"noPrototypeBuiltins": "warn",
"noAssignInExpressions": "warn",
"noArrayIndexKey": "warn"
},
"a11y": {
"useSemanticElements": "warn",
"useKeyWithClickEvents": "warn",
"noSvgWithoutTitle": "warn",
"useButtonType": "warn",
"useIframeTitle": "warn",
"useAltText": "warn",
"noPositiveTabindex": "warn",
"useFocusableInteractive": "warn",
"useAriaPropsForRole": "warn",
"useValidAnchor": "warn",
"noLabelWithoutControl": "warn",
"noNoninteractiveTabindex": "warn"
},
"nursery": {
"useSortedClasses": {
"level": "error",
"fix": "safe",
"options": {
"attributes": ["class", "className", "style"],
"functions": ["clsx", "tw"]
}
}
}
}
},
"javascript": {
"formatter": {
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingCommas": "es5",
"semicolons": "always",
"arrowParentheses": "always",
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto",
"bracketSpacing": true
}
},
"overrides": [
{
"include": [
"packages/gitbook/**/*",
"packages/gitbook-v2/**/*",
"packages/react-openapi/**/*",
"packages/react-math/**/*",
"packages/react-contentkit/**/*",
"packages/icons/**/*"
],
"javascript": {
"globals": ["React"]
}
},
{
"include": ["packages/gitbook/**/*"],
"javascript": {
"globals": ["React", "GitBookIntegrationEvent"]
}
},
{
"include": ["*.css"],
"javascript": {
"globals": ["theme"]
}
},
{
"include": ["*.test.ts", "packages/gitbook/tests/**/*"],
"javascript": {
"globals": ["Bun"]
}
},
{
"include": [
"packages/cache-do/**/*",
"packages/gitbook/cf-env.d.ts",
"packages/gitbook/src/cloudflare-entrypoint.ts"
],
"javascript": {
"globals": [
"DurableObjectLocationHint",
"DurableObjectNamespace",
"DurableObjectStub",
"ContinentCode",
"Fetcher",
"ExportedHandler"
]
}
}
]
}
+360 -1079
View File
File diff suppressed because it is too large Load Diff
-22
View File
@@ -1,22 +0,0 @@
# Caching
## Revalidating the cache
Invalidate cache can be done at two levels using tags:
- Data fetching cache
- Rendering cache
To invalidate and refetch the data cache, you can execute a POST request to `/~/gitbook/revalidate`:
```bash
curl --location --request POST 'https://gitbook/mycompany.com/~gitbook/revalidate' \
--header 'Content-Type: application/json' \
--data-raw '{"tags": ["space.id"]}'
```
To invalidate the rendering cache, the implementation mainly depends on the infrastructure serving the content, GitBook outputs a `Cache-Tag` header on every requests. The value of the header is a comma separated list of tags.
## Purging the cache
Purging the cache, without revalidating, is done by passing `"purge": true` in the request body.
+9 -11
View File
@@ -2,16 +2,17 @@
"name": "gitbook", "name": "gitbook",
"version": "0.1.0", "version": "0.1.0",
"devDependencies": { "devDependencies": {
"@biomejs/biome": "^1.9.4",
"@changesets/cli": "^2.27.12", "@changesets/cli": "^2.27.12",
"prettier": "^3.0.3", "turbo": "^2.4.4",
"turbo": "^2.4.1-canary.2",
"vercel": "^39.3.0" "vercel": "^39.3.0"
}, },
"packageManager": "bun@1.1.18", "packageManager": "bun@1.2.5",
"overrides": { "overrides": {
"@codemirror/state": "6.4.1", "@codemirror/state": "6.4.1",
"react": "18.3.1", "react": "18.3.1",
"react-dom": "18.3.1" "react-dom": "18.3.1",
"@gitbook/api": "0.106.0"
}, },
"private": true, "private": true,
"scripts": { "scripts": {
@@ -20,23 +21,20 @@
"build": "turbo run build", "build": "turbo run build",
"build:v2": "turbo run build:v2", "build:v2": "turbo run build:v2",
"clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules", "clean-deps": "rm -rf node_modules && rm -rf packages/*/node_modules",
"lint": "turbo run lint",
"lint:fix": "turbo run lint -- --fix",
"typecheck": "turbo run typecheck", "typecheck": "turbo run typecheck",
"format": "prettier ./ --ignore-unknown --write", "format": "biome check --write ./",
"format:check": "prettier ./ --ignore-unknown --list-different", "format:check": "biome check --diagnostic-level=error ./",
"unit": "turbo run unit", "unit": "turbo run unit",
"e2e": "turbo run e2e", "e2e": "turbo run e2e",
"e2e-customers": "turbo run e2e-customers", "e2e-customers": "turbo run e2e-customers",
"changeset": "changeset", "changeset": "changeset",
"changeset-version": "changeset version && bun run format",
"release": "turbo run release && changeset publish", "release": "turbo run release && changeset publish",
"release:preview": "turbo run release:preview", "release:preview": "turbo run release:preview",
"download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local", "download:env": "op read op://gitbook-x-dev/gitbook-open/.env.local >> .env.local",
"clean": "turbo run clean" "clean": "turbo run clean"
}, },
"workspaces": [ "workspaces": ["packages/*"],
"packages/*"
],
"patchedDependencies": { "patchedDependencies": {
"decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch", "decode-named-character-reference@1.0.2": "patches/decode-named-character-reference@1.0.2.patch",
"@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch" "@vercel/next@4.4.2": "patches/@vercel%2Fnext@4.4.2.patch"
+2 -9
View File
@@ -21,7 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"typescript": "^5.5.3", "typescript": "^5.5.3",
"wrangler": "^3.109.2" "wrangler": "^3.112.0"
}, },
"scripts": { "scripts": {
"generate": "wrangler types --experimental-include-runtime", "generate": "wrangler types --experimental-include-runtime",
@@ -31,12 +31,5 @@
"release": "wrangler deploy", "release": "wrangler deploy",
"release:preview": "wrangler deploy && wrangler deploy --env preview" "release:preview": "wrangler deploy && wrangler deploy --env preview"
}, },
"files": [ "files": ["dist", "src", "bin", "data", "README.md", "CHANGELOG.md"]
"dist",
"src",
"bin",
"data",
"README.md",
"CHANGELOG.md"
]
} }
+8 -15
View File
@@ -1,5 +1,5 @@
import { encode, decode } from '@msgpack/msgpack';
import { DurableObject } from 'cloudflare:workers'; import { DurableObject } from 'cloudflare:workers';
import { decode, encode } from '@msgpack/msgpack';
import { LRUMap } from 'lru_map'; import { LRUMap } from 'lru_map';
export interface CacheObjectDescriptor { export interface CacheObjectDescriptor {
@@ -137,7 +137,7 @@ export class CacheObject extends DurableObject {
*/ */
public async purge() { public async purge() {
return this.logOperation({ operation: 'purge' }, async (setLog) => { return this.logOperation({ operation: 'purge' }, async (setLog) => {
let result = new Set<string>(); const result = new Set<string>();
try { try {
// List all the keys in the cache object. // List all the keys in the cache object.
@@ -149,11 +149,7 @@ export class CacheObject extends DurableObject {
entries.forEach((exp) => { entries.forEach((exp) => {
result.add(exp.k); result.add(exp.k);
}); });
} catch (error) { } catch (_error) {}
// If an error occurs, reset the cache object.
// This is a safety mechanism to prevent the cache object from being stuck in a bad state.
console.error('Error during purge, resetting the cache object', error);
}
await this.reset(); await this.reset();
return Array.from(result); return Array.from(result);
@@ -174,7 +170,7 @@ export class CacheObject extends DurableObject {
const toDeleteSet = new Set<string>(); const toDeleteSet = new Set<string>();
for (const [key, exp] of entries) { for (const [key, exp] of entries) {
const timestamp = parseInt(key.split('.')[1]); const timestamp = Number.parseInt(key.split('.')[1]);
if (timestamp < Date.now()) { if (timestamp < Date.now()) {
toDeleteSet.add(key); toDeleteSet.add(key);
for (let i = 0; i < exp.c; i++) { for (let i = 0; i < exp.c; i++) {
@@ -194,10 +190,7 @@ export class CacheObject extends DurableObject {
if (toDelete.length) { if (toDelete.length) {
await this.ctx.storage.setAlarm(Date.now() + 12 * 60 * 60 * 1000); await this.ctx.storage.setAlarm(Date.now() + 12 * 60 * 60 * 1000);
} }
} catch (error) { } catch (_error) {
// If an error occurs, reset the cache object.
// This is a safety mechanism to prevent the cache object from being stuck in a bad state.
console.error('Error during alarm, reset the cache object', error);
await this.reset(); await this.reset();
} }
}); });
@@ -218,10 +211,10 @@ export class CacheObject extends DurableObject {
*/ */
async logOperation<T>( async logOperation<T>(
log: Record<string, unknown>, log: Record<string, unknown>,
fn: (update: (log: Record<string, unknown>) => void) => Promise<T>, fn: (update: (log: Record<string, unknown>) => void) => Promise<T>
): Promise<T> { ): Promise<T> {
const objectId = this.ctx.id.name ?? this.ctx.id.toString(); const objectId = this.ctx.id.name ?? this.ctx.id.toString();
let update: Record<string, unknown> = {}; const update: Record<string, unknown> = {};
const start = performance.now(); const start = performance.now();
try { try {
return await fn((arg) => { return await fn((arg) => {
@@ -265,7 +258,7 @@ function encodeChunks<T>(key: string, value: T): Record<string, Uint8Array> {
function decodeChunks<T>(entries: Map<string, Uint8Array>): { value: T; size: number } | undefined { function decodeChunks<T>(entries: Map<string, Uint8Array>): { value: T; size: number } | undefined {
const chunks = Array.from(entries.entries()) const chunks = Array.from(entries.entries())
.map(([key, value]) => { .map(([key, value]) => {
const index = parseInt(key.split('.').pop()!); const index = Number.parseInt(key.split('.').pop()!);
return [index, value] as const; return [index, value] as const;
}) })
.sort(([a], [b]) => a - b) .sort(([a], [b]) => a - b)
+3 -3
View File
@@ -1,4 +1,4 @@
import type { CacheObject, CacheObjectDescriptor } from './CacheObject'; import type { CacheObject } from './CacheObject';
export type CacheLocationId = ContinentCode; export type CacheLocationId = ContinentCode;
const allLocations: CacheLocationId[] = ['AF', 'AS', 'NA', 'SA', 'AN', 'EU', 'OC']; const allLocations: CacheLocationId[] = ['AF', 'AS', 'NA', 'SA', 'AN', 'EU', 'OC'];
@@ -30,7 +30,7 @@ export class CacheObjectStub {
/** ID of the location to target */ /** ID of the location to target */
private locationId: CacheLocationId, private locationId: CacheLocationId,
/** Name of the tag */ /** Name of the tag */
private tag: string, private tag: string
) { ) {
const groupId = getCacheObjectIdName(this.locationId, this.tag); const groupId = getCacheObjectIdName(this.locationId, this.tag);
this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), { this.stub = this.doNamespace.get(this.doNamespace.idFromName(groupId), {
@@ -85,7 +85,7 @@ export class CacheObjectStub {
}); });
const locationkeys = await cacheGroup.purge(); const locationkeys = await cacheGroup.purge();
locationkeys.forEach((key) => keys.add(key)); locationkeys.forEach((key) => keys.add(key));
}), })
); );
return keys; return keys;
+1
View File
@@ -0,0 +1 @@
dist/
+13
View File
@@ -0,0 +1,13 @@
# @gitbook/cache-tags
## 0.2.0
### Minor Changes
- f32bf1f: Export function `getCacheTagForURL` to easily get the cache tag for a URL.
## 0.1.0
### Minor Changes
- 05ffd0e: Initial version of the package
+3
View File
@@ -0,0 +1,3 @@
# `@gitbook/cache-tags`
Utility to generate cache tags for GitBook Open.
+25
View File
@@ -0,0 +1,25 @@
{
"name": "@gitbook/cache-tags",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"development": "./src/index.ts",
"default": "./dist/index.js"
}
},
"version": "0.2.0",
"dependencies": {
"@gitbook/api": "*",
"assert-never": "^1.2.1"
},
"devDependencies": {
"typescript": "^5.5.3"
},
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit",
"dev": "tsc -w"
},
"files": ["dist", "src", "README.md", "CHANGELOG.md"]
}
+209
View File
@@ -0,0 +1,209 @@
import type { ComputedContentSource } from '@gitbook/api';
import assertNever from 'assert-never';
/**
* Get a stringified cache tag for a given object.
*/
export function getCacheTag(
spec: /**
* All data related to a user
* @deprecated - in v2, no tag as this is an immutable data
*/
| {
tag: 'user';
user: string;
}
/**
* All data related to a space
*/
| {
tag: 'space';
space: string;
}
/**
* All data related to an integration.
*/
| {
tag: 'integration';
integration: string;
}
/**
* All data related to a change request
*/
| {
tag: 'change-request';
space: string;
changeRequest: string;
}
/**
* Immutable data related to a revision
* @deprecated - in v2, no tag as this is an immutable data
*/
| {
tag: 'revision';
space: string;
revision: string;
}
/**
* Immutable data related to a document
* @deprecated - in v2, no tag as this is an immutable data
*/
| {
tag: 'document';
space: string;
document: string;
}
/**
* Immutable data related to a computed document
* @deprecated - in v2, no tag as this is an immutable data
*/
| {
tag: 'computed-document';
space: string;
sourceType: string;
}
/**
* All data related to the URL of a content
*/
| {
tag: 'url';
hostname: string;
}
/**
* All data related to a site
*/
| {
tag: 'site';
site: string;
}
/**
* All data related to an OpenAPI spec
*/
| {
tag: 'openapi';
organization: string;
openAPISpec: string;
}
/**
* All data related to a translation
*/
| {
tag: 'translation';
organization: string;
translationSettings: string;
}
): string {
switch (spec.tag) {
case 'user':
return `user:${spec.user}`;
case 'url':
return `url:${spec.hostname}`;
case 'space':
return `space:${spec.space}`;
case 'change-request':
return `space:${spec.space}:change-request:${spec.changeRequest}`;
case 'revision':
return `space:${spec.space}:revision:${spec.revision}`;
case 'document':
return `space:${spec.space}:document:${spec.document}`;
case 'computed-document':
return `space:${spec.space}:computed-document:${spec.sourceType}`;
case 'site':
return `site:${spec.site}`;
case 'integration':
return `integration:${spec.integration}`;
case 'openapi':
return `organization:${spec.organization}:openapi:${spec.openAPISpec}`;
case 'translation':
return `organization:${spec.organization}:translation:${spec.translationSettings}`;
default:
assertNever(spec);
}
}
/**
* Get the cache tag for a given URL.
*/
export function getCacheTagForURL(url: string | URL) {
const parsedURL = url instanceof URL ? url : new URL(url);
return getCacheTag({
tag: 'url',
hostname: parsedURL.hostname,
});
}
/**
* Get the tags for a computed content source.
*/
export function getComputedContentSourceCacheTags(
inContext: {
spaceId: string;
organizationId: string;
},
source: ComputedContentSource
) {
const tags: string[] = [];
// We add the dependencies as tags, to ensure that the computed content is invalidated
// when the dependencies are updated.
const dependencies = Object.values(source.dependencies ?? {});
if (dependencies.length > 0) {
dependencies.forEach((dependency) => {
switch (dependency.ref.kind) {
case 'space':
tags.push(
getCacheTag({
tag: 'space',
space: dependency.ref.space,
})
);
break;
case 'openapi':
tags.push(
getCacheTag({
tag: 'openapi',
organization: inContext.organizationId,
openAPISpec: dependency.ref.spec,
})
);
break;
case 'translation-language':
tags.push(
getCacheTag({
tag: 'translation',
organization: inContext.organizationId,
translationSettings: dependency.ref.translationSettings,
})
);
break;
default:
// Do not throw for unknown dependency types
// as it might mean we are lacking behind the API version
break;
}
});
} else {
// Push a dummy tag, as the v1 is only using the first tag
tags.push(
getCacheTag({
tag: 'computed-document',
space: inContext.spaceId,
sourceType: source.type,
})
);
}
// We invalidate the computed content when a new version of the integration is deployed.
if (source.type.startsWith('integration:')) {
const integration = source.type.split(':')[1];
tags.push(
getCacheTag({
tag: 'integration',
integration,
})
);
}
return tags;
}
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": false,
"declaration": true,
"outDir": "dist",
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"types": [
"bun-types" // add Bun global
]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
}
+6
View File
@@ -1,5 +1,11 @@
# @gitbook/colors # @gitbook/colors
## 0.3.0
### Minor Changes
- 4f0a772: Override tint lightness if supplied color is out of bounds
## 0.2.0 ## 0.2.0
### Minor Changes ### Minor Changes
+2 -7
View File
@@ -8,7 +8,7 @@
"default": "./dist/index.js" "default": "./dist/index.js"
} }
}, },
"version": "0.2.0", "version": "0.3.0",
"devDependencies": { "devDependencies": {
"typescript": "^5.5.3" "typescript": "^5.5.3"
}, },
@@ -17,10 +17,5 @@
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"dev": "tsc -w" "dev": "tsc -w"
}, },
"files": [ "files": ["dist", "src", "README.md", "CHANGELOG.md"]
"dist",
"src",
"README.md",
"CHANGELOG.md"
]
} }
+17 -3
View File
@@ -1,4 +1,4 @@
import { DARK_BASE, LIGHT_BASE, DEFAULT_TINT_COLOR } from './colors'; import { DARK_BASE, DEFAULT_TINT_COLOR, LIGHT_BASE } from './colors';
type ColorShades = { type ColorShades = {
[key: string]: string; [key: string]: string;
@@ -178,12 +178,13 @@ export function colorScale(
background = darkMode ? DARK_BASE : LIGHT_BASE, background = darkMode ? DARK_BASE : LIGHT_BASE,
foreground = darkMode ? LIGHT_BASE : DARK_BASE, foreground = darkMode ? LIGHT_BASE : DARK_BASE,
mix, mix,
}: ColorScaleOptions = {}, }: ColorScaleOptions = {}
) { ) {
const baseColor = rgbToOklch(hexToRgbArray(hex)); const baseColor = rgbToOklch(hexToRgbArray(hex));
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null; const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
const foregroundColor = rgbToOklch(hexToRgbArray(foreground)); const foregroundColor = rgbToOklch(hexToRgbArray(foreground));
const backgroundColor = rgbToOklch(hexToRgbArray(background)); const backgroundColor = rgbToOklch(hexToRgbArray(background));
let mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light;
if (mixColor && mix?.ratio && mix.ratio > 0) { if (mixColor && mix?.ratio && mix.ratio > 0) {
// If defined, we mix in a (tiny) bit of the mix color with the base color. // If defined, we mix in a (tiny) bit of the mix color with the base color.
@@ -192,7 +193,20 @@ export function colorScale(
baseColor.H = mix.color === DEFAULT_TINT_COLOR ? baseColor.H : mixColor.H; baseColor.H = mix.color === DEFAULT_TINT_COLOR ? baseColor.H : mixColor.H;
} }
const mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light; if (
(darkMode && baseColor.L < backgroundColor.L) ||
(!darkMode && baseColor.L > backgroundColor.L)
) {
// If the supplied color is outside of our lightness bounds, use the supplied color's lightness.
// This is mostly used to allow darker-than-dark backgrounds for brands that specifically want that look.
const difference = (backgroundColor.L - baseColor.L) / backgroundColor.L;
backgroundColor.L = baseColor.L;
// At the edges of the scale, the subtle lightness changes stop being perceptible. We need to amp up our mapping to still stand out.
const amplifier = 1;
mapping = mapping.map((step, index) =>
index < 9 ? step + step * amplifier * difference : step
);
}
const result = []; const result = [];
+1 -1
View File
@@ -13,7 +13,7 @@
"moduleResolution": "bundler", "moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "react", "jsx": "react-jsx",
"incremental": true, "incremental": true,
"types": [ "types": [
"bun-types" // add Bun global "bun-types" // add Bun global
+1 -2
View File
@@ -17,12 +17,11 @@ Object.entries(emojis).forEach(([key, value]) => {
if (emoji && key !== emoji) { if (emoji && key !== emoji) {
output[key] = emoji; output[key] = emoji;
} else if (!emoji) { } else if (!emoji) {
console.log('No emoji for', key);
} }
}); });
fs.mkdirSync(path.resolve(__dirname, 'dist'), { recursive: true }); fs.mkdirSync(path.resolve(__dirname, 'dist'), { recursive: true });
fs.writeFileSync( fs.writeFileSync(
path.resolve(__dirname, 'dist/index.ts'), path.resolve(__dirname, 'dist/index.ts'),
`export const emojiCodepoints: Record<string, string> = ${JSON.stringify(output, null, 4)};`, `export const emojiCodepoints: Record<string, string> = ${JSON.stringify(output, null, 4)};`
); );
+40
View File
@@ -1,5 +1,45 @@
# gitbook-v2 # gitbook-v2
## 0.2.3
### Patch Changes
- 5b2bf82: Use stable site URL data for route rewrite in the middleware
## 0.2.2
### Patch Changes
- 54ee014: Add initial support for loading custom fonts
- bba2e52: Fix site redirects when it includes a section/variant path
## 0.2.1
### Patch Changes
- Updated dependencies [f32bf1f]
- @gitbook/cache-tags@0.2.0
## 0.2.0
### Minor Changes
- 76c7974: Add route to revalidate cached data
## 0.1.2
### Patch Changes
- 05ffd0e: Improving data cache management for computed content
- Updated dependencies [05ffd0e]
- @gitbook/cache-tags@0.1.0
## 0.1.1
### Patch Changes
- 3e11678: fix: lost section groups
## 0.1.0 ## 0.1.0
### Minor Changes ### Minor Changes
+27 -2
View File
@@ -5,16 +5,41 @@
*/ */
const nextConfig = { const nextConfig = {
experimental: { experimental: {
// This is needed to throw "forbidden" when the api token expired during revalidation
authInterrupts: true,
// This is needed to use 'use cache'
useCache: true, useCache: true,
// Content is fully static, we can cache it in the session memory cache for a long time
staleTimes: {
dynamic: 3600, // 1 hour
static: 3600, // 1 hour
},
}, },
env: { env: {
BUILD_VERSION: (process.env.GITHUB_SHA ?? '').slice(0, 7), BUILD_VERSION: (process.env.GITHUB_SHA ?? '').slice(0, 7),
GITBOOK_URL: process.env.GITBOOK_URL,
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX, // GitBook envs
GITBOOK_API_URL: process.env.GITBOOK_API_URL,
GITBOOK_APP_URL: process.env.GITBOOK_APP_URL,
GITBOOK_INTEGRATIONS_HOST: process.env.GITBOOK_INTEGRATIONS_HOST,
GITBOOK_IMAGE_RESIZE_URL: process.env.GITBOOK_IMAGE_RESIZE_URL,
GITBOOK_ICONS_URL: process.env.GITBOOK_ICONS_URL, GITBOOK_ICONS_URL: process.env.GITBOOK_ICONS_URL,
GITBOOK_ICONS_TOKEN: process.env.GITBOOK_ICONS_TOKEN, GITBOOK_ICONS_TOKEN: process.env.GITBOOK_ICONS_TOKEN,
GITBOOK_URL: process.env.GITBOOK_URL,
GITBOOK_API_TOKEN: process.env.GITBOOK_API_TOKEN,
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX,
GITBOOK_SECRET: process.env.GITBOOK_SECRET,
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
GITBOOK_FONTS_URL: process.env.GITBOOK_FONTS_URL,
// Next.js envs
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY, NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY,
// Used to detect if the app is running in V2 mode
GITBOOK_V2: 'true',
}, },
assetPrefix: process.env.GITBOOK_ASSETS_PREFIX, assetPrefix: process.env.GITBOOK_ASSETS_PREFIX,
+9 -29
View File
@@ -1,30 +1,10 @@
// default open-next.config.ts file created by @opennextjs/cloudflare import { defineCloudflareConfig } from '@opennextjs/cloudflare';
import d1TagCache from '@opennextjs/cloudflare/d1-tag-cache';
import kvIncrementalCache from '@opennextjs/cloudflare/kv-cache';
import memoryQueue from '@opennextjs/cloudflare/memory-queue';
import cache from '@opennextjs/cloudflare/kvCache'; export default defineCloudflareConfig({
incrementalCache: kvIncrementalCache,
const config = { queue: memoryQueue,
default: { tagCache: d1TagCache,
override: { });
wrapper: 'cloudflare-node',
converter: 'edge',
incrementalCache: async () => cache,
tagCache: 'dummy',
queue: 'dummy',
},
},
middleware: {
external: true,
override: {
wrapper: 'cloudflare-edge',
converter: 'edge',
proxyExternalRequest: 'fetch',
},
},
dangerous: {
enableCacheInterception: false,
},
};
export default config;
+14 -7
View File
@@ -1,29 +1,36 @@
{ {
"name": "gitbook-v2", "name": "gitbook-v2",
"version": "0.1.0", "version": "0.2.3",
"private": true, "private": true,
"dependencies": { "dependencies": {
"next": "canary", "next": "^15.2.3",
"react": "^19.0.0", "react": "^19.0.0",
"react-dom": "^19.0.0", "react-dom": "^19.0.0",
"@gitbook/api": "^0.96.0", "@gitbook/api": "*",
"@gitbook/cache-tags": "workspace:*",
"@sindresorhus/fnv1a": "^3.1.0", "@sindresorhus/fnv1a": "^3.1.0",
"server-only": "^0.0.1" "server-only": "^0.0.1",
"warn-once": "^0.1.1",
"rison": "^0.1.1",
"jwt-decode": "^4.0.0",
"p-memoize": "^7.1.1"
}, },
"devDependencies": { "devDependencies": {
"gitbook": "*", "gitbook": "*",
"@opennextjs/cloudflare": "^0.5.3", "@opennextjs/cloudflare": "^0.5.10",
"@types/rison": "^0.0.9",
"tailwindcss": "^3.4.0", "tailwindcss": "^3.4.0",
"postcss": "^8" "postcss": "^8"
}, },
"scripts": { "scripts": {
"generate": "rm -rf ./public && cp -r ../gitbook/public ./public", "generate": "rm -rf ./public && cp -r ../gitbook/public ./public",
"dev:v2": "env-cmd --silent -f ../../.env.local next", "dev:v2": "env-cmd --silent -f ../../.env.local next --turbopack",
"build": "next build", "build": "next build",
"build:v2": "next build", "build:v2": "next build",
"start": "next start", "start": "next start",
"build:v2:cloudflare": "opennextjs-cloudflare", "build:v2:cloudflare": "opennextjs-cloudflare",
"dev:v2:cloudflare": "wrangler dev --port 8771", "dev:v2:cloudflare": "wrangler dev --port 8771",
"unit": "bun test" "unit": "bun test",
"typecheck": "tsc --noEmit"
} }
} }
@@ -1,10 +1,10 @@
import { getDynamicSiteContext, getPagePathFromParams, RouteParams } from '@v2/app/utils';
import { import {
SitePage,
generateSitePageMetadata, generateSitePageMetadata,
generateSitePageViewport, generateSitePageViewport,
SitePage,
} from '@/components/SitePage'; } from '@/components/SitePage';
import { Metadata, Viewport } from 'next'; import { type RouteParams, getDynamicSiteContext, getPagePathFromParams } from '@v2/app/utils';
import type { Metadata, Viewport } from 'next';
type PageProps = { type PageProps = {
params: Promise<RouteParams>; params: Promise<RouteParams>;
@@ -13,26 +13,24 @@ type PageProps = {
export default async function Page(props: PageProps) { export default async function Page(props: PageProps) {
const params = await props.params; const params = await props.params;
const context = await getDynamicSiteContext(params); const { context } = await getDynamicSiteContext(params);
const pathname = getPagePathFromParams(params); const pathname = getPagePathFromParams(params);
return <SitePage context={context} pageParams={{ pathname }} redirectOnFallback={true} />; return <SitePage context={context} pageParams={{ pathname }} />;
} }
export async function generateViewport(props: PageProps): Promise<Viewport> { export async function generateViewport(props: PageProps): Promise<Viewport> {
const context = await getDynamicSiteContext(await props.params); const { context } = await getDynamicSiteContext(await props.params);
return generateSitePageViewport(context); return generateSitePageViewport(context);
} }
export async function generateMetadata(props: PageProps): Promise<Metadata> { export async function generateMetadata(props: PageProps): Promise<Metadata> {
const [params, searchParams] = await Promise.all([props.params, props.searchParams]); const params = await props.params;
const context = await getDynamicSiteContext(params); const { context } = await getDynamicSiteContext(params);
const pathname = getPagePathFromParams(params); const pathname = getPagePathFromParams(params);
return generateSitePageMetadata({ return generateSitePageMetadata({
context, context,
pageParams: { pathname }, pageParams: { pathname },
redirectOnFallback: true,
fallback: !!searchParams.fallback,
}); });
} }
@@ -0,0 +1,44 @@
import { CustomizationRootLayout } from '@/components/RootLayout';
import {
SiteLayout,
generateSiteLayoutMetadata,
generateSiteLayoutViewport,
} from '@/components/SiteLayout';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
import { getThemeFromMiddleware } from '@v2/lib/middleware';
interface SiteDynamicLayoutProps {
params: Promise<RouteLayoutParams>;
}
export default async function SiteDynamicLayout({
params,
children,
}: React.PropsWithChildren<SiteDynamicLayoutProps>) {
const { context, visitorAuthClaims } = await getDynamicSiteContext(await params);
const forcedTheme = await getThemeFromMiddleware();
return (
<CustomizationRootLayout forcedTheme={forcedTheme} customization={context.customization}>
<SiteLayout
context={context}
forcedTheme={forcedTheme}
withTracking={!GITBOOK_DISABLE_TRACKING}
visitorAuthClaims={visitorAuthClaims}
>
{children}
</SiteLayout>
</CustomizationRootLayout>
);
}
export async function generateViewport({ params }: SiteDynamicLayoutProps) {
const { context } = await getDynamicSiteContext(await params);
return generateSiteLayoutViewport(context);
}
export async function generateMetadata({ params }: SiteDynamicLayoutProps) {
const { context } = await getDynamicSiteContext(await params);
return generateSiteLayoutMetadata(context);
}
@@ -0,0 +1,12 @@
import type { NextRequest } from 'next/server';
import { serveIcon } from '@/routes/icon';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getDynamicSiteContext(await params);
return serveIcon(context, request);
}
@@ -0,0 +1,13 @@
import type { NextRequest } from 'next/server';
import type { PageIdParams } from '@/components/SitePage';
import { serveOGImage } from '@/routes/ogimage';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams & PageIdParams> }
) {
const { context } = await getDynamicSiteContext(await params);
return serveOGImage(context, await params);
}
@@ -0,0 +1,12 @@
import { PDFRootLayout } from '@/components/PDF';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
export default async function RootLayout(props: {
params: Promise<RouteLayoutParams>;
children: React.ReactNode;
}) {
const { params, children } = props;
const { context } = await getDynamicSiteContext(await params);
return <PDFRootLayout context={context}>{children}</PDFRootLayout>;
}
@@ -0,0 +1,20 @@
import { PDFPage, generatePDFMetadata } from '@/components/PDF';
import { type RouteLayoutParams, getDynamicSiteContext } from '@v2/app/utils';
export async function generateMetadata({
params,
}: {
params: Promise<RouteLayoutParams>;
}) {
const { context } = await getDynamicSiteContext(await params);
return generatePDFMetadata(context);
}
export default async function Page(props: {
params: Promise<RouteLayoutParams>;
searchParams: Promise<{ [key: string]: string }>;
}) {
const { params, searchParams } = props;
const { context } = await getDynamicSiteContext(await params);
return <PDFPage context={context} searchParams={await searchParams} />;
}
@@ -1,28 +0,0 @@
import { CustomizationRootLayout } from '@/components/RootLayout';
import { SiteLayout } from '@/components/SiteLayout';
import { getDynamicSiteContext, RouteLayoutParams } from '@v2/app/utils';
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
import { getThemeFromMiddleware } from '@v2/lib/middleware';
export default async function SiteDynamicLayout({
params,
children,
}: {
params: Promise<RouteLayoutParams>;
children: React.ReactNode;
}) {
const context = await getDynamicSiteContext(await params);
const forcedTheme = await getThemeFromMiddleware();
return (
<CustomizationRootLayout customization={context.customization}>
<SiteLayout
context={context}
forcedTheme={forcedTheme}
withTracking={!GITBOOK_DISABLE_TRACKING}
>
{children}
</SiteLayout>
</CustomizationRootLayout>
);
}
@@ -1,5 +0,0 @@
import { SitePageSkeleton } from '@/components/SitePage';
export default function Loading() {
return <SitePageSkeleton />;
}
@@ -1,12 +1,12 @@
import { unstable_cacheTag as cacheTag } from 'next/cache';
import { getSiteCacheTag } from '@v2/lib/cache';
import { getPagePathFromParams, getStaticSiteContext, RouteParams } from '@v2/app/utils';
import { import {
SitePage,
generateSitePageMetadata, generateSitePageMetadata,
generateSitePageViewport, generateSitePageViewport,
SitePage,
} from '@/components/SitePage'; } from '@/components/SitePage';
import { Metadata, Viewport } from 'next'; import { getCacheTag } from '@gitbook/cache-tags';
import { type RouteParams, getPagePathFromParams, getStaticSiteContext } from '@v2/app/utils';
import type { Metadata, Viewport } from 'next';
import { unstable_cacheTag as cacheTag } from 'next/cache';
export const dynamic = 'force-static'; export const dynamic = 'force-static';
@@ -18,27 +18,31 @@ export default async function Page(props: PageProps) {
'use cache'; 'use cache';
const params = await props.params; const params = await props.params;
const context = await getStaticSiteContext(params); const { context } = await getStaticSiteContext(params);
const pathname = getPagePathFromParams(params); const pathname = getPagePathFromParams(params);
cacheTag(getSiteCacheTag(context.site.id)); cacheTag(
getCacheTag({
tag: 'site',
site: context.site.id,
})
);
return <SitePage context={context} pageParams={{ pathname }} redirectOnFallback={true} />; return <SitePage context={context} pageParams={{ pathname }} />;
} }
export async function generateViewport(props: PageProps): Promise<Viewport> { export async function generateViewport(props: PageProps): Promise<Viewport> {
const context = await getStaticSiteContext(await props.params); const { context } = await getStaticSiteContext(await props.params);
return generateSitePageViewport(context); return generateSitePageViewport(context);
} }
export async function generateMetadata(props: PageProps): Promise<Metadata> { export async function generateMetadata(props: PageProps): Promise<Metadata> {
const params = await props.params; const params = await props.params;
const context = await getStaticSiteContext(params); const { context } = await getStaticSiteContext(params);
const pathname = getPagePathFromParams(params); const pathname = getPagePathFromParams(params);
return generateSitePageMetadata({ return generateSitePageMetadata({
context, context,
pageParams: { pathname }, pageParams: { pathname },
redirectOnFallback: true,
}); });
} }
@@ -0,0 +1,52 @@
import { CustomizationRootLayout } from '@/components/RootLayout';
import {
SiteLayout,
generateSiteLayoutMetadata,
generateSiteLayoutViewport,
} from '@/components/SiteLayout';
import { getCacheTag } from '@gitbook/cache-tags';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
import { unstable_cacheTag as cacheTag } from 'next/cache';
interface SiteStaticLayoutProps {
params: Promise<RouteLayoutParams>;
}
export default async function SiteStaticLayout({
params,
children,
}: React.PropsWithChildren<SiteStaticLayoutProps>) {
'use cache';
const { context, visitorAuthClaims } = await getStaticSiteContext(await params);
cacheTag(
getCacheTag({
tag: 'site',
site: context.site.id,
})
);
return (
<CustomizationRootLayout customization={context.customization}>
<SiteLayout
context={context}
withTracking={!GITBOOK_DISABLE_TRACKING}
visitorAuthClaims={visitorAuthClaims}
>
{children}
</SiteLayout>
</CustomizationRootLayout>
);
}
export async function generateViewport({ params }: SiteStaticLayoutProps) {
const { context } = await getStaticSiteContext(await params);
return generateSiteLayoutViewport(context);
}
export async function generateMetadata({ params }: SiteStaticLayoutProps) {
const { context } = await getStaticSiteContext(await params);
return generateSiteLayoutMetadata(context);
}
@@ -0,0 +1,14 @@
import type { NextRequest } from 'next/server';
import { serveLLMsTxt } from '@/routes/llms';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getStaticSiteContext(await params);
return serveLLMsTxt(context, { withMarkdownPages: true });
}
@@ -0,0 +1,14 @@
import type { NextRequest } from 'next/server';
import { serveRobotsTxt } from '@/routes/robots';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getStaticSiteContext(await params);
return serveRobotsTxt(context);
}
@@ -0,0 +1,14 @@
import type { NextRequest } from 'next/server';
import { servePagesSitemap } from '@/routes/sitemap';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getStaticSiteContext(await params);
return servePagesSitemap(context);
}
@@ -0,0 +1,14 @@
import type { NextRequest } from 'next/server';
import { serveRootSitemap } from '@/routes/sitemap';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getStaticSiteContext(await params);
return serveRootSitemap(context);
}
@@ -0,0 +1,14 @@
import type { NextRequest } from 'next/server';
import { serveIcon } from '@/routes/icon';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams> }
) {
const { context } = await getStaticSiteContext(await params);
return serveIcon(context, request);
}
@@ -0,0 +1,11 @@
import { servePageMarkdown } from '@/routes/markdownPage';
import { type RouteParams, getPagePathFromParams, getStaticSiteContext } from '@v2/app/utils';
import type { NextRequest } from 'next/server';
export const dynamic = 'force-static';
export async function GET(_request: NextRequest, { params }: { params: Promise<RouteParams> }) {
const { context } = await getStaticSiteContext(await params);
const pathname = getPagePathFromParams(await params);
return servePageMarkdown(context, pathname);
}
@@ -0,0 +1,15 @@
import type { NextRequest } from 'next/server';
import type { PageIdParams } from '@/components/SitePage';
import { serveOGImage } from '@/routes/ogimage';
import { type RouteLayoutParams, getStaticSiteContext } from '@v2/app/utils';
export const dynamic = 'force-static';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<RouteLayoutParams & PageIdParams> }
) {
const { context } = await getStaticSiteContext(await params);
return serveOGImage(context, await params);
}
@@ -1,28 +0,0 @@
import { unstable_cacheTag as cacheTag } from 'next/cache';
import { getSiteCacheTag } from '@v2/lib/cache';
import { getStaticSiteContext, RouteLayoutParams } from '@v2/app/utils';
import { CustomizationRootLayout } from '@/components/RootLayout';
import { SiteLayout } from '@/components/SiteLayout';
import { GITBOOK_DISABLE_TRACKING } from '@v2/lib/env';
export default async function SiteStaticLayout({
params,
children,
}: {
params: Promise<RouteLayoutParams>;
children: React.ReactNode;
}) {
'use cache';
const context = await getStaticSiteContext(await params);
cacheTag(getSiteCacheTag(context.site.id));
return (
<CustomizationRootLayout customization={context.customization}>
<SiteLayout context={context} withTracking={!GITBOOK_DISABLE_TRACKING}>
{children}
</SiteLayout>
</CustomizationRootLayout>
);
}
+53 -65
View File
@@ -1,9 +1,9 @@
import { fetchSiteContextByURL } from '@v2/lib/context'; import { getVisitorAuthClaims, getVisitorAuthClaimsFromToken } from '@/lib/adaptive';
import { createDataFetcher } from '@v2/lib/data'; import type { SiteAPIToken } from '@gitbook/api';
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_URL } from '@v2/lib/env'; import { type SiteURLData, fetchSiteContextByURLLookup, getBaseContext } from '@v2/lib/context';
import { createNoopImageResizer } from '@v2/lib/images'; import { jwtDecode } from 'jwt-decode';
import { createLinker } from '@v2/lib/links'; import { forbidden } from 'next/navigation';
import { headers } from 'next/headers'; import rison from 'rison';
export type RouteParamMode = 'url-host' | 'url'; export type RouteParamMode = 'url-host' | 'url';
@@ -12,6 +12,9 @@ export type RouteLayoutParams = {
/** URL encoded site URL */ /** URL encoded site URL */
siteURL: string; siteURL: string;
/** URL and Rison encoded site data from getPublishedContentByUrl */
siteData: string;
}; };
export type RouteParams = RouteLayoutParams & { export type RouteParams = RouteLayoutParams & {
@@ -21,25 +24,30 @@ export type RouteParams = RouteLayoutParams & {
/** /**
* Get the static context when rendering statically a site. * Get the static context when rendering statically a site.
*/ */
export function getStaticSiteContext(params: RouteLayoutParams) { export async function getStaticSiteContext(params: RouteLayoutParams) {
const url = getSiteURLFromParams(params); const siteURL = getSiteURLFromParams(params);
const siteURLData = getSiteURLDataFromParams(params);
const dataFetcher = createDataFetcher(); // For static routes, we check the expiration of the JWT token
const linker = createLinkerFromParams(params); // as the route might be revalidated after expiration
const imageResizer = createNoopImageResizer(); const decoded = jwtDecode<SiteAPIToken & { exp: number }>(siteURLData.apiToken);
if (decoded.exp && decoded.exp < Date.now() / 1000 + 120) {
forbidden();
}
return fetchSiteContextByURL( const context = await fetchSiteContextByURLLookup(
{ getBaseContext({
dataFetcher, siteURL,
linker, siteURLData,
imageResizer, urlMode: getModeFromParams(params.mode),
}, }),
{ siteURLData
url: url.toString(),
visitorAuthToken: null,
redirectOnError: false,
},
); );
return {
context,
visitorAuthClaims: getVisitorAuthClaimsFromToken(decoded),
};
} }
/** /**
@@ -47,31 +55,22 @@ export function getStaticSiteContext(params: RouteLayoutParams) {
* The context will depend on the request. * The context will depend on the request.
*/ */
export async function getDynamicSiteContext(params: RouteLayoutParams) { export async function getDynamicSiteContext(params: RouteLayoutParams) {
const url = getSiteURLFromParams(params); const siteURL = getSiteURLFromParams(params);
const headersSet = await headers(); const siteURLData = getSiteURLDataFromParams(params);
const dataFetcher = createDataFetcher({ const context = await fetchSiteContextByURLLookup(
apiToken: headersSet.get('x-gitbook-token') ?? GITBOOK_API_TOKEN, getBaseContext({
apiEndpoint: headersSet.get('x-gitbook-api') ?? GITBOOK_API_URL, siteURL,
}); siteURLData,
urlMode: getModeFromParams(params.mode),
const linker = createLinkerFromParams(params); }),
const imageResizer = createNoopImageResizer(); siteURLData
return fetchSiteContextByURL(
{
dataFetcher,
linker,
imageResizer,
},
{
url: url.toString(),
visitorAuthToken: headersSet.get('x-gitbook-visitor-token'),
// TODO: set it only when the token comes from the cookies.
redirectOnError: true,
},
); );
return {
context,
visitorAuthClaims: getVisitorAuthClaims(siteURLData),
};
} }
/** /**
@@ -82,28 +81,9 @@ export function getPagePathFromParams(params: RouteParams) {
return decoded; return decoded;
} }
function createLinkerFromParams(params: RouteLayoutParams) {
const url = getSiteURLFromParams(params);
const mode = getModeFromParams(params.mode);
if (mode === 'url-host') {
return createLinker({
host: url.host,
pathname: '/',
});
}
const gitbookURL = new URL(GITBOOK_URL);
return createLinker({
protocol: gitbookURL.protocol,
host: gitbookURL.host,
pathname: `/url/${url.host}`,
});
}
function getSiteURLFromParams(params: RouteLayoutParams) { function getSiteURLFromParams(params: RouteLayoutParams) {
const decoded = decodeURIComponent(params.siteURL); const decoded = decodeURIComponent(params.siteURL);
const url = new URL('https://' + decoded); const url = new URL(`https://${decoded}`);
return url; return url;
} }
@@ -114,3 +94,11 @@ function getModeFromParams(mode: string): RouteParamMode {
return 'url'; return 'url';
} }
/**
* Get the decoded site data from the params.
*/
function getSiteURLDataFromParams(params: RouteLayoutParams): SiteURLData {
const decoded = decodeURIComponent(params.siteData);
return rison.decode(decoded);
}
+40
View File
@@ -0,0 +1,40 @@
import { type NextRequest, NextResponse } from 'next/server';
import {
GITBOOK_API_PUBLIC_URL,
GITBOOK_API_TOKEN,
GITBOOK_API_URL,
GITBOOK_APP_URL,
GITBOOK_ASSETS_URL,
GITBOOK_DISABLE_TRACKING,
GITBOOK_FONTS_URL,
GITBOOK_ICONS_URL,
GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
GITBOOK_INTEGRATIONS_HOST,
GITBOOK_SECRET,
GITBOOK_URL,
GITBOOK_USER_AGENT,
} from '@v2/lib/env';
/**
* Output the public environment variables for this deployment
*/
export async function GET(_req: NextRequest) {
return NextResponse.json({
GITBOOK_URL,
GITBOOK_APP_URL,
GITBOOK_API_URL,
GITBOOK_API_PUBLIC_URL,
GITBOOK_ASSETS_URL,
GITBOOK_FONTS_URL,
GITBOOK_ICONS_URL,
GITBOOK_USER_AGENT,
GITBOOK_INTEGRATIONS_HOST,
GITBOOK_DISABLE_TRACKING,
// Secret envs
GITBOOK_SECRET: !!GITBOOK_SECRET,
GITBOOK_API_TOKEN: !!GITBOOK_API_TOKEN,
GITBOOK_IMAGE_RESIZE_SIGNING_KEY: !!GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
});
}
@@ -0,0 +1,35 @@
import { type NextRequest, NextResponse } from 'next/server';
import { withVerifySignature } from '@v2/lib/routes';
import { revalidateTag } from 'next/cache';
interface JsonBody {
tags: string[];
}
/**
* Revalidate cached data based on tags.
* The body should be a JSON with { tags: string[] }
*/
export async function POST(req: NextRequest) {
return withVerifySignature<JsonBody>(req, async (body) => {
if (!body.tags || !Array.isArray(body.tags)) {
return NextResponse.json(
{
error: 'tags must be an array',
},
{ status: 400 }
);
}
body.tags.forEach((tag) => {
// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`Revalidating tag: ${tag}`);
revalidateTag(tag);
});
return NextResponse.json({
success: true,
});
});
}
@@ -0,0 +1,59 @@
import {
type GitBookBaseContext,
type GitBookSpaceContext,
fetchSpaceContextByIds,
} from '@v2/lib/context';
import { createDataFetcher } from '@v2/lib/data';
import { createLinker } from '@v2/lib/links';
import { getAPITokenFromMiddleware } from '@v2/lib/middleware';
export type SpacePDFRouteParams = {
spaceId: string;
changeRequestId?: string;
revisionId?: string;
};
export async function getSpacePDFContext(
params: SpacePDFRouteParams
): Promise<GitBookSpaceContext> {
const { spaceId } = params;
const apiToken = await getAPITokenFromMiddleware();
const basePath = getPDFRoutePath(params);
const linker = createLinker({
spaceBasePath: basePath,
siteBasePath: basePath,
});
const dataFetcher = createDataFetcher({
apiToken: apiToken,
});
const baseContext: GitBookBaseContext = {
linker,
dataFetcher,
};
return await fetchSpaceContextByIds(baseContext, {
space: spaceId,
shareKey: undefined,
changeRequest: params.changeRequestId,
revision: params.revisionId,
});
}
function getPDFRoutePath(params: SpacePDFRouteParams) {
let path = `/~space/${params.spaceId}`;
if (params.changeRequestId) {
path += `/~/changes/${params.changeRequestId}`;
}
if (params.revisionId) {
path += `/~/revisions/${params.revisionId}`;
}
path += '~gitbook/pdf';
return path;
}
@@ -0,0 +1,2 @@
import RootLayout from '@v2/app/~space/[spaceId]/~gitbook/pdf/layout';
export default RootLayout;
@@ -0,0 +1,4 @@
import PDFPage, { generateMetadata } from '@v2/app/~space/[spaceId]/~gitbook/pdf/page';
export default PDFPage;
export { generateMetadata };
@@ -0,0 +1,2 @@
import RootLayout from '@v2/app/~space/[spaceId]/~gitbook/pdf/layout';
export default RootLayout;
@@ -0,0 +1,4 @@
import PDFPage, { generateMetadata } from '@v2/app/~space/[spaceId]/~gitbook/pdf/page';
export default PDFPage;
export { generateMetadata };
@@ -0,0 +1,12 @@
import { PDFRootLayout } from '@/components/PDF';
import { type SpacePDFRouteParams, getSpacePDFContext } from '@v2/app/~space/[spaceId]/pdf';
export default async function RootLayout(props: {
params: Promise<SpacePDFRouteParams>;
children: React.ReactNode;
}) {
const { params, children } = props;
const context = await getSpacePDFContext(await params);
return <PDFRootLayout context={context}>{children}</PDFRootLayout>;
}
@@ -0,0 +1,20 @@
import { PDFPage, generatePDFMetadata } from '@/components/PDF';
import { type SpacePDFRouteParams, getSpacePDFContext } from '@v2/app/~space/[spaceId]/pdf';
export async function generateMetadata({
params,
}: {
params: Promise<SpacePDFRouteParams>;
}) {
const context = await getSpacePDFContext(await params);
return generatePDFMetadata(context);
}
export default async function Page(props: {
params: Promise<SpacePDFRouteParams>;
searchParams: Promise<{ [key: string]: string }>;
}) {
const { params, searchParams } = props;
const context = await getSpacePDFContext(await params);
return <PDFPage context={context} searchParams={await searchParams} />;
}
-34
View File
@@ -1,34 +0,0 @@
/**
* Get the cache tag for a site.
*/
export function getSiteCacheTag(siteId: string) {
return `site:${siteId}`;
}
/**
* Get the cache tag for a hostname.
*/
export function getHostnameCacheTag(hostname: string) {
return `url:${hostname}`;
}
/**
* Get the cache tag for an OpenAPI spec.
*/
export function getOpenAPISpecCacheTag(organizationId: string, slug: string) {
return `organization:${organizationId}:openapi:${slug}`;
}
/**
* Get the cache tag for a space.
*/
export function getSpaceCacheTag(spaceId: string) {
return `space:${spaceId}`;
}
/**
* Get the cache tag for a change request.
*/
export function getChangeRequestCacheTag(spaceId: string, changeRequestId: string) {
return `space:${spaceId}:change-request:${changeRequestId}`;
}
+163 -62
View File
@@ -1,5 +1,7 @@
import { import { getSiteStructureSections } from '@/lib/sites';
import type {
ChangeRequest, ChangeRequest,
PublishedSiteContent,
RevisionPage, RevisionPage,
RevisionPageDocument, RevisionPageDocument,
Site, Site,
@@ -11,12 +13,41 @@ import {
SiteStructure, SiteStructure,
Space, Space,
} from '@gitbook/api'; } from '@gitbook/api';
import { redirect } from 'next/navigation'; import {
type GitBookDataFetcher,
createDataFetcher,
getDataOrNull,
throwIfDataError,
} from '@v2/lib/data';
import { notFound } from 'next/navigation';
import { assert } from 'ts-essentials'; import { assert } from 'ts-essentials';
import { getSiteStructureSections } from '@/lib/sites'; import { GITBOOK_URL } from './env';
import { createDataFetcher, GitBookDataFetcher } from '@v2/lib/data'; import { type ImageResizer, createImageResizer } from './images';
import { GitBookSpaceLinker, appendPrefixToLinker } from './links'; import { type GitBookLinker, createLinker } from './links';
import { ImageResizer } from './images';
/**
* Data about the site URL. Provided by the middleware.
* These data are stable between pages in the same site space.
*/
export type SiteURLData = Pick<
PublishedSiteContent,
| 'organization'
| 'apiToken'
| 'site'
| 'siteSpace'
| 'space'
| 'revision'
| 'changeRequest'
| 'shareKey'
| 'siteSection'
| 'siteBasePath'
| 'basePath'
> & {
/**
* Identifier used for image resizing.
*/
imagesContextId: string;
};
/** /**
* Generic context when rendering content. * Generic context when rendering content.
@@ -30,12 +61,12 @@ export type GitBookBaseContext = {
/** /**
* Linker to generate links in the current space. * Linker to generate links in the current space.
*/ */
linker: GitBookSpaceLinker; linker: GitBookLinker;
/** /**
* Image resizer to resize images. * Image resizer to resize images.
*/ */
imageResizer: ImageResizer; imageResizer?: ImageResizer;
}; };
/** /**
@@ -90,9 +121,6 @@ export type GitBookSiteContext = GitBookSpaceContext & {
/** Scripts to load for the site. */ /** Scripts to load for the site. */
scripts: SiteIntegrationScript[]; scripts: SiteIntegrationScript[];
/** Visitor token used to fetch the site */
visitorAuthToken: string | null;
}; };
/** /**
@@ -103,36 +131,65 @@ export type GitBookPageContext = (GitBookSpaceContext | GitBookSiteContext) & {
}; };
/** /**
* Fetch the context of a site for a given URL and a base context. * Get the base context for a request on a site.
*/ */
export async function fetchSiteContextByURL( export function getBaseContext(input: {
baseContext: GitBookBaseContext, siteURL: URL | string;
input: { siteURLData: SiteURLData;
url: string; urlMode: 'url' | 'url-host';
visitorAuthToken: string | null; }) {
redirectOnError: boolean; const { urlMode, siteURLData } = input;
}, const siteURL = typeof input.siteURL === 'string' ? new URL(input.siteURL) : input.siteURL;
): Promise<GitBookSiteContext> {
const { dataFetcher } = baseContext; const dataFetcher = createDataFetcher({
const data = await dataFetcher.getPublishedContentByUrl({ apiToken: siteURLData.apiToken ?? null,
url: input.url,
visitorAuthToken: input.visitorAuthToken,
redirectOnError: input.redirectOnError,
}); });
if ('redirect' in data) { const gitbookURL = GITBOOK_URL ? new URL(GITBOOK_URL) : undefined;
redirect(data.redirect); const linker =
urlMode === 'url-host'
? createLinker({
host: siteURL.host,
siteBasePath: siteURLData.siteBasePath,
spaceBasePath: siteURLData.basePath,
})
: createLinker({
protocol: gitbookURL?.protocol,
host: gitbookURL?.host,
siteBasePath: `/url/${siteURL.host}${siteURLData.siteBasePath}`,
spaceBasePath: `/url/${siteURL.host}${siteURLData.basePath}`,
});
if (urlMode === 'url') {
// Create link in the same format for links to other sites/sections.
linker.toLinkForContent = (rawURL: string) => {
const urlObject = new URL(rawURL);
return `/url/${urlObject.host}${urlObject.pathname}${urlObject.search}`;
};
} }
const context = await fetchSiteContextByIds( const imageResizer = createImageResizer({
{ imagesContextId: siteURLData.imagesContextId,
...baseContext, // To ensure image resizing work for proxied sites,
dataFetcher: createDataFetcher({ // we serve images from the root of the site.
apiEndpoint: dataFetcher.apiEndpoint, linker: linker,
apiToken: data.apiToken, });
}),
}, return {
{ dataFetcher,
linker,
imageResizer,
};
}
/**
* Fetch the context of a site using the resolution of a URL
*/
export async function fetchSiteContextByURLLookup(
baseContext: GitBookBaseContext,
data: SiteURLData
): Promise<GitBookSiteContext> {
return await fetchSiteContextByIds(baseContext, {
organization: data.organization, organization: data.organization,
site: data.site, site: data.site,
siteSection: data.siteSection, siteSection: data.siteSection,
@@ -141,16 +198,7 @@ export async function fetchSiteContextByURL(
shareKey: data.shareKey, shareKey: data.shareKey,
changeRequest: data.changeRequest, changeRequest: data.changeRequest,
revision: data.revision, revision: data.revision,
visitorAuthToken: input.visitorAuthToken, });
},
);
const siteContext = {
...context,
linker: appendPrefixToLinker(context.linker, data.basePath),
};
return siteContext;
} }
/** /**
@@ -167,18 +215,19 @@ export async function fetchSiteContextByIds(
shareKey: string | undefined; shareKey: string | undefined;
changeRequest: string | undefined; changeRequest: string | undefined;
revision: string | undefined; revision: string | undefined;
visitorAuthToken: string | null; }
},
): Promise<GitBookSiteContext> { ): Promise<GitBookSiteContext> {
const { dataFetcher } = baseContext; const { dataFetcher } = baseContext;
const [{ site: orgSite, structure: siteStructure, customizations, scripts }, spaceContext] = const [{ site: orgSite, structure: siteStructure, customizations, scripts }, spaceContext] =
await Promise.all([ await Promise.all([
throwIfDataError(
dataFetcher.getPublishedContentSite({ dataFetcher.getPublishedContentSite({
organizationId: ids.organization, organizationId: ids.organization,
siteId: ids.site, siteId: ids.site,
siteShareKey: ids.shareKey, siteShareKey: ids.shareKey,
}), })
),
fetchSpaceContextByIds(baseContext, ids), fetchSpaceContextByIds(baseContext, ids),
]); ]);
@@ -189,7 +238,9 @@ export async function fetchSiteContextByIds(
...(customizations.site?.title ? { title: customizations.site.title } : {}), ...(customizations.site?.title ? { title: customizations.site.title } : {}),
}; };
const sections = ids.siteSection ? parseSiteSectionsList(siteStructure, ids.siteSection) : null; const sections = ids.siteSection
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
: null;
const siteSpace = ( const siteSpace = (
siteStructure.type === 'siteSpaces' && siteStructure.structure siteStructure.type === 'siteSpaces' && siteStructure.structure
@@ -211,11 +262,13 @@ export async function fetchSiteContextByIds(
if (siteSpaceSettings) { if (siteSpaceSettings) {
return siteSpaceSettings; return siteSpaceSettings;
} }
// We got the pointer from an API and customizations from another. // We got the pointer from an API and customizations from another.
// It's possible that the two are unsynced leading to not found customizations for the space. // It's possible that the two are unsynced leading to not found customizations for the space.
// It's better to fallback on customization of the site that displaying an error. // It's better to fallback on customization of the site that displaying an error.
console.warn('Customization not found for site space', ids.siteSpace); console.warn('Customization not found for site space', ids.siteSpace);
} }
return customizations.site; return customizations.site;
})(); })();
@@ -229,7 +282,6 @@ export async function fetchSiteContextByIds(
structure: siteStructure, structure: siteStructure,
sections, sections,
scripts, scripts,
visitorAuthToken: ids.visitorAuthToken,
}; };
} }
@@ -243,32 +295,51 @@ export async function fetchSpaceContextByIds(
shareKey: string | undefined; shareKey: string | undefined;
changeRequest: string | undefined; changeRequest: string | undefined;
revision: string | undefined; revision: string | undefined;
}, }
): Promise<GitBookSpaceContext> { ): Promise<GitBookSpaceContext> {
const { dataFetcher } = baseContext; const { dataFetcher } = baseContext;
const [space, changeRequest] = await Promise.all([ const [space, changeRequest] = await Promise.all([
throwIfDataError(
dataFetcher.getSpace({ dataFetcher.getSpace({
spaceId: ids.space, spaceId: ids.space,
shareKey: ids.shareKey, shareKey: ids.shareKey,
}), })
),
ids.changeRequest ids.changeRequest
? dataFetcher.getChangeRequest({ ? getDataOrNull(
dataFetcher.getChangeRequest({
spaceId: ids.space, spaceId: ids.space,
changeRequestId: ids.changeRequest, changeRequestId: ids.changeRequest,
}) })
)
: null, : null,
]); ]);
const revisionId = changeRequest?.revision ?? ids.revision ?? space.revision; if (ids.changeRequest && !changeRequest) {
// When trying to render a change request with an invalid / non-existing ID,
// we should return a 404.
notFound();
}
const pages = await dataFetcher.getRevisionPages({ const revisionId = ids.revision ?? changeRequest?.revision ?? space.revision;
const pages = await getDataOrNull(
dataFetcher.getRevisionPages({
spaceId: ids.space, spaceId: ids.space,
revisionId, revisionId,
// We only care about the Git metadata when the Git sync is enabled, // We only care about the Git metadata when the Git sync is enabled,
// otherwise we can optimize performance by not fetching it // otherwise we can optimize performance by not fetching it
metadata: !!space.gitSync, metadata: !!space.gitSync,
}); }),
// When trying to render a revision with an invalid / non-existing ID,
// we should handle gracefully the 404 and throw notFound.
ids.revision ? [404] : undefined
);
if (!pages) {
notFound();
}
return { return {
...baseContext, ...baseContext,
@@ -281,9 +352,39 @@ export async function fetchSpaceContextByIds(
}; };
} }
function parseSiteSectionsList(structure: SiteStructure, siteSectionId: string) { /**
const sections = getSiteStructureSections(structure); * Check if the context is the root one for a site.
const section = sections.find((section) => section.id === siteSectionId); * Meaning we are on the default section / space.
assert(section, 'A section must be defined when there are multiple sections'); */
return { list: sections, current: section } satisfies SiteSections; export function checkIsRootSiteContext(context: GitBookSiteContext): boolean {
const { structure } = context;
switch (structure.type) {
case 'sections': {
return getSiteStructureSections(structure, { ignoreGroups: true }).some(
(structure) =>
structure.default &&
structure.id === context.sections?.current.id &&
structure.siteSpaces.some(
(siteSpace) => siteSpace.default && siteSpace.id === context.siteSpace.id
)
);
}
case 'siteSpaces': {
return structure.structure.some(
(siteSpace) => siteSpace.default && siteSpace.id === context.siteSpace.id
);
}
}
}
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
const section = parseCurrentSection(structure, siteSectionId);
assert(section, 'A section must be defined when there are multiple sections');
return { list: sectionsAndGroups, current: section } satisfies SiteSections;
}
function parseCurrentSection(structure: SiteStructure, siteSectionId: string) {
const sections = getSiteStructureSections(structure, { ignoreGroups: true });
return sections.find((section) => section.id === siteSectionId);
} }
+434 -213
View File
@@ -1,465 +1,686 @@
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache'; import { trace } from '@/lib/tracing';
import { ComputedContentSource, GitBookAPI } from '@gitbook/api';
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
import { GitBookDataFetcher } from './types';
import { import {
getChangeRequestCacheTag, type ComputedContentSource,
getHostnameCacheTag, GitBookAPI,
getOpenAPISpecCacheTag, type GitBookAPIServiceBinding,
getSiteCacheTag, type RenderIntegrationUI,
getSpaceCacheTag, } from '@gitbook/api';
} from '../cache'; import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-tags';
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
import { DataFetcherError, wrapDataFetcherError } from './errors';
import { memoize } from './memoize';
import type { GitBookDataFetcher } from './types';
interface DataFetcherInput { interface DataFetcherInput {
/**
* API host to use.
*/
apiEndpoint: string;
/** /**
* API token. * API token.
*/ */
apiToken: string | null; apiToken: string | null;
} }
const commonInput: DataFetcherInput = {
apiEndpoint: GITBOOK_API_URL,
apiToken: GITBOOK_API_TOKEN,
};
/** /**
* Create a data fetcher using an API token. * Create a data fetcher using an API token.
* The data are being cached by Next.js built-in cache. * The data are being cached by Next.js built-in cache.
*/ */
export function createDataFetcher(input: DataFetcherInput = commonInput): GitBookDataFetcher { export function createDataFetcher(
input: DataFetcherInput = { apiToken: null }
): GitBookDataFetcher {
return { return {
apiEndpoint: input.apiEndpoint, async api() {
return apiClient(input);
},
withToken({ apiToken }) {
return createDataFetcher({
apiToken,
});
},
// //
// API that are tied to the token // API that are tied to the token
// //
getPublishedContentSite(params) { getPublishedContentSite(params) {
return getPublishedContentSite(input, { return trace('getPublishedContentSite', () =>
getPublishedContentSite(input, {
organizationId: params.organizationId, organizationId: params.organizationId,
siteId: params.siteId, siteId: params.siteId,
siteShareKey: params.siteShareKey, siteShareKey: params.siteShareKey,
}); })
);
}, },
getSiteRedirectBySource(params) { getSiteRedirectBySource(params) {
return getSiteRedirectBySource(input, { return trace('getSiteRedirectBySource', () =>
getSiteRedirectBySource(input, {
organizationId: params.organizationId, organizationId: params.organizationId,
siteId: params.siteId, siteId: params.siteId,
siteShareKey: params.siteShareKey, siteShareKey: params.siteShareKey,
source: params.source, source: params.source,
}); })
);
}, },
getRevision(params) { getRevision(params) {
return getRevision(input, { return trace('getRevision', () =>
getRevision(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
revisionId: params.revisionId, revisionId: params.revisionId,
metadata: params.metadata, metadata: params.metadata,
}); })
);
}, },
getRevisionPages(params) { getRevisionPages(params) {
return getRevisionPages(input, { return trace('getRevisionPages', () =>
getRevisionPages(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
revisionId: params.revisionId, revisionId: params.revisionId,
metadata: params.metadata, metadata: params.metadata,
}); })
);
}, },
getRevisionFile(params) { getRevisionFile(params) {
return getRevisionFile(input, { return trace('getRevisionFile', () =>
getRevisionFile(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
revisionId: params.revisionId, revisionId: params.revisionId,
fileId: params.fileId, fileId: params.fileId,
}); })
);
}, },
getRevisionPageByPath(params) { getRevisionPageByPath(params) {
return getRevisionPageByPath(input, { return trace('getRevisionPageByPath', () =>
getRevisionPageByPath(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
revisionId: params.revisionId, revisionId: params.revisionId,
path: params.path, path: params.path,
}); })
);
},
getRevisionPageMarkdown(params) {
return trace('getRevisionPageMarkdown', () =>
getRevisionPageMarkdown(input, {
spaceId: params.spaceId,
revisionId: params.revisionId,
pageId: params.pageId,
})
);
}, },
getReusableContent(params) { getReusableContent(params) {
return getReusableContent(input, { return trace('getReusableContent', () =>
getReusableContent(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
revisionId: params.revisionId, revisionId: params.revisionId,
reusableContentId: params.reusableContentId, reusableContentId: params.reusableContentId,
}); })
);
}, },
getLatestOpenAPISpecVersionContent(params) { getLatestOpenAPISpecVersionContent(params) {
return getLatestOpenAPISpecVersionContent(input, { return trace('getLatestOpenAPISpecVersionContent', () =>
getLatestOpenAPISpecVersionContent(input, {
organizationId: params.organizationId, organizationId: params.organizationId,
slug: params.slug, slug: params.slug,
}); })
);
}, },
getSpace(params) { getSpace(params) {
return getSpace(input, { return trace('getSpace', () =>
getSpace(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
shareKey: params.shareKey, shareKey: params.shareKey,
}); })
);
}, },
getChangeRequest(params) { getChangeRequest(params) {
return getChangeRequest(input, { return trace('getChangeRequest', () =>
getChangeRequest(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
changeRequestId: params.changeRequestId, changeRequestId: params.changeRequestId,
}); })
);
}, },
getDocument(params) { getDocument(params) {
return getDocument(input, { return trace('getDocument', () =>
getDocument(input, {
spaceId: params.spaceId, spaceId: params.spaceId,
documentId: params.documentId, documentId: params.documentId,
}); })
);
}, },
getComputedDocument(params) { getComputedDocument(params) {
return getComputedDocument(input, { return trace('getComputedDocument', () =>
getComputedDocument(input, {
organizationId: params.organizationId,
spaceId: params.spaceId, spaceId: params.spaceId,
source: params.source, source: params.source,
}); seed: params.seed,
})
);
},
getEmbedByUrl(params) {
return trace('getEmbedByUrl', () =>
getEmbedByUrl(input, {
url: params.url,
spaceId: params.spaceId,
})
);
},
searchSiteContent(params) {
return trace('searchSiteContent', () => searchSiteContent(input, params));
}, },
// renderIntegrationUi(params) {
// API that are not tied to the token return trace('renderIntegrationUi', () =>
// where the data is the same for all users renderIntegrationUi(input, {
// integrationName: params.integrationName,
getUserById(userId) { request: params.request,
return getUserById(commonInput, userId); })
);
}, },
getPublishedContentByUrl(params) {
return getPublishedContentByUrl(commonInput, { getUserById(userId) {
url: params.url, return trace('getUserById', () => getUserById(input, { userId }));
visitorAuthToken: params.visitorAuthToken,
redirectOnError: params.redirectOnError,
});
}, },
}; };
} }
async function getUserById(input: DataFetcherInput, userId: string) { const getUserById = memoize(async function getUserById(
input: DataFetcherInput,
params: { userId: string }
) {
'use cache'; 'use cache';
try { return trace('getUserById.uncached', () => {
const res = await getAPI(input).users.getUserById(userId); cacheLife('days');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.users.getUserById(params.userId);
return res.data; return res.data;
} catch (error) { });
if (checkHasErrorCode(error, 404)) { });
return null; });
}
throw error; const getSpace = memoize(async function getSpace(
}
}
async function getSpace(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
shareKey: string | undefined; shareKey: string | undefined;
}, }
) { ) {
'use cache'; 'use cache';
cacheTag(getSpaceCacheTag(params.spaceId)); return trace('getSpace.uncached', () => {
cacheLife('days');
cacheTag(
getCacheTag({
tag: 'space',
space: params.spaceId,
})
);
const res = await getAPI(input).spaces.getSpaceById(params.spaceId, { return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getSpaceById(params.spaceId, {
shareKey: params.shareKey, shareKey: params.shareKey,
}); });
return res.data; return res.data;
} });
});
});
async function getChangeRequest( const getChangeRequest = memoize(async function getChangeRequest(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
changeRequestId: string; changeRequestId: string;
}, }
) { ) {
'use cache'; 'use cache';
try { return trace('getChangeRequest.uncached', () => {
const res = await getAPI(input).spaces.getChangeRequestById( cacheLife('minutes');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getChangeRequestById(
params.spaceId, params.spaceId,
params.changeRequestId, params.changeRequestId
);
cacheTag(
getCacheTag({
tag: 'change-request',
space: params.spaceId,
changeRequest: res.data.id,
})
); );
cacheTag(getChangeRequestCacheTag(params.spaceId, res.data.id));
return res.data; return res.data;
} catch (error) { });
if (checkHasErrorCode(error, 404)) { });
return null; });
}
throw error; const getRevision = memoize(async function getRevision(
}
}
async function getRevision(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
metadata: boolean; metadata: boolean;
}, }
) { ) {
'use cache'; 'use cache';
const res = await getAPI(input).spaces.getRevisionById(params.spaceId, params.revisionId, { return trace('getRevision.uncached', () => {
cacheLife('max');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getRevisionById(params.spaceId, params.revisionId, {
metadata: params.metadata, metadata: params.metadata,
}); });
return res.data; return res.data;
} });
});
});
async function getRevisionPages( const getRevisionPages = memoize(async function getRevisionPages(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
metadata: boolean; metadata: boolean;
}, }
) { ) {
'use cache'; 'use cache';
const res = await getAPI(input).spaces.listPagesInRevisionById( return trace('getRevisionPages.uncached', () => {
cacheLife('max');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.listPagesInRevisionById(
params.spaceId, params.spaceId,
params.revisionId, params.revisionId,
{ {
metadata: params.metadata, metadata: params.metadata,
}, }
); );
return res.data.pages; return res.data.pages;
} });
});
});
async function getRevisionFile( const getRevisionFile = memoize(async function getRevisionFile(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
fileId: string; fileId: string;
}, }
) { ) {
'use cache'; 'use cache';
try { return trace('getRevisionFile.uncached', () => {
const res = await getAPI(input).spaces.getFileInRevisionById( cacheLife('max');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getFileInRevisionById(
params.spaceId, params.spaceId,
params.revisionId, params.revisionId,
params.fileId, params.fileId,
{}
); );
return res.data; return res.data;
} catch (error) { });
if (checkHasErrorCode(error, 404)) { });
return null; });
const getRevisionPageMarkdown = memoize(async function getRevisionPageMarkdown(
input: DataFetcherInput,
params: {
spaceId: string;
revisionId: string;
pageId: string;
}
) {
'use cache';
return trace('getRevisionPageMarkdown.uncached', () => {
cacheLife('max');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getPageInRevisionById(
params.spaceId,
params.revisionId,
params.pageId,
{
format: 'markdown',
}
);
if (!('markdown' in res.data)) {
throw new DataFetcherError('Page is not a document', 404);
} }
throw error; return res.data.markdown;
} });
} });
});
async function getRevisionPageByPath( const getRevisionPageByPath = memoize(async function getRevisionPageByPath(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
path: string; path: string;
}, }
) { ) {
'use cache'; 'use cache';
const encodedPath = encodeURIComponent(params.path); return trace('getRevisionPageByPath.uncached', () => {
cacheLife('max');
try { const encodedPath = encodeURIComponent(params.path);
const res = await getAPI(input).spaces.getPageInRevisionByPath( return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getPageInRevisionByPath(
params.spaceId, params.spaceId,
params.revisionId, params.revisionId,
encodedPath, encodedPath,
{}
); );
return res.data; return res.data;
} catch (error) { });
if (checkHasErrorCode(error, 404)) { });
return null; });
}
throw error; const getDocument = memoize(async function getDocument(
}
}
async function getDocument(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
documentId: string; documentId: string;
}, }
) { ) {
'use cache'; 'use cache';
const res = await getAPI(input).spaces.getDocumentById(params.spaceId, params.documentId); return trace('getDocument.uncached', () => {
return res.data; cacheLife('max');
}
async function getComputedDocument( return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getDocumentById(params.spaceId, params.documentId, {});
return res.data;
});
});
});
const getComputedDocument = memoize(async function getComputedDocument(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
organizationId: string;
source: ComputedContentSource; source: ComputedContentSource;
}, seed: string;
}
) { ) {
'use cache'; 'use cache';
const res = await getAPI(input).spaces.getComputedDocument(params.spaceId, { return trace('getComputedDocument.uncached', () => {
cacheLife('days');
cacheTag(
...getComputedContentSourceCacheTags(
{
spaceId: params.spaceId,
organizationId: params.organizationId,
},
params.source
)
);
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getComputedDocument(params.spaceId, {
source: params.source, source: params.source,
seed: params.seed,
}); });
return res.data; return res.data;
} });
});
});
async function getReusableContent( const getReusableContent = memoize(async function getReusableContent(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
reusableContentId: string; reusableContentId: string;
}, }
) { ) {
'use cache'; 'use cache';
try { return trace('getReusableContent.uncached', () => {
const res = await getAPI(input).spaces.getReusableContentInRevisionById( cacheLife('max');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getReusableContentInRevisionById(
params.spaceId, params.spaceId,
params.revisionId, params.revisionId,
params.reusableContentId, params.reusableContentId
); );
return res.data; return res.data;
} catch (error) { });
if (checkHasErrorCode(error, 404)) { });
return null; });
}
throw error; const getLatestOpenAPISpecVersionContent = memoize(
} async function getLatestOpenAPISpecVersionContent(
}
async function getLatestOpenAPISpecVersionContent(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
organizationId: string; organizationId: string;
slug: string; slug: string;
}, }
) { ) {
'use cache'; 'use cache';
cacheTag(getOpenAPISpecCacheTag(params.organizationId, params.slug)); return trace('getLatestOpenAPISpecVersionContent.uncached', () => {
cacheTag(
getCacheTag({
tag: 'openapi',
organization: params.organizationId,
openAPISpec: params.slug,
})
);
cacheLife('days');
try { return wrapDataFetcherError(async () => {
const res = await getAPI(input).orgs.getLatestOpenApiSpecVersionContent( const api = await apiClient(input);
const res = await api.orgs.getLatestOpenApiSpecVersionContent(
params.organizationId, params.organizationId,
params.slug, params.slug
); );
return res.data; return res.data;
} catch (error) {
if (checkHasErrorCode(error, 404)) {
return null;
}
throw error;
}
}
async function getPublishedContentByUrl(
input: DataFetcherInput,
params: {
url: string;
visitorAuthToken: string | null;
redirectOnError: boolean;
},
) {
'use cache';
const { url, visitorAuthToken, redirectOnError } = params;
const hostname = new URL(url).hostname;
cacheTag(getHostnameCacheTag(hostname));
const res = await getAPI(input).urls.getPublishedContentByUrl({
url,
visitorAuthToken: visitorAuthToken ?? undefined,
redirectOnError,
}); });
});
if ('site' in res.data) {
cacheTag(getSiteCacheTag(res.data.site));
} }
);
return res.data; const getPublishedContentSite = memoize(async function getPublishedContentSite(
}
async function getPublishedContentSite(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
organizationId: string; organizationId: string;
siteId: string; siteId: string;
siteShareKey: string | undefined; siteShareKey: string | undefined;
}, }
) { ) {
'use cache'; 'use cache';
cacheTag(getSiteCacheTag(params.siteId));
const res = await getAPI(input).orgs.getPublishedContentSite( return trace('getPublishedContentSite.uncached', () => {
cacheLife('days');
cacheTag(
getCacheTag({
tag: 'site',
site: params.siteId,
})
);
return trace('getPublishedContentSite', () => {
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.orgs.getPublishedContentSite(
params.organizationId, params.organizationId,
params.siteId, params.siteId,
{ {
shareKey: params.siteShareKey, shareKey: params.siteShareKey,
}, }
); );
return res.data; return res.data;
} });
});
});
});
async function getSiteRedirectBySource( const getSiteRedirectBySource = memoize(async function getSiteRedirectBySource(
input: DataFetcherInput, input: DataFetcherInput,
params: { params: {
organizationId: string; organizationId: string;
siteId: string; siteId: string;
siteShareKey: string | undefined; siteShareKey: string | undefined;
source: string; source: string;
}, }
) { ) {
'use cache'; 'use cache';
try { return trace('getSiteRedirectBySource.uncached', () => {
const res = await getAPI(input).orgs.getSiteRedirectBySource( cacheTag(
getCacheTag({
tag: 'site',
site: params.siteId,
})
);
cacheLife('days');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.orgs.getSiteRedirectBySource(
params.organizationId, params.organizationId,
params.siteId, params.siteId,
{ {
shareKey: params.siteShareKey, shareKey: params.siteShareKey,
source: params.source, source: params.source,
}, }
); );
return res.data; return res.data;
});
});
});
const getEmbedByUrl = memoize(async function getEmbedByUrl(
input: DataFetcherInput,
params: {
url: string;
spaceId: string;
}
) {
'use cache';
return trace('getEmbedByUrl.uncached', () => {
cacheLife('weeks');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.spaces.getEmbedByUrlInSpace(params.spaceId, { url: params.url });
return res.data;
});
});
});
const searchSiteContent = memoize(async function searchSiteContent(
input: DataFetcherInput,
params: Parameters<GitBookDataFetcher['searchSiteContent']>[0]
) {
'use cache';
return trace('searchSiteContent.uncached', () => {
const { organizationId, siteId, query, scope } = params;
cacheLife('days');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.orgs.searchSiteContent(organizationId, siteId, {
query,
...scope,
});
return res.data.items;
});
});
});
const renderIntegrationUi = memoize(async function renderIntegrationUi(
input: DataFetcherInput,
params: {
integrationName: string;
request: RenderIntegrationUI;
}
) {
'use cache';
return trace('renderIntegrationUi.uncached', () => {
cacheTag(getCacheTag({ tag: 'integration', integration: params.integrationName }));
cacheLife('days');
return wrapDataFetcherError(async () => {
const api = await apiClient(input);
const res = await api.integrations.renderIntegrationUiWithPost(
params.integrationName,
params.request
);
return res.data;
});
});
});
let loggedServiceBinding = false;
/**
* Create a new API client.
*/
export async function apiClient(input: DataFetcherInput = { apiToken: null }) {
const { apiToken } = input;
let serviceBinding: GitBookAPIServiceBinding | undefined;
try {
// HACK: This is a workaround to avoid webpack trying to bundle this cloudflare only module
// @ts-ignore
const { env } = await import(
/* webpackIgnore: true */ `${'__cloudflare:workers'.replaceAll('_', '')}`
);
serviceBinding = env.GITBOOK_API;
if (!loggedServiceBinding) {
loggedServiceBinding = true;
if (serviceBinding) {
// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`using service binding for the API (${GITBOOK_API_URL})`);
} else {
// biome-ignore lint/suspicious/noConsole: we want to log here
console.warn(`no service binding for the API (${GITBOOK_API_URL})`);
}
}
} catch (error) { } catch (error) {
// 422 is returned when the source is invalid if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
// we don't want to throw but just return null
if (checkHasErrorCode(error, 422)) {
return null;
}
if (checkHasErrorCode(error, 404)) {
return null;
}
throw error; throw error;
} }
} }
function getAPI(input: DataFetcherInput) {
const { apiEndpoint, apiToken } = input;
const api = new GitBookAPI({ const api = new GitBookAPI({
authToken: apiToken ?? undefined, authToken: apiToken || GITBOOK_API_TOKEN || undefined,
endpoint: apiEndpoint, endpoint: GITBOOK_API_URL,
userAgent: GITBOOK_USER_AGENT, userAgent: GITBOOK_USER_AGENT,
serviceBinding,
}); });
return api; return api;
} }
function checkHasErrorCode(error: unknown, code: number) {
return error instanceof Error && 'code' in error && error.code === code;
}
+115
View File
@@ -0,0 +1,115 @@
import { GitBookAPIError } from '@gitbook/api';
import type { DataFetcherErrorData, DataFetcherResponse } from './types';
export class DataFetcherError extends Error {
constructor(
message: string,
public readonly code: number
) {
super(message);
}
}
/**
* Throw an error if the response contains an error.
*/
export function throwIfDataError<T>(response: DataFetcherResponse<T>): T;
export function throwIfDataError<T>(response: Promise<DataFetcherResponse<T>>): Promise<T>;
export function throwIfDataError<T>(
response: DataFetcherResponse<T> | Promise<DataFetcherResponse<T>>
): T | Promise<T> {
if (response instanceof Promise) {
return response.then((result) => throwIfDataError(result));
}
if (response.error) {
throw new DataFetcherError(response.error.message, response.error.code);
}
return response.data;
}
/**
* Get the data from the response or null if there is an "Not found" error.
*/
export function getDataOrNull<T>(
response: DataFetcherResponse<T>,
ignoreErrors?: number[]
): T | null;
export function getDataOrNull<T>(
response: Promise<DataFetcherResponse<T>>,
ignoreErrors?: number[]
): Promise<T | null>;
export function getDataOrNull<T>(
response: DataFetcherResponse<T> | Promise<DataFetcherResponse<T>>,
ignoreErrors: number[] = [404]
): T | null | Promise<T | null> {
if (response instanceof Promise) {
return response.then((result) => getDataOrNull(result, ignoreErrors));
}
if (response.error) {
if (ignoreErrors.includes(response.error.code)) return null;
throw new DataFetcherError(response.error.message, response.error.code);
}
return response.data;
}
/**
* Ignore error for an API or data call.
*/
export async function ignoreDataThrownError<T>(promise: Promise<T>): Promise<T | null> {
try {
return await promise;
} catch (error) {
getExposableError(error as Error);
return null;
}
}
/**
* Ignore all errors for an API or data call.
*/
export async function ignoreAllThrownError<T>(promise: Promise<T>): Promise<T | null> {
try {
return await promise;
} catch (error) {
console.warn('Ignoring error', error);
return null;
}
}
/**
* Wrap an async execution to handle errors and return a DataFetcherResponse.
*/
export async function wrapDataFetcherError<T>(
fn: () => Promise<T>
): Promise<DataFetcherResponse<T>> {
try {
return { data: await fn() };
} catch (error) {
return {
error: getExposableError(error as Error),
};
}
}
/**
* Get a data fetcher exposable error from a JS error.
*/
export function getExposableError(error: Error): DataFetcherErrorData {
if (error instanceof GitBookAPIError) {
return {
code: error.code,
message: error.errorMessage,
};
}
if (error instanceof DataFetcherError) {
return {
code: error.code,
message: error.message,
};
}
throw error;
}
+3 -1
View File
@@ -1,4 +1,6 @@
export * from './api'; export * from './api';
export * from './types'; export * from './types';
export * from './utils'; export * from './pages';
export * from './urls'; export * from './urls';
export * from './errors';
export * from './lookup';
+140
View File
@@ -0,0 +1,140 @@
import { race, tryCatch } from '@/lib/async';
import { joinPath, joinPathWithBaseURL } from '@/lib/paths';
import { trace } from '@/lib/tracing';
import type { PublishedSiteContentLookup } from '@gitbook/api';
import { apiClient } from './api';
import { getExposableError } from './errors';
import type { DataFetcherResponse } from './types';
import { getURLLookupAlternatives, stripURLSearch } from './urls';
/**
* Lookup a content by its URL using the GitBook API.
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
*/
export async function getPublishedContentByURL(input: {
url: string;
visitorAuthToken: string | null;
redirectOnError: boolean;
apiToken: string | null;
}): Promise<DataFetcherResponse<PublishedSiteContentLookup>> {
const lookupURL = new URL(input.url);
const url = stripURLSearch(lookupURL);
const lookup = getURLLookupAlternatives(url);
const result = await race(lookup.urls, async (alternative, { signal }) => {
const api = await apiClient({ apiToken: input.apiToken });
const callResult = await trace(
{
operation: 'getPublishedContentByURL',
name: alternative.url,
},
() =>
tryCatch(
api.urls.getPublishedContentByUrl(
{
url: alternative.url,
visitorAuthToken: input.visitorAuthToken ?? undefined,
redirectOnError: input.redirectOnError,
// As this endpoint is cached by our API, we version the request
// to void getting stale data with missing properties.
// this could be improved by ensuring our API cache layer is versioned
// or invalidated when needed
// @ts-expect-error - cacheVersion is not a real query param
cacheVersion: 'v2',
},
{
signal,
}
)
)
);
if (callResult.error) {
if (alternative.primary) {
// We only return an error for the primary alternative (full URL),
// as other parts could result in errors due to the URL being incomplete (share links, etc).
return { error: callResult.error };
}
return null;
}
const {
data: { data },
} = callResult;
if ('redirect' in data) {
if (alternative.primary) {
// Append the path to the redirect URL
// because we might have matched a shorter path and the redirect is relative to it
if (alternative.extraPath) {
if (data.target === 'content') {
const redirect = new URL(data.redirect);
redirect.pathname = joinPath(redirect.pathname, alternative.extraPath);
data.redirect = redirect.toString();
} else {
const redirect = new URL(data.redirect);
if (redirect.searchParams.has('location')) {
redirect.searchParams.set(
'location',
joinPath(
redirect.searchParams.get('location') ?? '',
alternative.extraPath
)
);
data.redirect = redirect.toString();
}
}
}
return { data };
}
return null;
}
/**
* We use the following criteria to determine if the lookup result is the right one:
* - the primary alternative was resolved (because that's the longest or most inclusive path)
* - the resolution of the site URL is complete (because we want to resolve the deepest path possible)
*
* In both cases, the idea is to use the deepest/longest/most inclusive path to resolve the content.
*/
if (alternative.primary || ('site' in data && data.complete)) {
const changeRequest = data.changeRequest ?? lookup.changeRequest;
const revision = data.revision ?? lookup.revision;
const siteResult: PublishedSiteContentLookup = {
...data,
canonicalUrl: joinPathWithBaseURL(data.canonicalUrl, alternative.extraPath),
basePath: joinPath(data.basePath, lookup.basePath ?? ''),
pathname: joinPath(data.pathname, alternative.extraPath),
...(changeRequest ? { changeRequest } : {}),
...(revision ? { revision } : {}),
};
return { data: siteResult };
}
return null;
});
if (!result) {
return {
error: {
code: 404,
message: 'No content found',
},
};
}
if (result.error) {
return {
error: getExposableError(result.error),
};
}
return {
data: result.data,
};
}
@@ -0,0 +1,39 @@
import { describe, expect, it, mock } from 'bun:test';
import { memoize } from './memoize';
describe('memoize', () => {
it('should memoize the function', async () => {
const fn = mock(async () => Math.random());
const memoized = memoize(fn);
expect(await memoized()).toBe(await memoized());
});
it('should memoize the function with different arguments', async () => {
const fn = mock(async (a: number, b: number) => a + b);
const memoized = memoize(fn);
expect(await memoized(1, 2)).toBe(await memoized(1, 2));
expect(fn.mock.calls.length).toBe(1);
expect(await memoized(1, 2)).not.toBe(await memoized(2, 3));
expect(fn.mock.calls.length).toBe(2);
});
it('should memoize a function complex object', async () => {
const fn = mock(async (a: { foo: string; bar: number }) => a.foo + a.bar);
const memoized = memoize(fn);
expect(await memoized({ foo: 'foo', bar: 1 })).toBe(await memoized({ foo: 'foo', bar: 1 }));
expect(fn.mock.calls.length).toBe(1);
expect(await memoized({ foo: 'foo', bar: 1 })).not.toBe(
await memoized({ foo: 'foo', bar: 2 })
);
expect(fn.mock.calls.length).toBe(2);
});
it('should wrap concurrent async calls', async () => {
const fn = mock(async () => Math.random());
const memoized = memoize(fn);
const promise1 = memoized();
const promise2 = memoized();
expect(await promise1).toBe(await promise2);
expect(fn.mock.calls.length).toBe(1);
});
});
@@ -0,0 +1,43 @@
import pMemoize from 'p-memoize';
/**
* We wrap 'use cache' calls in a p-memoize function to avoid
* executing the function multiple times when doing concurrent calls.
*
* Hopefully one day this can be done directly by 'use cache'.
*/
export function memoize<F extends (...args: any[]) => any>(f: F): F {
return pMemoize(f, {
cacheKey: (args) => {
return JSON.stringify(deepSortValue(args));
},
});
}
function deepSortValue(value: unknown): unknown {
if (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean' ||
value === null ||
value === undefined
) {
return value;
}
if (Array.isArray(value)) {
return value.map(deepSortValue);
}
if (value && typeof value === 'object') {
return Object.entries(value)
.map(([key, subValue]) => {
return [key, deepSortValue(subValue)] as const;
})
.sort((a, b) => {
return a[0].localeCompare(b[0]);
});
}
return value;
}
+30
View File
@@ -0,0 +1,30 @@
import type { JSONDocument, RevisionPageDocument, Space } from '@gitbook/api';
import { getDataOrNull } from './errors';
import type { GitBookDataFetcher } from './types';
/**
* Get the document for a page.
*/
export async function getPageDocument(
dataFetcher: GitBookDataFetcher,
space: Space,
page: RevisionPageDocument
): Promise<JSONDocument | null> {
if (page.documentId) {
return getDataOrNull(
dataFetcher.getDocument({ spaceId: space.id, documentId: page.documentId })
);
}
if ('computed' in page && page.computed) {
return getDataOrNull(
dataFetcher.getComputedDocument({
organizationId: space.organization,
spaceId: space.id,
source: page.computed,
seed: page.computedSeed,
})
);
}
return null;
}
+82 -24
View File
@@ -1,28 +1,41 @@
import type * as api from '@gitbook/api'; import type * as api from '@gitbook/api';
export type DataFetcherErrorData = {
code: number;
message: string;
};
export type DataFetcherResponse<T> =
| {
data: T;
error?: undefined;
}
| {
error: DataFetcherErrorData;
data?: undefined;
};
/** /**
* Generic fetcher for GitBook data. * Generic fetcher for GitBook data.
* It is used between v1 and v2. * It is used between v1 and v2.
*/ */
export interface GitBookDataFetcher { export interface GitBookDataFetcher {
/** /**
* Endpoint of the API. * Get an API client for the current context.
*/ */
apiEndpoint: string; api(): Promise<api.GitBookAPI>;
/**
* Create a data fetcher authenticated with a specific token.
*/
withToken(input: {
apiToken: string;
}): GitBookDataFetcher;
/** /**
* Get a user by its ID. * Get a user by its ID.
*/ */
getUserById(userId: string): Promise<api.User | null>; getUserById(userId: string): Promise<DataFetcherResponse<api.User>>;
/**
* Get a published content by its URL.
*/
getPublishedContentByUrl(params: {
url: string;
visitorAuthToken: string | null;
redirectOnError: boolean;
}): Promise<api.PublishedSiteContentLookup>;
/** /**
* Get a published content site by its organization ID and site ID. * Get a published content site by its organization ID and site ID.
@@ -31,12 +44,14 @@ export interface GitBookDataFetcher {
organizationId: string; organizationId: string;
siteId: string; siteId: string;
siteShareKey: string | undefined; siteShareKey: string | undefined;
}): Promise<api.PublishedContentSite>; }): Promise<DataFetcherResponse<api.PublishedContentSite>>;
/** /**
* Get a space by its ID. * Get a space by its ID.
*/ */
getSpace(params: { spaceId: string; shareKey: string | undefined }): Promise<api.Space>; getSpace(params: { spaceId: string; shareKey: string | undefined }): Promise<
DataFetcherResponse<api.Space>
>;
/** /**
* Get a change request by its space ID and change request ID. * Get a change request by its space ID and change request ID.
@@ -44,7 +59,7 @@ export interface GitBookDataFetcher {
getChangeRequest(params: { getChangeRequest(params: {
spaceId: string; spaceId: string;
changeRequestId: string; changeRequestId: string;
}): Promise<api.ChangeRequest | null>; }): Promise<DataFetcherResponse<api.ChangeRequest>>;
/** /**
* Get the revision by its space ID and revision ID. * Get the revision by its space ID and revision ID.
@@ -53,7 +68,7 @@ export interface GitBookDataFetcher {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
metadata: boolean; metadata: boolean;
}): Promise<api.Revision>; }): Promise<DataFetcherResponse<api.Revision>>;
/** /**
* Get the revision pages by its space ID and revision ID. * Get the revision pages by its space ID and revision ID.
@@ -62,7 +77,7 @@ export interface GitBookDataFetcher {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
metadata: boolean; metadata: boolean;
}): Promise<api.RevisionPage[]>; }): Promise<DataFetcherResponse<api.RevisionPage[]>>;
/** /**
* Get a revision file by its space ID, revision ID and file ID. * Get a revision file by its space ID, revision ID and file ID.
@@ -71,7 +86,7 @@ export interface GitBookDataFetcher {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
fileId: string; fileId: string;
}): Promise<api.RevisionFile | null>; }): Promise<DataFetcherResponse<api.RevisionFile>>;
/** /**
* Get a revision page by its path. * Get a revision page by its path.
@@ -80,20 +95,33 @@ export interface GitBookDataFetcher {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
path: string; path: string;
}): Promise<api.RevisionPageDocument | api.RevisionPageGroup | null>; }): Promise<DataFetcherResponse<api.RevisionPageDocument | api.RevisionPageGroup>>;
/**
* Get the markdown content of a page by its path.
*/
getRevisionPageMarkdown(params: {
spaceId: string;
revisionId: string;
pageId: string;
}): Promise<DataFetcherResponse<string>>;
/** /**
* Get a document by its space ID and document ID. * Get a document by its space ID and document ID.
*/ */
getDocument(params: { spaceId: string; documentId: string }): Promise<api.JSONDocument>; getDocument(params: { spaceId: string; documentId: string }): Promise<
DataFetcherResponse<api.JSONDocument>
>;
/** /**
* Get a computed document by its space ID and computed source. * Get a computed document by its space ID and computed source.
*/ */
getComputedDocument(params: { getComputedDocument(params: {
organizationId: string;
spaceId: string; spaceId: string;
source: api.ComputedContentSource; source: api.ComputedContentSource;
}): Promise<api.JSONDocument>; seed: string;
}): Promise<DataFetcherResponse<api.JSONDocument>>;
/** /**
* Get a reusable content by its space ID, revision ID and reusable content ID. * Get a reusable content by its space ID, revision ID and reusable content ID.
@@ -102,7 +130,7 @@ export interface GitBookDataFetcher {
spaceId: string; spaceId: string;
revisionId: string; revisionId: string;
reusableContentId: string; reusableContentId: string;
}): Promise<api.RevisionReusableContent | null>; }): Promise<DataFetcherResponse<api.RevisionReusableContent>>;
/** /**
* Get the latest OpenAPI spec version content by its organization ID and slug. * Get the latest OpenAPI spec version content by its organization ID and slug.
@@ -110,7 +138,7 @@ export interface GitBookDataFetcher {
getLatestOpenAPISpecVersionContent(params: { getLatestOpenAPISpecVersionContent(params: {
organizationId: string; organizationId: string;
slug: string; slug: string;
}): Promise<api.OpenAPISpecContent | null>; }): Promise<DataFetcherResponse<api.OpenAPISpecContent>>;
/** /**
* Get a site redirect by its source path. * Get a site redirect by its source path.
@@ -120,5 +148,35 @@ export interface GitBookDataFetcher {
siteId: string; siteId: string;
siteShareKey: string | undefined; siteShareKey: string | undefined;
source: string; source: string;
}): Promise<{ redirect: api.SiteRedirect | null; target: string } | null>; }): Promise<DataFetcherResponse<{ redirect: api.SiteRedirect | null; target: string }>>;
/**
* Get an embed by its URL.
*/
getEmbedByUrl(params: { url: string; spaceId: string }): Promise<
DataFetcherResponse<api.Embed>
>;
/**
* Search content in a site.
*/
searchSiteContent(params: {
organizationId: string;
siteId: string;
query: string;
scope:
| { mode: 'all' }
| { mode: 'current'; siteSpaceId: string }
| { mode: 'specific'; siteSpaceIds: string[] };
/** Cache bust to ensure the search results are fresh when the space is updated. */
cacheBust?: string;
}): Promise<DataFetcherResponse<api.SearchSpaceResult[]>>;
/**
* Render an integration UI.
*/
renderIntegrationUi(params: {
integrationName: string;
request: api.RenderIntegrationUI;
}): Promise<DataFetcherResponse<api.ContentKitRenderOutput>>;
} }
+14 -14
View File
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'bun:test'; import { describe, expect, it } from 'bun:test';
import { getURLLookupAlternatives, normalizeURL } from './urls'; import { getURLLookupAlternatives, normalizeURL } from './urls';
@@ -68,7 +68,7 @@ describe('getURLLookupAlternatives', () => {
it('should not match before the variant for a variant url', () => { it('should not match before the variant for a variant url', () => {
expect( expect(
getURLLookupAlternatives(new URL('https://test.gitbook.io/v/variant/space')), getURLLookupAlternatives(new URL('https://test.gitbook.io/v/variant/space'))
).toEqual({ ).toEqual({
revision: undefined, revision: undefined,
changeRequest: undefined, changeRequest: undefined,
@@ -85,7 +85,7 @@ describe('getURLLookupAlternatives', () => {
it('should not match before the variant for a variant in a share link', () => { it('should not match before the variant for a variant in a share link', () => {
expect( expect(
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space')), getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space'))
).toEqual({ ).toEqual({
revision: undefined, revision: undefined,
changeRequest: undefined, changeRequest: undefined,
@@ -103,8 +103,8 @@ describe('getURLLookupAlternatives', () => {
it('should not match before a revision in a variant', () => { it('should not match before a revision in a variant', () => {
expect( expect(
getURLLookupAlternatives( getURLLookupAlternatives(
new URL('https://test.gitbook.io/v/variant/~/revisions/id/rest'), new URL('https://test.gitbook.io/v/variant/~/revisions/id/rest')
), )
).toEqual({ ).toEqual({
revision: 'id', revision: 'id',
changeRequest: undefined, changeRequest: undefined,
@@ -121,7 +121,7 @@ describe('getURLLookupAlternatives', () => {
it('should not match before a revision ID', () => { it('should not match before a revision ID', () => {
expect( expect(
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/revisions/id/a/b/c')), getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/revisions/id/a/b/c'))
).toEqual({ ).toEqual({
revision: 'id', revision: 'id',
changeRequest: undefined, changeRequest: undefined,
@@ -138,7 +138,7 @@ describe('getURLLookupAlternatives', () => {
it('should not match before a change request ID', () => { it('should not match before a change request ID', () => {
expect( expect(
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/changes/id/hello')), getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/changes/id/hello'))
).toEqual({ ).toEqual({
revision: undefined, revision: undefined,
changeRequest: 'id', changeRequest: 'id',
@@ -195,7 +195,7 @@ describe('getURLLookupAlternatives', () => {
it('should match a variant in a share-link', () => { it('should match a variant in a share-link', () => {
expect( expect(
getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space')), getURLLookupAlternatives(new URL('https://test.gitbook.io/sharelink/v/variant/space'))
).toEqual({ ).toEqual({
revision: undefined, revision: undefined,
changeRequest: undefined, changeRequest: undefined,
@@ -213,8 +213,8 @@ describe('getURLLookupAlternatives', () => {
it('should match a revision in a variant in a share-link', () => { it('should match a revision in a variant in a share-link', () => {
expect( expect(
getURLLookupAlternatives( getURLLookupAlternatives(
new URL('https://test.gitbook.io/sharelink/v/variant/~/revisions/id/a/b/c'), new URL('https://test.gitbook.io/sharelink/v/variant/~/revisions/id/a/b/c')
), )
).toEqual({ ).toEqual({
revision: 'id', revision: 'id',
changeRequest: undefined, changeRequest: undefined,
@@ -232,8 +232,8 @@ describe('getURLLookupAlternatives', () => {
it('should match a change request in a variant in a share-link', () => { it('should match a change request in a variant in a share-link', () => {
expect( expect(
getURLLookupAlternatives( getURLLookupAlternatives(
new URL('https://test.gitbook.io/sharelink/v/variant/~/changes/id/a/b/c'), new URL('https://test.gitbook.io/sharelink/v/variant/~/changes/id/a/b/c')
), )
).toEqual({ ).toEqual({
revision: undefined, revision: undefined,
changeRequest: 'id', changeRequest: 'id',
@@ -367,13 +367,13 @@ describe('getURLLookupAlternatives', () => {
describe('normalizeURL', () => { describe('normalizeURL', () => {
it('should remove trailing slashes', () => { it('should remove trailing slashes', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com/hello/'))).toEqual( expect(normalizeURL(new URL('https://docs.mycompany.com/hello/'))).toEqual(
new URL('https://docs.mycompany.com/hello'), new URL('https://docs.mycompany.com/hello')
); );
}); });
it('should remove duplicate slashes', () => { it('should remove duplicate slashes', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com//hello//there'))).toEqual( expect(normalizeURL(new URL('https://docs.mycompany.com//hello//there'))).toEqual(
new URL('https://docs.mycompany.com/hello/there'), new URL('https://docs.mycompany.com/hello/there')
); );
}); });
}); });
+1 -123
View File
@@ -1,125 +1,3 @@
import { race, tryCatch } from '@/lib/async';
import { joinPath } from '@/lib/paths';
import { GitBookAPI, type GitBookAPIError, type PublishedSiteContentLookup } from '@gitbook/api';
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
/**
* Lookup a content by its URL using the GitBook API.
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
*/
export async function getPublishedContentByURL(input: {
url: string;
visitorAuthToken: string | null;
redirectOnError: boolean;
}): Promise<
| { data: PublishedSiteContentLookup; error?: undefined }
| { data?: undefined; error: Error | GitBookAPIError }
> {
const lookupURL = new URL(input.url);
const url = stripURLSearch(lookupURL);
const lookup = getURLLookupAlternatives(url);
console.log(
`lookup content for url "${url.toString()}", with ${lookup.urls.length} alternatives`,
);
const result = await race(lookup.urls, async (alternative, { signal }) => {
const api = new GitBookAPI({
authToken: GITBOOK_API_TOKEN ?? undefined,
endpoint: GITBOOK_API_URL,
userAgent: GITBOOK_USER_AGENT,
});
const callResult = await tryCatch(
api.urls.getPublishedContentByUrl(
{
url: alternative.url,
visitorAuthToken: input.visitorAuthToken ?? undefined,
redirectOnError: input.redirectOnError,
cache: true,
},
{
signal,
headers: {
'x-gitbook-force-cache': 'true',
},
},
),
);
if (callResult.error) {
if (alternative.primary) {
// We only return an error for the primary alternative (full URL),
// as other parts could result in errors due to the URL being incomplete (share links, etc).
return { error: callResult.error };
}
return null;
}
const {
data: { data },
} = callResult;
if ('redirect' in data) {
if (alternative.primary) {
// Append the path to the redirect URL
// because we might have matched a shorter path and the redirect is relative to it
if (alternative.extraPath) {
if (data.target === 'content') {
const redirect = new URL(data.redirect);
redirect.pathname = joinPath(redirect.pathname, alternative.extraPath);
data.redirect = redirect.toString();
} else {
const redirect = new URL(data.redirect);
if (redirect.searchParams.has('location')) {
redirect.searchParams.set(
'location',
joinPath(
redirect.searchParams.get('location') ?? '',
alternative.extraPath,
),
);
data.redirect = redirect.toString();
}
}
}
return { data };
}
return null;
}
/**
* We use the following criteria to determine if the lookup result is the right one:
* - the primary alternative was resolved (because that's the longest or most inclusive path)
* - the resolution of the site URL is complete (because we want to resolve the deepest path possible)
*
* In both cases, the idea is to use the deepest/longest/most inclusive path to resolve the content.
*/
if (alternative.primary || ('site' in data && data.complete)) {
const siteResult: PublishedSiteContentLookup = {
...data,
changeRequest: data.changeRequest ?? lookup.changeRequest,
revision: data.revision ?? lookup.revision,
basePath: joinPath(data.basePath, lookup.basePath ?? ''),
pathname: joinPath(data.pathname, alternative.extraPath),
};
return { data: siteResult };
}
return null;
});
if (!result) {
return {
error: new Error('No content found'),
};
}
return result;
}
/** /**
* For a given GitBook URL, return a list of alternative URLs that could be matched against to lookup the content. * For a given GitBook URL, return a list of alternative URLs that could be matched against to lookup the content.
* The approach is optimized to aim at reusing cached lookup results as much as possible. * The approach is optimized to aim at reusing cached lookup results as much as possible.
@@ -155,7 +33,7 @@ export function getURLLookupAlternatives(input: URL) {
throw new Error( throw new Error(
`Invalid extraPath ${extraPath} for url ${adding.toString()}, already set to ${ `Invalid extraPath ${extraPath} for url ${adding.toString()}, already set to ${
existing.extraPath existing.extraPath
}`, }`
); );
} }
return; return;
-19
View File
@@ -1,19 +0,0 @@
import { RevisionPageDocument } from '@gitbook/api';
import { GitBookDataFetcher } from './types';
/**
* Get the document for a page.
*/
export async function getPageDocument(
dataFetcher: GitBookDataFetcher,
spaceId: string,
page: RevisionPageDocument,
) {
if (page.documentId) {
return dataFetcher.getDocument({ spaceId, documentId: page.documentId });
} else if (page.computed) {
return dataFetcher.getComputedDocument({ spaceId, source: page.computed });
}
return null;
}
-38
View File
@@ -1,38 +0,0 @@
/**
* Main host on which GitBook is running.
*/
export const GITBOOK_URL =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: ((process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : undefined) ??
process.env.GITBOOK_URL ??
'');
/**
* GitBook app URL.
*/
export const GITBOOK_APP_URL = process.env.NEXT_PUBLIC_GITBOOK_APP_URL ?? `https://app.gitbook.com`;
/**
* Default GitBook API URL endpoint.
*/
export const GITBOOK_API_URL = process.env.GITBOOK_API_URL ?? 'https://api.gitbook.com';
/**
* Default GitBook API token.
* It can be use to avoid rate-limiting.
*/
export const GITBOOK_API_TOKEN = process.env.GITBOOK_API_TOKEN ?? null;
/**
* User agent to use for API requests.
*/
export const GITBOOK_USER_AGENT = process.env.GITBOOK_USER_AGENT ?? 'GitBook-Open/2.0.0';
/**
* Whether to disable tracking of events into site insights.
* This is used to disable tracking in development mode.
*/
export const GITBOOK_DISABLE_TRACKING = Boolean(
!!process.env.GITBOOK_DISABLE_TRACKING || process.env.NODE_ENV !== 'production',
);
+94
View File
@@ -0,0 +1,94 @@
import 'server-only';
/*
* Support both Cloudflare and Vercel, environment variables can be bundled.
* To avoid leaking them on the client-side, they should be accessed from this file
* and not from the `process.env` object.
*/
/**
* Main host on which GitBook is running.
*/
export const GITBOOK_URL =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: (process.env.GITBOOK_URL ??
(process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : undefined) ??
'');
/**
* URL at which static assets are served.
*/
export const GITBOOK_ASSETS_URL =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: process.env.GITBOOK_ASSETS_PREFIX;
/**
* GitBook app URL.
*/
export const GITBOOK_APP_URL = process.env.GITBOOK_APP_URL || 'https://app.gitbook.com';
/**
* Default GitBook API URL endpoint.
*/
export const GITBOOK_API_URL = process.env.GITBOOK_API_URL || 'https://api.gitbook.com';
/**
* Default GitBook API URL endpoint, to be shared with the client.
*/
export const GITBOOK_API_PUBLIC_URL = process.env.GITBOOK_API_PUBLIC_URL || GITBOOK_API_URL;
/**
* Default GitBook API token.
* It can be use to avoid rate-limiting.
*/
export const GITBOOK_API_TOKEN = process.env.GITBOOK_API_TOKEN || null;
/**
* User agent to use for API requests.
*/
export const GITBOOK_USER_AGENT = process.env.GITBOOK_USER_AGENT || 'GitBook-Open/2.0.0';
/**
* Whether to disable tracking of events into site insights.
* This is used to disable tracking in development mode.
*/
export const GITBOOK_DISABLE_TRACKING = Boolean(
!!process.env.GITBOOK_DISABLE_TRACKING || process.env.NODE_ENV !== 'production'
);
/**
* Hostname serving the integrations.
*/
export const GITBOOK_INTEGRATIONS_HOST =
process.env.GITBOOK_INTEGRATIONS_HOST || 'integrations.gitbook.com';
/**
* Hostname for fonts.
*/
export const GITBOOK_FONTS_URL = process.env.GITBOOK_FONTS_URL || 'https://fonts.gitbook.com';
/**
* Endpoint to use for resizing images.
* It should be a Cloudflare domain with image resizing enabled.
*/
export const GITBOOK_IMAGE_RESIZE_URL = process.env.GITBOOK_IMAGE_RESIZE_URL ?? null;
export const GITBOOK_IMAGE_RESIZE_SIGNING_KEY =
process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY ?? null;
/**
* Endpoint where icons are served.
*/
export const GITBOOK_ICONS_URL =
process.env.GITBOOK_ICONS_URL || `${GITBOOK_ASSETS_URL || ''}/~gitbook/static/icons`;
/**
* Token passed to the icons endpoint.
*/
export const GITBOOK_ICONS_TOKEN = process.env.GITBOOK_ICONS_TOKEN;
/**
* Secret used to validate requests from the GitBook app.
*/
export const GITBOOK_SECRET = process.env.GITBOOK_SECRET ?? null;
+2
View File
@@ -0,0 +1,2 @@
export * from './globals';
export * from './urls';
+45
View File
@@ -0,0 +1,45 @@
import { GITBOOK_ASSETS_URL, GITBOOK_URL } from './globals';
/**
* Check if the URL is a GitBook host URL.
*/
export function isGitBookHostURL(input: URL | string): boolean {
const url = typeof input === 'string' ? new URL(input) : input;
if (!GITBOOK_URL) {
return false;
}
const gitbookHost = new URL(GITBOOK_URL).host;
if (url.host === gitbookHost) {
return true;
}
// Handle the Cloudflare preview URLs that are prefixed with a random hash
// https://developers.cloudflare.com/workers/configuration/previews/
if (url.host.endsWith(`-${gitbookHost}`)) {
return true;
}
return false;
}
/**
* Check if the URL is a GitBook assets host URL.
*/
export function isGitBookAssetsHostURL(input: URL | string): boolean {
const url = typeof input === 'string' ? new URL(input) : input;
if (!GITBOOK_ASSETS_URL) {
return false;
}
const gitbookAssetsHost = new URL(GITBOOK_ASSETS_URL).host;
if (url.host === gitbookAssetsHost) {
return true;
}
return false;
}
@@ -1,8 +1,9 @@
import 'server-only'; import 'server-only';
import { GITBOOK_IMAGE_RESIZE_SIGNING_KEY, GITBOOK_IMAGE_RESIZE_URL } from '../env';
import type { GitBookLinker } from '../links';
import { type SignatureVersion, generateImageSignature } from './signatures';
import type { ImageResizer } from './types'; import type { ImageResizer } from './types';
import { generateImageSignature, SignatureVersion } from './signatures';
import { GitBookSpaceLinker } from '../links';
interface CloudflareImageJsonFormat { interface CloudflareImageJsonFormat {
width: number; width: number;
@@ -32,16 +33,20 @@ export interface CloudflareImageOptions {
* Create an image resizer for a rendering context. * Create an image resizer for a rendering context.
*/ */
export function createImageResizer({ export function createImageResizer({
host, imagesContextId,
linker, linker,
}: { }: {
/** The linker to use to create URLs. */ /** The linker to use to create URLs. */
linker: GitBookSpaceLinker; linker: GitBookLinker;
/** The host name of the current site. */ /** The site identifier to use for verifying the image signature. */
host: string; imagesContextId: string;
}): ImageResizer { }): ImageResizer {
if (!GITBOOK_IMAGE_RESIZE_URL || !GITBOOK_IMAGE_RESIZE_SIGNING_KEY) {
return createNoopImageResizer();
}
return { return {
resize: (urlInput) => { getResizedImageURL: (urlInput) => {
if (!checkIsSizableImageURL(urlInput)) { if (!checkIsSizableImageURL(urlInput)) {
return null; return null;
} }
@@ -53,30 +58,31 @@ export function createImageResizer({
return async (options) => { return async (options) => {
cachedSignature ??= await generateImageSignature({ cachedSignature ??= await generateImageSignature({
host, imagesContextId,
url: urlInput, url: urlInput,
}); });
const url = new URL(linker.toAbsoluteURL('/~gitbook/image')); const url = linker.toAbsoluteURL(linker.toPathInSite('/~gitbook/image'));
url.searchParams.set('url', getImageAPIUrl(urlInput)); const searchParams = new URLSearchParams();
searchParams.set('url', getImageAPIUrl(urlInput));
if (options.width) { if (options.width) {
url.searchParams.set('width', options.width.toString()); searchParams.set('width', options.width.toString());
} }
if (options.height) { if (options.height) {
url.searchParams.set('height', options.height.toString()); searchParams.set('height', options.height.toString());
} }
if (options.dpr) { if (options.dpr) {
url.searchParams.set('dpr', options.dpr.toString()); searchParams.set('dpr', options.dpr.toString());
} }
if (options.quality) { if (options.quality) {
url.searchParams.set('quality', options.quality.toString()); searchParams.set('quality', options.quality.toString());
} }
url.searchParams.set('sign', cachedSignature.signature); searchParams.set('sign', cachedSignature.signature);
url.searchParams.set('sv', cachedSignature.version); searchParams.set('sv', cachedSignature.version);
return url.toString(); return `${url}?${searchParams.toString()}`;
}; };
}, },
@@ -95,18 +101,11 @@ export function createImageResizer({
*/ */
export function createNoopImageResizer(): ImageResizer { export function createNoopImageResizer(): ImageResizer {
return { return {
resize: () => null, getResizedImageURL: () => null,
getImageSize: async (input) => null, getImageSize: async (_input) => null,
}; };
} }
/**
* Return true if images resizing is enabled.
*/
export function isImageResizingEnabled(): boolean {
return !!process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY;
}
/** /**
* Check if a URL is an HTTP URL. * Check if a URL is an HTTP URL.
*/ */
@@ -149,9 +148,9 @@ export function checkIsSizableImageURL(input: string): boolean {
*/ */
export async function getImageSize( export async function getImageSize(
input: string, input: string,
defaultSize: Partial<CloudflareImageOptions> = {}, defaultSize: Partial<CloudflareImageOptions> = {}
): Promise<{ width: number; height: number } | null> { ): Promise<{ width: number; height: number } | null> {
if (!isImageResizingEnabled() || !checkIsSizableImageURL(input)) { if (!checkIsSizableImageURL(input)) {
return null; return null;
} }
@@ -171,12 +170,7 @@ export async function getImageSize(
width: json.original.width, width: json.original.width,
height: json.original.height, height: json.original.height,
}; };
} catch (error) { } catch (_error) {
console.error(
`failed to fetch image size for ${input}: ${
(error as Error).stack ?? (error as Error).message ?? error
}`,
);
return null; return null;
} }
} }
@@ -188,7 +182,7 @@ export async function resizeImage(
input: string, input: string,
options: CloudflareImageOptions & { options: CloudflareImageOptions & {
signal?: AbortSignal; signal?: AbortSignal;
}, }
): Promise<Response> { ): Promise<Response> {
const { signal, ...resizeOptions } = options; const { signal, ...resizeOptions } = options;
@@ -203,10 +197,13 @@ export async function resizeImage(
// Since Cloudflare Images options on fetch are not supported on Cloudflare Pages, // Since Cloudflare Images options on fetch are not supported on Cloudflare Pages,
// we need to use the Cloudflare Image Resize API directly. // we need to use the Cloudflare Image Resize API directly.
if (process.env.GITBOOK_IMAGE_RESIZE_URL) { if (!GITBOOK_IMAGE_RESIZE_URL) {
const response = await fetch( throw new Error('GITBOOK_IMAGE_RESIZE_URL is not set');
`${process.env.GITBOOK_IMAGE_RESIZE_URL}${stringifyOptions( }
resizeOptions,
return await fetch(
`${GITBOOK_IMAGE_RESIZE_URL}${stringifyOptions(
resizeOptions
)}/${encodeURIComponent(input)}`, )}/${encodeURIComponent(input)}`,
{ {
headers: { headers: {
@@ -217,19 +214,8 @@ export async function resizeImage(
: `image/${resizeOptions.format || 'jpeg'}`, : `image/${resizeOptions.format || 'jpeg'}`,
}, },
signal, signal,
},
);
return response;
} }
);
return fetch(parsed, {
// @ts-ignore
cf: {
image: resizeOptions,
},
signal,
});
} }
function stringifyOptions(options: CloudflareImageOptions): string { function stringifyOptions(options: CloudflareImageOptions): string {
@@ -0,0 +1,14 @@
/**
* Get the site identifier to use for image resizing for an incoming request.
* This identifier can be obtained before resolving the request URL.
*/
export function getImageResizingContextId(url: URL): string {
if (url.host === 'proxy.gitbook.site' || url.host === 'proxy.gitbook-staging.site') {
// For proxy requests, we extract the site ID from the pathname
// e.g. https://proxy.gitbook.site/site/siteId/...
const pathname = url.pathname.slice(1).split('/');
return pathname.slice(0, 2).join('/');
}
return url.host;
}
@@ -2,3 +2,4 @@ export * from './types';
export * from './createImageResizer'; export * from './createImageResizer';
export * from './signatures'; export * from './signatures';
export * from './utils'; export * from './utils';
export * from './getImageResizingContextId';
@@ -2,6 +2,8 @@ import 'server-only';
import fnv1a from '@sindresorhus/fnv1a'; import fnv1a from '@sindresorhus/fnv1a';
import type { MaybePromise } from 'p-map'; import type { MaybePromise } from 'p-map';
import { assert } from 'ts-essentials';
import { GITBOOK_IMAGE_RESIZE_SIGNING_KEY } from '../env';
/** /**
* GitBook has supported different version of image signing in the past. To maintain backwards * GitBook has supported different version of image signing in the past. To maintain backwards
@@ -16,7 +18,7 @@ export const CURRENT_SIGNATURE_VERSION: SignatureVersion = '2';
type SignFnInput = { type SignFnInput = {
url: string; url: string;
host: string; imagesContextId: string;
}; };
type SignFn = (input: SignFnInput) => MaybePromise<string>; type SignFn = (input: SignFnInput) => MaybePromise<string>;
@@ -26,7 +28,7 @@ type SignFn = (input: SignFnInput) => MaybePromise<string>;
*/ */
export async function verifyImageSignature( export async function verifyImageSignature(
input: SignFnInput, input: SignFnInput,
{ signature, version }: { signature: string; version: SignatureVersion }, { signature, version }: { signature: string; version: SignatureVersion }
): Promise<boolean> { ): Promise<boolean> {
const generator = IMAGE_SIGNATURE_FUNCTIONS[version]; const generator = IMAGE_SIGNATURE_FUNCTIONS[version];
const generated = await generator(input); const generated = await generator(input);
@@ -55,10 +57,11 @@ const fnv1aUtf8Buffer = new Uint8Array(512);
* The signature is relative to the current site being rendered to avoid serving images from other sites on the same domain. * The signature is relative to the current site being rendered to avoid serving images from other sites on the same domain.
*/ */
const generateSignatureV2: SignFn = async (input) => { const generateSignatureV2: SignFn = async (input) => {
assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
const all = [ const all = [
input.url, input.url,
input.host, // The hostname is used to avoid serving images from other sites on the same domain input.imagesContextId, // The hostname is used to avoid serving images from other sites on the same domain
process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY, GITBOOK_IMAGE_RESIZE_SIGNING_KEY,
] ]
.filter(Boolean) .filter(Boolean)
.join(':'); .join(':');
@@ -74,7 +77,8 @@ const fnv1aUtf8BufferV1 = new Uint8Array(512);
* to know that it was the algorithm that was used. * to know that it was the algorithm that was used.
*/ */
const generateSignatureV1: SignFn = async (input) => { const generateSignatureV1: SignFn = async (input) => {
const all = [input.url, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':'); assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
const all = [input.url, GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
return fnv1a(all, { utf8Buffer: fnv1aUtf8BufferV1 }).toString(16); return fnv1a(all, { utf8Buffer: fnv1aUtf8BufferV1 }).toString(16);
}; };
@@ -84,7 +88,8 @@ const generateSignatureV1: SignFn = async (input) => {
* but still exist in previously generated and cached content. * but still exist in previously generated and cached content.
*/ */
const generateSignatureV0: SignFn = async (input) => { const generateSignatureV0: SignFn = async (input) => {
const all = [input.url, process.env.GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':'); assert(GITBOOK_IMAGE_RESIZE_SIGNING_KEY, 'GITBOOK_IMAGE_RESIZE_SIGNING_KEY is not set');
const all = [input.url, GITBOOK_IMAGE_RESIZE_SIGNING_KEY].filter(Boolean).join(':');
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all)); const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(all));
// Convert ArrayBuffer to hex string // Convert ArrayBuffer to hex string
+2 -2
View File
@@ -20,10 +20,10 @@ export type ImageResizer = {
* @param input - The image URL to resize. * @param input - The image URL to resize.
* @param options - The options to resize the image. * @param options - The options to resize the image.
*/ */
resize(input: string): null | ((options: ResizeImageOptions) => Promise<string>); getResizedImageURL(imageURL: string): null | ((options: ResizeImageOptions) => Promise<string>);
/** /**
* Get the size of an image. * Get the size of an image.
*/ */
getImageSize(input: string, options: GetImageSizeOptions): Promise<ImageSize | null>; getImageSize(imageURL: string, options: GetImageSizeOptions): Promise<ImageSize | null>;
}; };
+4 -4
View File
@@ -1,14 +1,14 @@
import { ImageResizer, ResizeImageOptions } from './types'; import type { ImageResizer, ResizeImageOptions } from './types';
/** /**
* Quick utility to get a resized image URL. * Quick utility to get a resized image URL.
*/ */
export async function getResizedImageURL( export async function getResizedImageURL(
resizer: ImageResizer, resizer: ImageResizer | undefined,
url: string, url: string,
options: ResizeImageOptions, options: ResizeImageOptions
) { ) {
const getURL = resizer.resize(url); const getURL = resizer?.getResizedImageURL(url);
if (!getURL) { if (!getURL) {
return url; return url;
} }
+74
View File
@@ -0,0 +1,74 @@
import { describe, expect, it } from 'bun:test';
import { createLinker } from './links';
const root = createLinker({
host: 'docs.company.com',
spaceBasePath: '/',
siteBasePath: '/',
});
const variantInSection = createLinker({
host: 'docs.company.com',
spaceBasePath: '/section/variant',
siteBasePath: '/',
});
const siteGitBookIO = createLinker({
host: 'org.gitbook.io',
spaceBasePath: '/sitename/variant/',
siteBasePath: '/sitename/',
});
describe('toPathInContent', () => {
it('should return the correct path', () => {
expect(root.toPathInSpace('some/path')).toBe('/some/path');
expect(variantInSection.toPathInSpace('some/path')).toBe('/section/variant/some/path');
});
it('should handle leading slash', () => {
expect(root.toPathInSpace('/some/path')).toBe('/some/path');
expect(variantInSection.toPathInSpace('/some/path')).toBe('/section/variant/some/path');
});
});
describe('toPathInSite', () => {
it('should return the correct path', () => {
expect(root.toPathInSite('some/path')).toBe('/some/path');
expect(siteGitBookIO.toPathInSite('some/path')).toBe('/sitename/some/path');
});
});
describe('toRelativePathInSite', () => {
it('should return the correct path', () => {
expect(root.toRelativePathInSite('/some/path')).toBe('some/path');
expect(siteGitBookIO.toRelativePathInSite('/sitename/some/path')).toBe('some/path');
});
it('should preserve absolute paths outside of the site', () => {
expect(siteGitBookIO.toRelativePathInSite('/outside/some/path')).toBe('/outside/some/path');
});
});
describe('toAbsoluteURL', () => {
it('should return the correct path', () => {
expect(root.toAbsoluteURL('some/path')).toBe('https://docs.company.com/some/path');
expect(variantInSection.toAbsoluteURL('some/path')).toBe(
'https://docs.company.com/some/path'
);
});
});
describe('toLinkForContent', () => {
it('should return the correct path', () => {
expect(root.toLinkForContent('https://docs.company.com/some/path')).toBe('/some/path');
expect(siteGitBookIO.toLinkForContent('https://org.gitbook.io/sitename/some/path')).toBe(
'/sitename/some/path'
);
});
it('should preserve an absolute URL if the site is not the same', () => {
expect(siteGitBookIO.toLinkForContent('https://org.gitbook.io/anothersite/some/path')).toBe(
'https://org.gitbook.io/anothersite/some/path'
);
});
});
+78 -42
View File
@@ -1,17 +1,39 @@
import { RevisionPage, RevisionPageDocument, RevisionPageGroup } from '@gitbook/api';
import { getPagePath } from '@/lib/pages'; import { getPagePath } from '@/lib/pages';
import { withLeadingSlash, withTrailingSlash } from '@/lib/paths';
import type { RevisionPage, RevisionPageDocument, RevisionPageGroup } from '@gitbook/api';
import warnOnce from 'warn-once';
/** /**
* Generic interface to generate links based on a given context. * Generic interface to generate links based on a given context.
*
* URL levels:
*
* https://docs.company.com/basename/section/variant/page
*
* toPathInSpace('some/path') => /basename/section/variant/some/path
* toPathInSite('some/path') => /basename/some/path
* toPathForPage({ pages, page }) => /basename/section/variant/some/path
* toAbsoluteURL('some/path') => https://docs.company.com/some/path
*/ */
export interface GitBookSpaceLinker { export interface GitBookLinker {
/** /**
* Generate an absolute path for a relative path in the current space content. * Generate an absolute path for a relative path to the current content.
*/ */
toPathInSpace(relativePath: string): string; toPathInSpace(relativePath: string): string;
/** /**
* Generate an absolute path for a page in the current space. * Generate an absolute path for a relative path to the current site.
*/
toPathInSite(relativePath: string): string;
/**
* Transform an absolute path in a site, to a relative path from the root of the site.
*/
toRelativePathInSite(absolutePath: string): string;
/**
* Generate an absolute path for a page in the current content.
* The result should NOT be passed to `toPathInContent`.
*/ */
toPathForPage(input: { toPathForPage(input: {
pages: RevisionPage[]; pages: RevisionPage[];
@@ -20,9 +42,14 @@ export interface GitBookSpaceLinker {
}): string; }): string;
/** /**
* Generate an absolute URL for a given path. * Generate an absolute URL for a given path relative to the host of the current content.
*/ */
toAbsoluteURL(absolutePath: string): string; toAbsoluteURL(absolutePath: string): string;
/**
* Generate a link (URL or path) for a GitBook content URL (url of another site)
*/
toLinkForContent(url: string): string;
} }
/** /**
@@ -32,60 +59,69 @@ export function createLinker(
/** Where the top of the space is served on */ /** Where the top of the space is served on */
servedOn: { servedOn: {
protocol?: string; protocol?: string;
host: string; host?: string;
pathname: string;
/** The base path of the space */
spaceBasePath: string;
/** The base path of the site */
siteBasePath: string;
}
): GitBookLinker {
warnOnce(!servedOn.host, 'No host provided to createLinker. It can lead to issues with links.');
const siteBasePath = withTrailingSlash(withLeadingSlash(servedOn.siteBasePath));
const spaceBasePath = withTrailingSlash(withLeadingSlash(servedOn.spaceBasePath));
const linker: GitBookLinker = {
toPathInSpace(relativePath: string): string {
return joinPaths(spaceBasePath, relativePath);
}, },
): GitBookSpaceLinker {
if (servedOn.host.includes('/')) { toPathInSite(relativePath: string): string {
throw new Error('Host cannot include a slash'); return joinPaths(siteBasePath, relativePath);
},
toRelativePathInSite(absolutePath: string): string {
const normalizedPath = withLeadingSlash(absolutePath);
if (!normalizedPath.startsWith(servedOn.siteBasePath)) {
return normalizedPath;
} }
const linker: GitBookSpaceLinker = { return normalizedPath.slice(servedOn.siteBasePath.length);
toPathInSpace(relativePath: string): string {
return joinPaths(servedOn.pathname, relativePath);
}, },
toAbsoluteURL(absolutePath: string): string { toAbsoluteURL(absolutePath: string): string {
if (!servedOn.host) {
return absolutePath;
}
return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`; return `${servedOn.protocol ?? 'https:'}//${joinPaths(servedOn.host, absolutePath)}`;
}, },
toPathForPage({ pages, page, anchor }) { toPathForPage({ pages, page, anchor }) {
return linker.toPathInSpace(getPagePath(pages, page)) + (anchor ? '#' + anchor : ''); return linker.toPathInSpace(getPagePath(pages, page)) + (anchor ? `#${anchor}` : '');
},
toLinkForContent(rawURL: string): string {
const url = new URL(rawURL);
// If the link points to a content in the same site, we return an absolute path
// instead of a full URL; it makes it possible to use router navigation
if (url.hostname === servedOn.host && url.pathname.startsWith(servedOn.siteBasePath)) {
return url.pathname;
}
return rawURL;
}, },
}; };
return linker; return linker;
} }
/**
* Append a prefix to a linker.
*/
export function appendPrefixToLinker(
linker: GitBookSpaceLinker,
prefix: string,
): GitBookSpaceLinker {
const linkerWithPrefix: GitBookSpaceLinker = {
toPathInSpace(relativePath: string): string {
return linker.toPathInSpace(joinPaths(prefix, relativePath));
},
toAbsoluteURL(absolutePath: string): string {
return linker.toAbsoluteURL(joinPaths(prefix, absolutePath));
},
toPathForPage({ pages, page, anchor }) {
return (
linkerWithPrefix.toPathInSpace(getPagePath(pages, page)) +
(anchor ? '#' + anchor : '')
);
},
};
return linkerWithPrefix;
}
function joinPaths(prefix: string, path: string): string { function joinPaths(prefix: string, path: string): string {
const prefixPath = prefix.endsWith('/') ? prefix : prefix + '/'; const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`;
const suffixPath = path.startsWith('/') ? path.slice(1) : path; const suffixPath = path.startsWith('/') ? path.slice(1) : path;
return prefixPath + suffixPath; return prefixPath + suffixPath;
} }
+80
View File
@@ -1,14 +1,48 @@
import { CustomizationThemeMode } from '@gitbook/api'; import { CustomizationThemeMode } from '@gitbook/api';
import { headers } from 'next/headers'; import { headers } from 'next/headers';
import type { SiteURLData } from './context';
export enum MiddlewareHeaders { export enum MiddlewareHeaders {
/**
* Type of the route (static or dynamic)
*/
RouteType = 'x-gitbook-route-type',
/**
* The URL of the site (without the pathname)
*/
SiteURL = 'x-gitbook-site-url',
/**
* The data associated with the URL.
*/
SiteURLData = 'x-gitbook-site-url-data',
/**
* The mode of the URL (url or url-host)
*/
URLMode = 'x-gitbook-url-mode', URLMode = 'x-gitbook-url-mode',
/**
* The theme of the page (light or dark)
*/
Theme = 'x-gitbook-theme', Theme = 'x-gitbook-theme',
/**
* The customization override to apply.
*/
Customization = 'x-gitbook-customization', Customization = 'x-gitbook-customization',
/**
* The API token used to fetch the content.
* This should only be passed for non-site dynamic routes.
*/
APIToken = 'x-gitbook-api-token',
} }
/** /**
* Get the URL mode from the middleware headers. * Get the URL mode from the middleware headers.
* This function should only be called in a server action or a dynamic route.
*/ */
export async function getURLModeFromMiddleware(): Promise<'url' | 'url-host'> { export async function getURLModeFromMiddleware(): Promise<'url' | 'url-host'> {
const headersList = await headers(); const headersList = await headers();
@@ -20,8 +54,40 @@ export async function getURLModeFromMiddleware(): Promise<'url' | 'url-host'> {
return mode as 'url' | 'url-host'; return mode as 'url' | 'url-host';
} }
/**
* Get the site URL data from the middleware headers.
* This function should only be called in a server action or a dynamic route.
*/
export async function getSiteURLDataFromMiddleware(): Promise<SiteURLData> {
const headersList = await headers();
const siteURLData = headersList.get(MiddlewareHeaders.SiteURLData);
if (!siteURLData) {
throw new Error(
'Site URL data is not set by the middleware. This should only be called in a server action or a dynamic route.'
);
}
return JSON.parse(siteURLData);
}
/**
* Get the URL from the middleware headers.
* This function should only be called in a server action or a dynamic route.
*/
export async function getSiteURLFromMiddleware(): Promise<string> {
const headersList = await headers();
const siteURL = headersList.get(MiddlewareHeaders.SiteURL);
if (!siteURL) {
throw new Error('URL mode is not set by the middleware');
}
return siteURL;
}
/** /**
* For preview, the theme can be set via query string (?theme=light). * For preview, the theme can be set via query string (?theme=light).
* This function should only be called in a dynamic route.
*/ */
export async function getThemeFromMiddleware() { export async function getThemeFromMiddleware() {
const headersList = await headers(); const headersList = await headers();
@@ -34,3 +100,17 @@ export async function getThemeFromMiddleware() {
? CustomizationThemeMode.Light ? CustomizationThemeMode.Light
: CustomizationThemeMode.Dark; : CustomizationThemeMode.Dark;
} }
/**
* Get the API token from the middleware headers.
* This function should only be called in a dynamic route.
*/
export async function getAPITokenFromMiddleware(): Promise<string> {
const headersList = await headers();
const apiToken = headersList.get(MiddlewareHeaders.APIToken);
if (!apiToken) {
throw new Error('API token is not set by the middleware');
}
return apiToken;
}
+41
View File
@@ -0,0 +1,41 @@
import crypto from 'node:crypto';
import { NextResponse } from 'next/server';
import { GITBOOK_SECRET } from './env';
/**
* Verify the signature of the request and call the function with the body.
*/
export async function withVerifySignature<T>(
request: Request,
fn: (body: T) => Promise<NextResponse>
) {
try {
const rawBody = await request.text();
const body = JSON.parse(rawBody) as T;
if (GITBOOK_SECRET) {
// Retrieve the signature header from the request
const incomingSignature = request.headers.get('x-gitbook-signature');
if (!incomingSignature) {
return NextResponse.json({ error: 'Missing signature header' }, { status: 400 });
}
const computedSignature = crypto
.createHmac('sha256', GITBOOK_SECRET)
.update(rawBody)
.digest('hex');
// Compare incoming signature to computed signature
if (incomingSignature !== computedSignature) {
return NextResponse.json({ error: 'Invalid signature' }, { status: 401 });
}
}
return await fn(body);
} catch (_error) {
return NextResponse.json(
{ error: 'Invalid request or unable to parse JSON' },
{ status: 400 }
);
}
}
@@ -0,0 +1,31 @@
import { type GitBookBaseContext, fetchSiteContextByURLLookup, getBaseContext } from './context';
import {
getSiteURLDataFromMiddleware,
getSiteURLFromMiddleware,
getURLModeFromMiddleware,
} from './middleware';
/**
* Get the base context for a server action.
* This function should only be called in a server action.
*/
export async function getServerActionBaseContext() {
const siteURL = await getSiteURLFromMiddleware();
const siteURLData = await getSiteURLDataFromMiddleware();
const urlMode = await getURLModeFromMiddleware();
return getBaseContext({
siteURL,
siteURLData,
urlMode,
});
}
/**
* Fetch the context for a site in a server action.
* The server action is always dynamic and the request is passed through the middleware.
*/
export async function fetchServerActionSiteContext(baseContext: GitBookBaseContext) {
const siteURLData = await getSiteURLDataFromMiddleware();
return fetchSiteContextByURLLookup(baseContext, siteURLData);
}
+410 -52
View File
@@ -1,111 +1,469 @@
import { NextResponse } from 'next/server'; import { CustomizationThemeMode } from '@gitbook/api';
import type { NextRequest } from 'next/server'; import type { NextRequest } from 'next/server';
import { GitBookAPIError } from '@gitbook/api'; import { NextResponse } from 'next/server';
import rison from 'rison';
import { removeTrailingSlash } from '@/lib/paths'; import { getContentSecurityPolicy } from '@/lib/csp';
import { validateSerializedCustomization } from '@/lib/customization';
import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
import {
type ResponseCookies,
getPathScopedCookieName,
getResponseCookiesForVisitorAuth,
getVisitorToken,
normalizeVisitorAuthURL,
} from '@/lib/visitor-token';
import { serveResizedImage } from '@/routes/image';
import {
DataFetcherError,
getPublishedContentByURL,
normalizeURL,
throwIfDataError,
} from '@v2/lib/data';
import { isGitBookAssetsHostURL, isGitBookHostURL } from '@v2/lib/env';
import { getImageResizingContextId } from '@v2/lib/images';
import { MiddlewareHeaders } from '@v2/lib/middleware'; import { MiddlewareHeaders } from '@v2/lib/middleware';
import { getPublishedContentByURL } from '@v2/lib/data'; import type { SiteURLData } from './lib/context';
export const config = { export const config = {
matcher: ['/((?!_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)'], matcher: [
'/((?!_next/static|_next/image|~gitbook/static|~gitbook/revalidate|~gitbook/monitoring|~scalar/proxy).*)',
],
}; };
type URLWithMode = { url: URL; mode: 'url' | 'url-host' }; type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
const extracted = extractURL(request); try {
if (extracted) { const requestURL = new URL(request.url);
return serveSiteByURL(request, extracted);
// Redirect to normalize the URL
const normalized = normalizeURL(requestURL);
if (normalized.toString() !== requestURL.toString()) {
return NextResponse.redirect(normalized.toString());
}
for (const handler of [serveSiteRoutes, serveSpacePDFRoutes]) {
const result = await handler(requestURL, request);
if (result) {
return result;
}
} }
return NextResponse.next(); return NextResponse.next();
} catch (error) {
return serveErrorResponse(error as Error);
}
} }
/** /**
* Serve site by URL. * Handle request that are targetting the site routes group.
*/ */
async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) { async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
const dynamicHeaders = getDynamicHeaders(request); const match = getSiteURLFromRequest(request);
const { url, mode } = urlWithMode; if (!match) {
return null;
}
const result = await getPublishedContentByURL({ const { url: siteRequestURL, mode } = match;
url: url.toString(), const imagesContextId = getImageResizingContextId(siteRequestURL);
visitorAuthToken: null,
redirectOnError: false, /**
* Serve image resizing requests (all requests containing `/~gitbook/image`).
* All URLs containing `/~gitbook/image` are rewritten to `/~gitbook/image`
* and serve from a single route handler.
*
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
*/
if (siteRequestURL.pathname.endsWith('/~gitbook/image')) {
return await serveResizedImage(request, {
imagesContextId: imagesContextId,
});
}
//
// Detect and extract the visitor authentication token from the request
//
// @ts-ignore - request typing
const visitorToken = getVisitorToken({
cookies: request.cookies.getAll(),
url: siteRequestURL,
}); });
if (result.error) { const withAPIToken = async (apiToken: string | null) => {
if (result.error instanceof GitBookAPIError) { const siteURLData = await throwIfDataError(
return NextResponse.json( getPublishedContentByURL({
{ url: siteRequestURL.toString(),
error: result.error.message, visitorAuthToken: visitorToken?.token ?? null,
}, // When the visitor auth token is pulled from the cookie, set redirectOnError when calling getPublishedContentByUrl to allow
{ status: result.error.code }, // redirecting when the token is invalid as we could be dealing with stale token stored in the cookie.
// For example when the VA backend signature has changed but the token stored in the cookie is not yet expired.
redirectOnError: visitorToken?.source === 'visitor-auth-cookie',
// Use the API token passed in the request, if any
// as it could be used for .preview hostnames
apiToken,
})
);
const cookies: ResponseCookies = [];
//
// Handle redirects
//
if ('redirect' in siteURLData) {
// biome-ignore lint/suspicious/noConsole: we want to log the redirect
console.log('redirect', siteURLData.redirect);
if (siteURLData.target === 'content') {
let contentRedirect = new URL(siteURLData.redirect, request.url);
// For content redirects, we redirect using the /url/:url format
// during development and testing in 'url' mode.
if (mode === 'url') {
const urlObject = new URL(siteURLData.redirect);
contentRedirect = new URL(
`/url/${urlObject.host}${urlObject.pathname}${urlObject.search}`,
request.url
); );
} else {
throw result.error;
}
} }
const { data } = result; // Keep the same search params as the original request
// as it might contain a VA token
contentRedirect.search = request.nextUrl.search;
if ('redirect' in data) { return NextResponse.redirect(contentRedirect);
return NextResponse.redirect(data.redirect);
} }
return NextResponse.redirect(siteURLData.redirect);
}
cookies.push(...getResponseCookiesForVisitorAuth(siteURLData.siteBasePath, visitorToken));
// We use the host/origin from the canonical URL to ensure the links are
// correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/...
const siteCanonicalURL = new URL(siteURLData.canonicalUrl);
//
// Make sure the URL is clean of any va token after a successful lookup
// The token is stored in a cookie that is set on the redirect response
//
const incomingURL = mode === 'url' ? requestURL : siteCanonicalURL;
const requestURLWithoutToken = normalizeVisitorAuthURL(incomingURL);
if (
requestURLWithoutToken !== incomingURL &&
requestURLWithoutToken.toString() !== incomingURL.toString()
) {
return writeResponseCookies(
NextResponse.redirect(requestURLWithoutToken.toString()),
cookies
);
}
//
// Render and serve the content
//
// The route is static, except when using dynamic parameters from query params
// (customization override, theme, etc)
let routeType: 'dynamic' | 'static' = 'static';
const requestHeaders = new Headers(request.headers); const requestHeaders = new Headers(request.headers);
requestHeaders.set(MiddlewareHeaders.RouteType, routeType);
requestHeaders.set(MiddlewareHeaders.URLMode, mode); requestHeaders.set(MiddlewareHeaders.URLMode, mode);
if (dynamicHeaders) { requestHeaders.set(
for (const [key, value] of Object.entries(dynamicHeaders)) { MiddlewareHeaders.SiteURL,
requestHeaders.set(key, value); `${siteCanonicalURL.origin}${siteURLData.basePath}`
);
requestHeaders.set(MiddlewareHeaders.SiteURLData, JSON.stringify(siteURLData));
// Preview of customization/theme
const customization = siteRequestURL.searchParams.get('customization');
if (customization && validateSerializedCustomization(customization)) {
routeType = 'dynamic';
requestHeaders.set(MiddlewareHeaders.Customization, customization);
} }
const theme = siteRequestURL.searchParams.get('theme');
if (theme === CustomizationThemeMode.Dark || theme === CustomizationThemeMode.Light) {
routeType = 'dynamic';
requestHeaders.set(MiddlewareHeaders.Theme, theme);
} }
// We support forcing dynamic routes by setting a `gitbook-dynamic-route` cookie
// This is useful for testing dynamic routes.
if (request.cookies.has('gitbook-dynamic-route')) {
routeType = 'dynamic';
}
// Pass a x-forwarded-host and origin that are equal to ensure Next doesn't block server actions when proxied
requestHeaders.set('x-forwarded-host', request.nextUrl.host);
requestHeaders.set('origin', request.nextUrl.origin);
const siteURLWithoutProtocol = `${siteCanonicalURL.host}${siteURLData.basePath}`;
const { pathname, routeType: routeTypeFromPathname } = encodePathInSiteContent(
siteURLData.pathname
);
routeType = routeTypeFromPathname ?? routeType;
// We pick only stable data from the siteURL data to prevent re-rendering of
// the root layout when changing pages..
const stableSiteURLData: SiteURLData = {
site: siteURLData.site,
siteSection: siteURLData.siteSection,
siteSpace: siteURLData.siteSpace,
siteBasePath: siteURLData.siteBasePath,
basePath: siteURLData.basePath,
space: siteURLData.space,
organization: siteURLData.organization,
changeRequest: siteURLData.changeRequest,
revision: siteURLData.revision,
shareKey: siteURLData.shareKey,
apiToken: siteURLData.apiToken,
imagesContextId: imagesContextId,
};
const route = [ const route = [
'sites', 'sites',
dynamicHeaders ? 'dynamic' : 'static', routeType,
mode, mode,
encodeURIComponent(url.host + data.basePath), encodeURIComponent(siteURLWithoutProtocol),
encodeURIComponent(removeTrailingSlash(data.pathname) || '/'), encodeURIComponent(
rison.encode(
// rison can't encode undefined values
Object.fromEntries(
Object.entries(stableSiteURLData).filter(
([_, v]) => typeof v !== 'undefined'
)
)
)
),
pathname,
].join('/'); ].join('/');
return NextResponse.rewrite(new URL('/' + route, request.url), { console.log(`rewriting ${request.nextUrl.toString()} to ${route}`);
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
const response = NextResponse.rewrite(rewrittenURL, {
request: {
headers: requestHeaders, headers: requestHeaders,
},
}); });
// Add Content Security Policy header
response.headers.set('content-security-policy', getContentSecurityPolicy());
// Basic security headers
response.headers.set('strict-transport-security', 'max-age=31536000');
response.headers.set('referrer-policy', 'no-referrer-when-downgrade');
response.headers.set('x-content-type-options', 'nosniff');
// Debug header
response.headers.set('x-gitbook-route-type', routeType);
response.headers.set('x-gitbook-route-site', siteURLWithoutProtocol);
return writeResponseCookies(response, cookies);
};
// For https://preview/<siteURL> requests,
if (siteRequestURL.hostname === 'preview') {
return serveWithQueryAPIToken(
// We scope the API token to the site ID.
`${siteRequestURL.hostname}/${requestURL.pathname.slice(1).split('/')[0]}`,
request,
withAPIToken
);
}
return withAPIToken(null);
} }
/** /**
* The URL of the GitBook content can be passed in 2 different ways: * Serve routes for PDF export for a space: /~space/:spaceId/~gitbook/pdf
* - Hostname is in the `X-GitBook-Host` header and the pathname is the path in the request URL.
* - The request URL is matching `/url/:url`
*/ */
function extractURL(request: NextRequest): URLWithMode | null { async function serveSpacePDFRoutes(requestURL: URL, request: NextRequest) {
const xGitbookHost = request.headers.get('x-gitbook-host'); const pathnameParts = requestURL.pathname.slice(1).split('/');
if (xGitbookHost) { if (pathnameParts[0] !== '~space' && pathnameParts[0] !== '~site') {
return null;
}
return serveWithQueryAPIToken(
pathnameParts.slice(0, 2).join('/'),
request,
async (apiToken) => {
// Handle the rest with the router default logic
return NextResponse.next({
headers: {
[MiddlewareHeaders.APIToken]: apiToken,
},
});
}
);
}
/**
* Serve an error response.
*/
function serveErrorResponse(error: Error) {
if (error instanceof DataFetcherError) {
return new Response(error.message, {
status: error.code,
headers: { 'content-type': 'text/plain' },
});
}
throw error;
}
/**
* Server a response with an API token obtained from the query params.
*/
async function serveWithQueryAPIToken(
scopePath: string,
request: NextRequest,
serve: (apiToken: string) => Promise<NextResponse>
) {
// We store the API token in a cookie that is scoped to the specific route
// to avoid errors when multiple previews are opened in different tabs.
const cookieName = getPathScopedCookieName('gitbook-api-token', scopePath);
// Extract a potential GitBook API token passed in the request
// If found, we redirect to the same URL but with the token in the cookie
const queryAPIToken = request.nextUrl.searchParams.get('token');
if (queryAPIToken) {
request.nextUrl.searchParams.delete('token');
return writeResponseCookies(NextResponse.redirect(request.nextUrl.toString()), [
{
name: cookieName,
value: queryAPIToken,
options: {
httpOnly: true,
sameSite: process.env.NODE_ENV === 'production' ? 'none' : undefined,
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60, // 1 hour
},
},
]);
}
const apiToken = request.cookies.get(cookieName)?.value;
if (!apiToken) {
throw new DataFetcherError('Missing API token', 400);
}
return serve(apiToken);
}
/**
* The URL of the GitBook content can be passed in 3 different ways (in order of priority):
* - The request has a `X-GitBook-URL` header:
* URL is taken from the header.
* - The request has a `X-Forwarded-Host` header:
* Host is taken from the header, pathname is taken from the request URL.
* - The request URL is matching `/url/:url`:
* URL is taken from the pathname.
*/
function getSiteURLFromRequest(request: NextRequest): URLWithMode | null {
const xGitbookUrl = request.headers.get('x-gitbook-url');
if (xGitbookUrl) {
return { return {
url: new URL(`https://${xGitbookHost}${request.nextUrl.pathname}`), url: appendQueryParams(new URL(xGitbookUrl), request.nextUrl.searchParams),
mode: 'url-host', mode: 'url-host',
}; };
} }
const isMainHost = isGitBookHostURL(request.url);
const isAssetsHost = isGitBookAssetsHostURL(request.url);
// /url/:url requests on the main host
const prefix = '/url/'; const prefix = '/url/';
if (request.nextUrl.pathname.startsWith(prefix)) { if (isMainHost && request.nextUrl.pathname.startsWith(prefix)) {
return { return {
url: new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`), url: appendQueryParams(
new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
request.nextUrl.searchParams
),
mode: 'url', mode: 'url',
}; };
} }
// Skip other requests to main hosts
if (isMainHost || isAssetsHost) {
return null;
}
const xForwardedHost = request.headers.get('x-forwarded-host');
// The x-forwarded-host is set by Vercel for all requests
// so we ignore it if the hostname is the same as the instance one.
if (xForwardedHost) {
return {
url: appendQueryParams(
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
request.nextUrl.searchParams
),
mode: 'url-host',
};
}
return null; return null;
} }
/** /**
* Evaluate if a request is dynamic or static. * Encode path in a site content.
* Special paths are not encoded and passed to be handled by the route handlers.
*/ */
function getDynamicHeaders(request: NextRequest): null | Record<string, string> { function encodePathInSiteContent(rawPathname: string): {
// TODO: pathname: string;
// - check token in query string routeType?: 'static' | 'dynamic';
// - check token in cookies } {
// - check special headers or query string const pathname = removeLeadingSlash(removeTrailingSlash(rawPathname));
return null;
if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) {
return { pathname };
}
// If the pathname is a markdown file, we rewrite it to ~gitbook/markdown/:pathname
if (pathname.match(/\.md$/)) {
const pagePathWithoutMD = pathname.slice(0, -3);
return {
pathname: `~gitbook/markdown/${encodeURIComponent(pagePathWithoutMD)}`,
// The markdown content is always static and doesn't depend on the dynamic parameter (customization, theme, etc)
routeType: 'static',
};
}
switch (pathname) {
case '~gitbook/icon':
return { pathname };
case 'llms.txt':
case 'sitemap.xml':
case 'sitemap-pages.xml':
case 'robots.txt':
// LLMs.txt, sitemap, sitemap-pages and robots.txt are always static
// as they only depend on the site structure / pages.
return { pathname, routeType: 'static' };
case '~gitbook/pdf':
// PDF routes are always dynamic as they depend on the search params.
return { pathname, routeType: 'dynamic' };
default:
return { pathname: encodeURIComponent(pathname || '/') };
}
}
/**
* Append all the query params from a URL to another URL.
*/
function appendQueryParams(url: URL, from: URLSearchParams) {
for (const [key, value] of from.entries()) {
url.searchParams.set(key, value);
}
return url;
}
/**
* Write the cookies to a response.
*/
function writeResponseCookies<R extends NextResponse>(response: R, cookies: ResponseCookies): R {
cookies.forEach((cookie) => {
response.cookies.set(cookie.name, cookie.value, cookie.options);
});
return response;
} }
+11
View File
@@ -0,0 +1,11 @@
{
"extends": ["//"],
"tasks": {
"generate": {
"outputs": ["public"]
},
"typecheck": {
"dependsOn": ["^typecheck"]
}
}
}
+48 -3
View File
@@ -1,5 +1,50 @@
main = ".open-next/worker.js" main = ".open-next/worker.js"
name = "gitbook-v2" name = "gitbook-open-v2"
compatibility_date = "2024-09-23" compatibility_date = "2025-03-11"
compatibility_flags = ["nodejs_compat"] compatibility_flags = ["nodejs_compat", "allow_importable_env"]
assets = { directory = ".open-next/assets", binding = "ASSETS" } assets = { directory = ".open-next/assets", binding = "ASSETS" }
observability = { enabled = true }
[env.preview]
kv_namespaces = [
{ binding = "NEXT_CACHE_WORKERS_KV", id = "b7dd9cf58bf2458f84812a2d83b3760c" } # gitbook-open-v2-cache-preview
]
d1_databases = [
{ binding = "NEXT_CACHE_D1", database_id = "f59ddb40-ad72-4312-9395-0ac6a129af8e", database_name = "gitbook-open-v2-tags-preview" }
]
services = [
{ binding = "NEXT_CACHE_REVALIDATION_WORKER", service = "gitbook-open-v2-preview" },
{ binding = "GITBOOK_API", service = "gitbook-x-prod-api-cache" }
]
[env.staging]
routes = [
{ pattern = "open-2c.gitbook-staging.com/*", zone_name = "gitbook-staging.com" },
{ pattern = "static-2c.gitbook-staging.com/*", zone_name = "gitbook-staging.com" }
]
kv_namespaces = [
{ binding = "NEXT_CACHE_WORKERS_KV", id = "a446e25f12b741afb185f1e5b4474f0a" } # gitbook-open-v2-cache-staging
]
d1_databases = [
{ binding = "NEXT_CACHE_D1", database_id = "9df62e39-1f35-4066-83aa-e9b8ed3ac8d5", database_name = "gitbook-open-v2-tags-staging" }
]
services = [
{ binding = "NEXT_CACHE_REVALIDATION_WORKER", service = "gitbook-open-v2-staging" },
{ binding = "GITBOOK_API", service = "gitbook-x-staging-api-cache" }
]
[env.production]
routes = [
{ pattern = "open-2c.gitbook.com/*", zone_name = "gitbook.com" },
{ pattern = "static-2c.gitbook.com/*", zone_name = "gitbook.com" }
]
kv_namespaces = [
{ binding = "NEXT_CACHE_WORKERS_KV", id = "72379746280d4e79acf24440eea950dc" } # gitbook-open-v2-cache-production
]
d1_databases = [
{ binding = "NEXT_CACHE_D1", database_id = "a6f16fce-5f45-43a9-89a4-7b83ddf25b77", database_name = "gitbook-open-v2-tags-production" }
]
services = [
{ binding = "NEXT_CACHE_REVALIDATION_WORKER", service = "gitbook-open-v2-production" },
{ binding = "GITBOOK_API", service = "gitbook-x-prod-api-cache" }
]
-3
View File
@@ -23,8 +23,5 @@
## Caching ## Caching
# GITBOOK_OUTPUT_CACHE=true # GITBOOK_OUTPUT_CACHE=true
### Sentry ###
# SENTRY_DSN=xxx
### Silent logs ### Silent logs
# SILENT=true # SILENT=true
-31
View File
@@ -1,31 +0,0 @@
{
"extends": "next/core-web-vitals",
"plugins": ["import"],
"rules": {
"import/order": [
"error",
{
"groups": ["builtin", "external", ["internal", "parent", "sibling", "index"]],
"newlines-between": "always",
"distinctGroup": true,
"pathGroups": [
{
"pattern": "@/**",
"group": "external",
"position": "after"
},
{
"pattern": "@gitbook/**",
"group": "external",
"position": "after"
}
],
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
],
"@next/next/no-img-element": ["off"]
}
}
-3
View File
@@ -23,9 +23,6 @@ next-env.d.ts
# visual tests # visual tests
screenshots/ screenshots/
# Sentry Config File
.sentryclirc
/test-results/ /test-results/
/playwright-report/ /playwright-report/
/blob-report/ /blob-report/

Some files were not shown because too many files have changed in this diff Show More