Skip to content
Merged
Changes from all 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
12 changes: 10 additions & 2 deletions src/mirror-redirect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down