From 48af68b05a32f5da596f79172a610418e6ebc479 Mon Sep 17 00:00:00 2001 From: carl chen Date: Sat, 12 Sep 2026 19:12:48 +0800 Subject: [PATCH] fix: preserve trailing slashes in mirror redirects --- src/mirror-redirect/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/mirror-redirect/index.ts b/src/mirror-redirect/index.ts index 8ab9199..289cfb1 100644 --- a/src/mirror-redirect/index.ts +++ b/src/mirror-redirect/index.ts @@ -221,9 +221,17 @@ function hostMatches(hostname: string, matcher: HostnameMatcher): boolean { function toCnSuffixPathname(pathname: string): string { if (pathname === '/' || pathname === '') return '/index-cn' - if (pathname.startsWith('/~demos') || pathname.endsWith('-cn')) + + // Append the suffix to the path segment, not after its trailing slash. + // Strip the slash while checking/appending `-cn`, then restore it. + const hasTrailingSlash = pathname.endsWith('/') + const normalizedPathname = hasTrailingSlash ? pathname.slice(0, -1) : pathname + + if (normalizedPathname.startsWith('/~demos') || normalizedPathname.endsWith('-cn')) return pathname - return `${pathname}-cn` + + const suffixedPathname = `${normalizedPathname}-cn` + return hasTrailingSlash ? `${suffixedPathname}/` : suffixedPathname } function readStorage(key: string): string | null {