Skip to content

Commit 7071ba5

Browse files
authored
fix: handle trailing slashes on catchall paths (#673)
1 parent d714591 commit 7071ba5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

app/composables/usePackageRoute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function usePackageRoute() {
1313
const route = useRoute('package')
1414

1515
const parsedRoute = computed(() => {
16-
const segments = route.params.package || []
16+
const segments = route.params.package?.filter(Boolean) || []
1717

1818
// Find the /v/ separator for version
1919
const vIndex = segments.indexOf('v')

app/pages/code/[...path].vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const parsedRoute = computed(() => {
4242
4343
const packageName = computed(() => parsedRoute.value.packageName)
4444
const version = computed(() => parsedRoute.value.version)
45-
const filePath = computed(() => parsedRoute.value.filePath)
45+
const filePathOrig = computed(() => parsedRoute.value.filePath)
46+
const filePath = computed(() => parsedRoute.value.filePath?.replace(/\/$/, ''))
4647
4748
// Fetch package data for version list
4849
const { data: pkg } = usePackage(packageName)
@@ -64,17 +65,26 @@ const { data: fileTree, status: treeStatus } = useFetch<PackageFileTreeResponse>
6465
// Determine what to show based on the current path
6566
// Note: This needs fileTree to be loaded first
6667
const currentNode = computed(() => {
67-
if (!fileTree.value?.tree || !filePath.value) return null
68+
if (!fileTree.value?.tree || !filePathOrig.value) return null
6869
69-
const parts = filePath.value.split('/')
70+
// We use original file path to correctly handle trailing slashes for file tree navigation
71+
// - /src/index.ts - correct file path
72+
// - /src/index.ts/ - incorrect file path (but formally can exist as a directory)
73+
// - /src/index and /src/index/ - correct directory paths
74+
const parts = filePathOrig.value.split('/')
7075
let current: PackageFileTree[] | undefined = fileTree.value.tree
7176
let lastFound: PackageFileTree | null = null
77+
const partsLength = parts.length
7278
73-
for (const part of parts) {
79+
for (let i = 0; i < partsLength; i++) {
80+
const part = parts[i]
81+
const isLast = i === partsLength - 1
82+
// If the previous part is a directory and the last one is empty (like /lib/) then return the previous directory
83+
if (!part && isLast && lastFound?.type === 'directory') return lastFound
7484
const found: PackageFileTree | undefined = current?.find(n => n.name === part)
7585
if (!found) return null
7686
lastFound = found
77-
if (found.type === 'file') return found
87+
if (found.type === 'file' && isLast) return found
7888
current = found.children
7989
}
8090

app/pages/docs/[...path].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const route = useRoute('docs')
1111
const router = useRouter()
1212
1313
const parsedRoute = computed(() => {
14-
const segments = route.params.path || []
14+
const segments = route.params.path?.filter(Boolean) || []
1515
const vIndex = segments.indexOf('v')
1616
1717
if (vIndex === -1 || vIndex >= segments.length - 1) {

0 commit comments

Comments
 (0)