Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL alternative resolution criteria #2497

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ export function withAPI<T>(client: GitBookAPI, fn: () => Promise<T>): Promise<T>
}

export type PublishedContentWithCache =
| ((PublishedContentLookup | PublishedSiteContentLookup) & {
| ((
| PublishedContentLookup
| (PublishedSiteContentLookup & {
/**
* ID of the default site space variant in the URL lookup.
*/
defaultSiteSpace: string;
taranvohra marked this conversation as resolved.
Show resolved Hide resolved
})
) & {
cacheMaxAge?: number;
cacheTags?: string[];
})
Expand Down
15 changes: 15 additions & 0 deletions packages/gitbook/src/lib/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
Expand Down Expand Up @@ -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,
},
],
Expand All @@ -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,
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/gitbook/src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
54 changes: 33 additions & 21 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,27 +666,39 @@ 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 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;
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 (
Expand Down
Loading