Skip to content

fix(api): support server-side sorting for newest, oldest, and Z-A pagination in link listing#308

Open
wulukewu wants to merge 2 commits into
miantiao-me:masterfrom
wulukewu:fix/kv-sort-list
Open

fix(api): support server-side sorting for newest, oldest, and Z-A pagination in link listing#308
wulukewu wants to merge 2 commits into
miantiao-me:masterfrom
wulukewu:fix/kv-sort-list

Conversation

@wulukewu

Copy link
Copy Markdown

Problem

KV.list() returns pages in alphabetical key order, but the dashboard's sorting for "newest", "oldest", and "Z-A" (za) was done client-side on already-loaded data only. This means:

  1. Wrong page priority — for newest, oldest, and Z-A sorting, the first page always contains alphabetically early slugs (A-Z), not the actual sorted entries. Users must scroll through all pages before seeing the correct top links.
  2. Cursor skips entriesKV.list() cursor represents an alphabetical position. Any link created/sorted that falls before the cursor is never returned in subsequent pages during pagination.

Solution

Add a sort query parameter to GET /api/link/list supporting slug_asc (default), slug_desc (Z-A), createdAt_desc (newest), and createdAt_asc (oldest).

  • For slug_asc (A-Z), the server continues to use native KV.list() for optimal performance.
  • For slug_desc, createdAt_desc, and createdAt_asc, the server:
    1. Lists all KV keys (loops through KV.list() pages of 1000)
    2. Fetches all links
    3. Sorts by the requested criteria
    4. Returns the requested page with a custom cursor:
      • {createdAt}::{slug} for creation date sorting
      • {slug} for Z-A (slug_desc) sorting

The client-side sort for newest/oldest is removed, and all custom sorting relies on the server-side pagination.

Changes

  • server/utils/link-store.ts — add listLinksSorted() supporting custom sorting criteria
  • server/api/link/list.get.ts — add sort query param and route to listLinksSorted() or listLinks()
  • layers/dashboard/app/components/dashboard/links/Index.vue — pass sort, reset list on sort change

wulukewu added 2 commits June 29, 2026 11:48
Add sort query param to /api/link/list for createdAt_desc/asc sorting.
List all KV keys on the server, sort by createdAt, and return paginated
results so the first page always contains the newest links.

Previously KV.list() returned pages in alphabetical key order, and
client-side sorting only applied to already-loaded links. This caused
two bugs:

1. Newest links (alphabetically late slugs) were invisible until all
   pages loaded via infinite scroll.
2. KV.list() cursor pagination skipped links created before the cursor
   position, causing them to never appear.

Closes #KV-cursor-newlink-bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant