From 91bbb1934de26073d1ec3ba4bb983c1362341af6 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Fri, 27 Sep 2024 10:39:00 +0530 Subject: [PATCH 1/6] WIP --- packages/gitbook/src/lib/api.ts | 10 +++++- packages/gitbook/src/middleware.ts | 52 ++++++++++++++++++------------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/packages/gitbook/src/lib/api.ts b/packages/gitbook/src/lib/api.ts index 6d2c08b13..766fe6542 100644 --- a/packages/gitbook/src/lib/api.ts +++ b/packages/gitbook/src/lib/api.ts @@ -127,7 +127,15 @@ export function withAPI(client: GitBookAPI, fn: () => Promise): Promise } export type PublishedContentWithCache = - | ((PublishedContentLookup | PublishedSiteContentLookup) & { + | (( + | PublishedContentLookup + | (PublishedSiteContentLookup & { + /** + * ID of the default site space variant in the URL lookup. + */ + defaultVariant: string; + }) + ) & { cacheMaxAge?: number; cacheTags?: string[]; }) diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index b3e1bc57b..1940d8575 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -666,27 +666,37 @@ async function lookupSpaceByAPI( return null; } - const changeRequest = data.changeRequest ?? lookup.changeRequest; - return { - space: data.space, - changeRequest, - revision: data.revision ?? lookup.revision, - basePath: joinPath(data.basePath, lookup.basePath ?? ''), - pathname: joinPath(data.pathname, alternative.extraPath), - apiToken: data.apiToken, - // We don't cache change requests as they often change and we want to have consistent previews - // Purging the CDN cache will not be efficient enough. - cacheMaxAge: changeRequest ? 0 : data.cacheMaxAge, - cacheTags: data.cacheTags, - ...('site' in data - ? { - site: data.site, - siteSpace: data.siteSpace, - organization: data.organization, - shareKey: data.shareKey, - } - : {}), - } as PublishedContentWithCache; + /** + * 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 resolves to a site space variant which is different from the default variant + * (in this case, we are looking at the deepest path using the variant path explicitly) + */ + if (alternative.primary || ('site' in data && data.defaultVariant !== data.siteSpace)) { + const changeRequest = data.changeRequest ?? lookup.changeRequest; + return { + space: data.space, + changeRequest, + revision: data.revision ?? lookup.revision, + basePath: joinPath(data.basePath, lookup.basePath ?? ''), + pathname: joinPath(data.pathname, alternative.extraPath), + apiToken: data.apiToken, + // We don't cache change requests as they often change and we want to have consistent previews + // Purging the CDN cache will not be efficient enough. + cacheMaxAge: changeRequest ? 0 : data.cacheMaxAge, + cacheTags: data.cacheTags, + ...('site' in data + ? { + site: data.site, + siteSpace: data.siteSpace, + organization: data.organization, + shareKey: data.shareKey, + } + : {}), + } as PublishedContentWithCache; + } + + return null; }); return ( From 52999f8a93d166562317c7a2dfaa96c78e58460f Mon Sep 17 00:00:00 2001 From: taranvohra Date: Fri, 27 Sep 2024 17:09:50 +0530 Subject: [PATCH 2/6] wip --- packages/gitbook/src/lib/api.ts | 2 +- packages/gitbook/src/lib/middleware.test.ts | 15 +++++++++++++++ packages/gitbook/src/lib/middleware.ts | 2 +- packages/gitbook/src/middleware.ts | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/gitbook/src/lib/api.ts b/packages/gitbook/src/lib/api.ts index 766fe6542..28b1cdf7d 100644 --- a/packages/gitbook/src/lib/api.ts +++ b/packages/gitbook/src/lib/api.ts @@ -133,7 +133,7 @@ export type PublishedContentWithCache = /** * ID of the default site space variant in the URL lookup. */ - defaultVariant: string; + defaultSiteSpace: string; }) ) & { cacheMaxAge?: number; diff --git a/packages/gitbook/src/lib/middleware.test.ts b/packages/gitbook/src/lib/middleware.test.ts index 7efa364f9..6723c6598 100644 --- a/packages/gitbook/src/lib/middleware.test.ts +++ b/packages/gitbook/src/lib/middleware.test.ts @@ -22,6 +22,11 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'c', url: 'https://docs.mycompany.com/a/b', + primary: false, + }, + { + extraPath: '', + url: 'https://docs.mycompany.com/a/b/c', primary: true, }, ], @@ -229,6 +234,11 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'c/d', url: 'https://docs.mycompany.com/a/b', + primary: false, + }, + { + extraPath: 'd', + url: 'https://docs.mycompany.com/a/b/c', primary: true, }, ], @@ -254,6 +264,11 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'b/c/d', url: 'https://docs.mycompany.com/a/~', + primary: false, + }, + { + extraPath: 'c/d', + url: 'https://docs.mycompany.com/a/~/b', primary: true, }, ], diff --git a/packages/gitbook/src/lib/middleware.ts b/packages/gitbook/src/lib/middleware.ts index f86281d29..c3aef41a9 100644 --- a/packages/gitbook/src/lib/middleware.ts +++ b/packages/gitbook/src/lib/middleware.ts @@ -91,7 +91,7 @@ export function getURLLookupAlternatives(input: URL) { } // Otherwise match with the first two segments of the path - for (let i = 1; i <= 2; i++) { + for (let i = 1; i <= 3; i++) { if (pathSegments.length >= i) { const shortURL = new URL(url); shortURL.pathname = pathSegments.slice(0, i).join('/'); diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 1940d8575..345d04e43 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -672,7 +672,7 @@ async function lookupSpaceByAPI( * - the resolution resolves to a site space variant which is different from the default variant * (in this case, we are looking at the deepest path using the variant path explicitly) */ - if (alternative.primary || ('site' in data && data.defaultVariant !== data.siteSpace)) { + if (alternative.primary || ('site' in data && data.defaultSiteSpace !== data.siteSpace)) { const changeRequest = data.changeRequest ?? lookup.changeRequest; return { space: data.space, From 1f9a79b47117c5b0ee2249bff6e0bc04289040a7 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Fri, 27 Sep 2024 17:46:00 +0530 Subject: [PATCH 3/6] update comment --- packages/gitbook/src/middleware.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 345d04e43..a035bd3b3 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -670,7 +670,9 @@ async function lookupSpaceByAPI( * 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 resolves to a site space variant which is different from the default variant - * (in this case, we are looking at the deepest path using the variant path explicitly) + * (in this case, we are looking at the deepest path that uses the variant path explicitly) + * + * 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.defaultSiteSpace !== data.siteSpace)) { const changeRequest = data.changeRequest ?? lookup.changeRequest; From 06a174903aa9fcc54c8f4954fc859defc1a87692 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Fri, 27 Sep 2024 18:16:02 +0530 Subject: [PATCH 4/6] todo comment --- packages/gitbook/src/lib/api.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gitbook/src/lib/api.ts b/packages/gitbook/src/lib/api.ts index 28b1cdf7d..6a3b704a3 100644 --- a/packages/gitbook/src/lib/api.ts +++ b/packages/gitbook/src/lib/api.ts @@ -130,6 +130,7 @@ export type PublishedContentWithCache = | (( | PublishedContentLookup | (PublishedSiteContentLookup & { + //TODO: Remove this once the @gitbook/api is bumped /** * ID of the default site space variant in the URL lookup. */ From 3b85aca8cfd2ececf07cf8bc46fcbed7e5caa0c1 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Fri, 27 Sep 2024 18:27:28 +0530 Subject: [PATCH 5/6] changeset --- .changeset/wicked-bees-change.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wicked-bees-change.md diff --git a/.changeset/wicked-bees-change.md b/.changeset/wicked-bees-change.md new file mode 100644 index 000000000..3f16eeee2 --- /dev/null +++ b/.changeset/wicked-bees-change.md @@ -0,0 +1,5 @@ +--- +'gitbook': minor +--- + +Changed the alternative URL resolution criteria in order to support site URLs without /v/ prefix From b005a3800f37660c0a87039eb8c2de9482adec64 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Sun, 29 Sep 2024 19:43:36 +0530 Subject: [PATCH 6/6] update with complete property --- packages/gitbook/src/lib/api.ts | 5 +++-- packages/gitbook/src/middleware.ts | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/gitbook/src/lib/api.ts b/packages/gitbook/src/lib/api.ts index 6a3b704a3..841e27ef6 100644 --- a/packages/gitbook/src/lib/api.ts +++ b/packages/gitbook/src/lib/api.ts @@ -132,9 +132,10 @@ export type PublishedContentWithCache = | (PublishedSiteContentLookup & { //TODO: Remove this once the @gitbook/api is bumped /** - * ID of the default site space variant in the URL lookup. + * Whether the resolved site URL is complete and at it's terminal point, meaning no more site path segments + * can be further expected before any page path segments. */ - defaultSiteSpace: string; + complete: boolean; }) ) & { cacheMaxAge?: number; diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index a035bd3b3..933f425a9 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -669,12 +669,11 @@ async function lookupSpaceByAPI( /** * 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 resolves to a site space variant which is different from the default variant - * (in this case, we are looking at the deepest path that uses the variant path explicitly) + * - 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.defaultSiteSpace !== data.siteSpace)) { + if (alternative.primary || ('site' in data && data.complete)) { const changeRequest = data.changeRequest ?? lookup.changeRequest; return { space: data.space,