Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 26 additions & 2 deletions server/middleware/canonical-redirects.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,37 @@ const pages = [
const cacheControl = 's-maxage=3600, stale-while-revalidate=36000'

export default defineEventHandler(async event => {
const [path = '/', query] = event.path.split('?')

if (query) {
const params = new URLSearchParams(query)

switch (params.get('activeTab')) {
case 'versions': {
// /package/name?activeTab=versions → /package/name/versions
// /package/@scope/name?activeTab=versions → /package/@scope/name/versions

const pkgPathMatch = path.match(/^\/package\/((?:@[^/]+\/)?[^/]+)$/)
if (pkgPathMatch) {
params.delete('activeTab')
const remaining = params.toString()
setHeader(event, 'cache-control', cacheControl)
return sendRedirect(
event,
`/package/${pkgPathMatch[1]}/versions` + (remaining ? '?' + remaining : ''),
301,
)
}
break
}
}
}

const routeRules = getRouteRules(event)
if (Object.keys(routeRules).length > 1) {
return
}

const [path = '/', query] = event.path.split('?')

// username
if (path.startsWith('/~') || path.startsWith('/_')) {
return
Expand Down
19 changes: 19 additions & 0 deletions test/e2e/url-compatibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ test.describe('npmjs.com URL Compatibility', () => {
})
})

test.describe('npmjs.com activeTab=versions Compatibility', () => {
test('/package/vue?activeTab=versions → /package/vue/versions', async ({ page, goto }) => {
await goto('/package/vue?activeTab=versions', { waitUntil: 'domcontentloaded' })

await expect(page).toHaveURL(/\/package\/vue\/versions$/)
await expect(page.locator('h1')).toContainText('Version History')
})

test('/package/@nuxt/kit?activeTab=versions → /package/@nuxt/kit/versions', async ({
page,
goto,
}) => {
await goto('/package/@nuxt/kit?activeTab=versions', { waitUntil: 'domcontentloaded' })

await expect(page).toHaveURL(/\/package\/@nuxt\/kit\/versions$/)
await expect(page.locator('h1')).toContainText('Version History')
})
})

test.describe('Edge Cases', () => {
test('package name with dots: /package/lodash.merge', async ({ page, goto }) => {
await goto('/package/lodash.merge', { waitUntil: 'domcontentloaded' })
Expand Down
Loading