From 09de40002a5ecb44b8715014afb5161fe167487b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Tue, 20 Feb 2024 14:29:14 +0100 Subject: [PATCH] Fix share links resulting in errors because of (#177) --- src/lib/middleware.test.ts | 13 +++++++++++++ src/lib/middleware.ts | 11 ++++++----- src/middleware.ts | 7 ++++++- tests/visual-testing.ts | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/lib/middleware.test.ts b/src/lib/middleware.test.ts index 939bb972e..81dc48fd0 100644 --- a/src/lib/middleware.test.ts +++ b/src/lib/middleware.test.ts @@ -8,14 +8,17 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'a/b/c', url: 'https://docs.mycompany.com/', + primary: false, }, { extraPath: 'b/c', url: 'https://docs.mycompany.com/a', + primary: false, }, { extraPath: '', url: 'https://docs.mycompany.com/a/b/c', + primary: true, }, ]); }); @@ -27,10 +30,12 @@ describe('getURLLookupAlternatives', () => { { url: 'https://test.gitbook.io/v/variant', extraPath: 'space', + primary: false, }, { url: 'https://test.gitbook.io/v/variant/space', extraPath: '', + primary: true, }, ]); }); @@ -42,10 +47,12 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'hello', url: 'https://docs.mycompany.com/~/revisions/id', + primary: false, }, { extraPath: '', url: 'https://docs.mycompany.com/~/revisions/id/hello', + primary: true, }, ]); }); @@ -57,10 +64,12 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'hello', url: 'https://docs.mycompany.com/~/changes/id', + primary: false, }, { extraPath: '', url: 'https://docs.mycompany.com/~/changes/id/hello', + primary: true, }, ]); }); @@ -70,10 +79,12 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'hello', url: 'https://docs.mycompany.com/', + primary: false, }, { extraPath: '', url: 'https://docs.mycompany.com/hello', + primary: true, }, ]); }); @@ -83,10 +94,12 @@ describe('getURLLookupAlternatives', () => { { extraPath: 'hello', url: 'https://docs.mycompany.com/', + primary: false, }, { extraPath: '', url: 'https://docs.mycompany.com/hello', + primary: true, }, ]); }); diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index b3a2eaeda..50b5a55e7 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -4,14 +4,14 @@ */ export function getURLLookupAlternatives(input: URL) { const url = normalizeURL(input); - const alternatives: Array<{ url: string; extraPath: string }> = []; + const alternatives: Array<{ url: string; extraPath: string; primary: boolean }> = []; - const pushAlternative = (url: URL, extraPath: string) => { - const existing = alternatives.find((alt) => alt.url === url.toString()); + const pushAlternative = (adding: URL, extraPath: string) => { + const existing = alternatives.find((alt) => alt.url === adding.toString()); if (existing) { if (existing.extraPath !== extraPath) { throw new Error( - `Invalid extraPath ${extraPath} for url ${url.toString()}, already set to ${ + `Invalid extraPath ${extraPath} for url ${adding.toString()}, already set to ${ existing.extraPath }`, ); @@ -20,8 +20,9 @@ export function getURLLookupAlternatives(input: URL) { } alternatives.push({ - url: url.toString(), + url: adding.toString(), extraPath, + primary: adding.toString() === url.toString(), }); }; diff --git a/src/middleware.ts b/src/middleware.ts index b7d0ede26..580a8b544 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -480,7 +480,12 @@ async function lookupSpaceByAPI( }); if ('error' in data) { - return data; + 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 data; + } + return null; } if ('redirect' in data) { diff --git a/tests/visual-testing.ts b/tests/visual-testing.ts index 2650c312e..f3766cde3 100644 --- a/tests/visual-testing.ts +++ b/tests/visual-testing.ts @@ -219,6 +219,20 @@ const testCases: TestsCase[] = [ }, ], }, + { + name: 'Share links', + baseUrl: 'https://gitbook.gitbook.io/test-share-links/', + tests: [ + { + name: 'Valid link', + url: 'Fc6mMII9FKgnwm7qqynx/', + }, + { + name: 'Invalid link', + url: 'invalid/', + }, + ], + }, ]; console.log(`Starting visual testing with ${getTargetURL()}...`);