From 28be5aaecad5ca60a74fadb29dcd18013eeeb5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Sun, 7 Jan 2024 23:19:05 +0100 Subject: [PATCH] Fix resolution of URL for collection URLs (#88) --- src/middleware.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 7a366b538..f27607c32 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -441,25 +441,27 @@ function computeLookupAlternatives(url: URL) { const pathSegments = url.pathname.slice(1).split('/'); - // Match with only the first segment of the path + // URL looks like a collection url (with /v/ in the path) + // We only start matching after the /v/ segment and we ignore everything before it + // to avoid potentially matching as a page not found under the default space in the collection + if (pathSegments.includes('v')) { + const collectionURL = new URL(url); + const vIndex = pathSegments.indexOf('v'); + collectionURL.pathname = pathSegments.slice(0, vIndex + 2).join('/'); + + pushAlternative(collectionURL, pathSegments.slice(vIndex + 2).join('/')); + } + + // Otherwise match with only the first segment of the path // as it could potentially a space in an organization or collection domain // or a space using a share link secret - if (pathSegments.length > 0) { + else if (pathSegments.length > 0) { const shortURL = new URL(url); shortURL.pathname = pathSegments[0]; pushAlternative(shortURL, pathSegments.slice(1).join('/')); } - // URL looks like a collection url (with /v/ in the path) - if (pathSegments.includes('v')) { - const collectionURL = new URL(url); - const vIndex = pathSegments.indexOf('v'); - collectionURL.pathname = pathSegments.slice(0, vIndex + 1).join('/'); - - pushAlternative(collectionURL, pathSegments.slice(vIndex + 1).join('/')); - } - // Always try with the full URL if (!alternatives.some((alt) => alt.url === url.toString())) { pushAlternative(url, '');