diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..70eb499 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-18 - Concurrent Fetching in API Routes +**Learning:** In Next.js App Router API routes, multiple independent database fetches (like getting user shares, lists, and projects) often happen sequentially when using simple `await` statements one after the other. This creates unnecessary latency. Grouping them into `await Promise.all([])` executes them concurrently. +**Action:** Always scan API routes and server components for sequential `await` calls that do not depend on each other's results, and refactor them to use `Promise.all` to reduce overall execution time. diff --git a/src/app/api/share/route.ts b/src/app/api/share/route.ts index 618a120..fbece07 100644 --- a/src/app/api/share/route.ts +++ b/src/app/api/share/route.ts @@ -22,11 +22,14 @@ export async function GET() { ); } - const shares = await listUserShares(userId); + // Fetch data concurrently to minimize latency + const [shares, lists, projects] = await Promise.all([ + listUserShares(userId), + getUserLists(userId), + getUserProjects(userId), + ]); // Enrich with target name so the UI can render without extra round-trips. - const lists = await getUserLists(userId); - const projects = await getUserProjects(userId); const listById = new Map(lists.map((l) => [l.id, l])); const projectById = new Map(projects.map((p) => [p.id, p]));