fix(fetchVersions): Sort package versions (#10695)

* fix(fetchVersions): sort package versions

* fix(middleware): fix stable redirect
This commit is contained in:
Jiralite
2025-01-12 18:09:46 +00:00
committed by GitHub
parent 01e64b4e9a
commit 91f59cf183
3 changed files with 15 additions and 19 deletions

View File

@@ -9,9 +9,12 @@ async function fetchLatestVersion(packageName: string): Promise<string> {
}
try {
const { rows } = await sql`select version from documentation where name = ${packageName} order by version desc`;
const { rows } = await sql<{ version: string }>`with ordered_versions as (
select version from documentation where name = ${packageName} and version != 'main' order by string_to_array(version, '.')::int[] desc
)
select version from ordered_versions limit 1`;
return rows.map((row) => row.version).at(1) ?? 'main';
return rows[0]?.version ?? 'main';
} catch {
return '';
}

View File

@@ -1,16 +0,0 @@
import { sql } from '@vercel/postgres';
import { ENV } from './env';
export async function fetchLatestVersion(packageName: string): Promise<string> {
if (ENV.IS_LOCAL_DEV) {
return 'main';
}
try {
const { rows } = await sql`select version from documentation where name = ${packageName} order by version desc`;
return rows.map((row) => row.version).at(1) ?? 'main';
} catch {
return '';
}
}

View File

@@ -9,7 +9,16 @@ export async function fetchVersions(packageName: string) {
try {
const { rows } = await sql<{
version: string;
}>`select version from documentation where name = ${packageName} order by version desc`;
}>`select version from documentation where name = ${packageName} order by
case
when version = 'main' then 0
else 1
end,
case
when version = 'main' then null
else string_to_array(version, '.')::int[]
end desc;
`;
return rows;
} catch {