Skip to content

Commit

Permalink
fix(cloud-function): avoid double slash where canonical url has trail…
Browse files Browse the repository at this point in the history
…ing slash (#11441)

Avoids redirecting `curriculum/index.json` to `curriculum//index.json`.
  • Loading branch information
caugner authored Jul 10, 2024
1 parent d78a80f commit 593f7b0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cloud-function/src/middlewares/redirect-non-canonicals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ export async function redirectNonCanonicals(
typeof REDIRECTS[source] == "string" &&
REDIRECTS[source] !== originalSource
) {
const target = REDIRECTS[source] + suffix + parsedUrl.search;
return redirect(res, target, {
status: 301,
cacheControlSeconds: THIRTY_DAYS,
});
const target = joinPath(REDIRECTS[source], suffix) + parsedUrl.search;
if (pathname !== target) {
return redirect(res, target, {
status: 301,
cacheControlSeconds: THIRTY_DAYS,
});
}
}
}

next();
}

function joinPath(a: string, b: string) {
if (a.endsWith("/") && b.startsWith("/")) {
return a + b.slice(1);
} else {
return a + b;
}
}

0 comments on commit 593f7b0

Please sign in to comment.