From 1dcbf529a3d6d5ecd5749a32e93bc5867c7b1679 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Wed, 1 Oct 2025 14:17:39 +0100 Subject: [PATCH] Make initial News loading faster --- docker-compose.yml | 2 +- islands/news/Articles.tsx | 7 +++++++ lib/models/news.ts | 11 +++++++++++ routes/news.tsx | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index cb4a8cf..24e8c1c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: website: - image: ghcr.io/bewcloud/bewcloud:v2.6.0 + image: ghcr.io/bewcloud/bewcloud:v2.6.1 restart: always ports: - 127.0.0.1:8000:8000 diff --git a/islands/news/Articles.tsx b/islands/news/Articles.tsx index f667a2d..6190cc3 100644 --- a/islands/news/Articles.tsx +++ b/islands/news/Articles.tsx @@ -15,6 +15,8 @@ interface Filter { status: 'all' | 'unread'; } +let hasFetchedAllArticlesOnce = false; + export default function Articles({ initialArticles }: ArticlesProps) { const isRefreshing = useSignal(false); const articles = useSignal(initialArticles); @@ -148,6 +150,11 @@ export default function Articles({ initialArticles }: ArticlesProps) { function setNewFilter(newFilter: Partial) { filter.value = { ...filter.value, ...newFilter }; + if (newFilter.status === 'all' && !hasFetchedAllArticlesOnce) { + refreshArticles(); + hasFetchedAllArticlesOnce = true; + } + isFilterDropdownOpen.value = false; } diff --git a/lib/models/news.ts b/lib/models/news.ts index eeaf703..b780d90 100644 --- a/lib/models/news.ts +++ b/lib/models/news.ts @@ -214,6 +214,17 @@ export class ArticleModel { return articles; } + static async listUnread(userId: string) { + const articles = await db.query( + sql`SELECT * FROM "bewcloud_news_feed_articles" WHERE "user_id" = $1 AND "is_read" = FALSE ORDER BY "article_date" DESC`, + [ + userId, + ], + ); + + return articles; + } + static async listByFeedId(feedId: string) { const articles = await db.query( sql`SELECT * FROM "bewcloud_news_feed_articles" WHERE "feed_id" = $1 ORDER BY "article_date" DESC`, diff --git a/routes/news.tsx b/routes/news.tsx index 222b56e..f373396 100644 --- a/routes/news.tsx +++ b/routes/news.tsx @@ -19,7 +19,7 @@ export const handler: Handlers = { return new Response('Redirect', { status: 303, headers: { 'Location': `/dashboard` } }); } - const userArticles = await ArticleModel.list(context.state.user.id); + const userArticles = await ArticleModel.listUnread(context.state.user.id); return await context.render({ userArticles }); },