fix(homepage): dedupe feed bands and surface featured section
The bridge built hero, trending, featured, latest, and category rows from overlapping slices of the same post list, so the same thumbnails appeared many times. Reserve post ids top-to-bottom, over-fetch the main feed and category queries, and de-dupe the hero when stickies overlap the feed. Render the featured slice as its own banded section, localize the sports trending strip, and use distinct heading ids when two grids share the page. Co-Authored-By: Paperclip <[email protected]> Made-with: Cursor
This commit is contained in:
1 parent
921f52f259
commit
4ef03e52ea
6 files changed
+117
-22
No files matched your search
@@ -12,6 +12,57 @@ const TRENDING_COUNT = 5;
|
||||
const FEATURED_COUNT = 4;
|
||||
const LATEST_COUNT = 12;
|
||||
const BY_CATEGORY_COUNT = 4;
|
||||
/** Extra posts so trending / featured / latest stay full after de-duplicating from hero. */
|
||||
const MAIN_FEED_PER_PAGE = HERO_COUNT + TRENDING_COUNT + FEATURED_COUNT + LATEST_COUNT + 24;
|
||||
/** Fetch more per category than we show so we can skip posts already used above the fold. */
|
||||
const BY_CATEGORY_FETCH = Math.max(BY_CATEGORY_COUNT * 10, 32);
|
||||
|
||||
function add_to_used(articles: ArticleListItem[], used: Set<number>): void {
|
||||
for (const a of articles) {
|
||||
used.add(a.id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks `pool` in order and takes up to `limit` items whose ids are not in `used`,
|
||||
* then registers those ids in `used`. Applies `patch` to each picked item.
|
||||
*/
|
||||
function take_unique(
|
||||
pool: ArticleListItem[],
|
||||
used: Set<number>,
|
||||
limit: number,
|
||||
patch: Partial<Pick<ArticleListItem, 'is_trending' | 'is_featured'>>,
|
||||
): ArticleListItem[] {
|
||||
const out: ArticleListItem[] = [];
|
||||
for (const item of pool) {
|
||||
if (used.has(item.id)) {
|
||||
continue;
|
||||
}
|
||||
used.add(item.id);
|
||||
out.push({ ...item, ...patch });
|
||||
if (out.length >= limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** First `limit` items with distinct ids (sticky + latest often overlap). */
|
||||
function take_unique_ids(pool: ArticleListItem[], limit: number): ArticleListItem[] {
|
||||
const seen = new Set<number>();
|
||||
const out: ArticleListItem[] = [];
|
||||
for (const item of pool) {
|
||||
if (seen.has(item.id)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(item.id);
|
||||
out.push(item);
|
||||
if (out.length >= limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const empty_homepage = (): Omit<HomepageData, 'fpa_agenda'> => ({
|
||||
hero: [],
|
||||
@@ -34,27 +85,26 @@ export async function homepage_routes(app: FastifyInstance): Promise<void> {
|
||||
// Fetch in parallel where possible
|
||||
const [sticky_posts, latest_result, categories_list] = await Promise.all([
|
||||
client.get_sticky_posts(HERO_COUNT).catch((): ArticleListItem[] => []),
|
||||
client.get_posts({ per_page: LATEST_COUNT + TRENDING_COUNT }),
|
||||
client.get_posts({ per_page: MAIN_FEED_PER_PAGE }),
|
||||
client.get_categories().catch(() => []),
|
||||
]);
|
||||
|
||||
const all_latest = latest_result.items;
|
||||
|
||||
// Hero = sticky posts (or first N from latest if none)
|
||||
const hero = sticky_posts.length >= HERO_COUNT
|
||||
? sticky_posts.slice(0, HERO_COUNT)
|
||||
: [...sticky_posts, ...all_latest.slice(0, HERO_COUNT - sticky_posts.length)];
|
||||
// Hero = sticky first, then backfill from latest; de-dupe ids (stickies usually also in the feed)
|
||||
const hero_pool =
|
||||
sticky_posts.length >= HERO_COUNT
|
||||
? sticky_posts
|
||||
: [...sticky_posts, ...all_latest];
|
||||
const hero = take_unique_ids(hero_pool, HERO_COUNT);
|
||||
|
||||
// Trending = most recent posts marked is_trending (first N for now; can swap with view-count logic later)
|
||||
const trending: ArticleListItem[] = all_latest
|
||||
.slice(0, TRENDING_COUNT)
|
||||
.map(p => ({ ...p, is_trending: true }));
|
||||
const used = new Set<number>();
|
||||
add_to_used(hero, used);
|
||||
|
||||
// Featured = first FEATURED_COUNT from latest
|
||||
const featured: ArticleListItem[] = all_latest.slice(0, FEATURED_COUNT).map(p => ({ ...p, is_featured: true }));
|
||||
|
||||
// Latest = all fetched
|
||||
const latest = all_latest.slice(0, LATEST_COUNT);
|
||||
// Each band below uses only posts not already shown above (avoids repeated images)
|
||||
const trending = take_unique(all_latest, used, TRENDING_COUNT, { is_trending: true });
|
||||
const featured = take_unique(all_latest, used, FEATURED_COUNT, { is_featured: true });
|
||||
const latest = take_unique(all_latest, used, LATEST_COUNT, {});
|
||||
|
||||
// Per-category sections using domain config categories
|
||||
const category_slugs = config.categories.map(c => c.slug);
|
||||
@@ -64,7 +114,8 @@ export async function homepage_routes(app: FastifyInstance): Promise<void> {
|
||||
category_slugs.map(async (slug) => {
|
||||
const cat = categories_list.find(c => c.slug === slug);
|
||||
if (!cat) return { slug, items: [] as ArticleListItem[] };
|
||||
const items = await client.get_posts_by_category_id(cat.id, BY_CATEGORY_COUNT);
|
||||
const raw = await client.get_posts_by_category_id(cat.id, BY_CATEGORY_FETCH);
|
||||
const items = take_unique(raw, used, BY_CATEGORY_COUNT, {});
|
||||
return { slug, items };
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -9,17 +9,32 @@ interface Props {
|
||||
title?: string;
|
||||
show_more_href?: string;
|
||||
locale?: string;
|
||||
/** `band` adds a subtle full-width background between major homepage sections */
|
||||
variant?: 'default' | 'band';
|
||||
/** Stable id for heading + `aria-labelledby` when multiple grids appear on one page */
|
||||
titleId?: string;
|
||||
}
|
||||
|
||||
const { theme, articles, title, show_more_href, locale = 'en-US' } = Astro.props;
|
||||
const {
|
||||
theme,
|
||||
articles,
|
||||
title,
|
||||
show_more_href,
|
||||
locale = 'en-US',
|
||||
variant = 'default',
|
||||
titleId = 'latest-title',
|
||||
} = Astro.props;
|
||||
const { ArticleCard } = getThemeComponents(theme);
|
||||
const ui = getUiStrings(locale);
|
||||
const heading = title ?? ui.latest_news;
|
||||
---
|
||||
<section class="latest-section" aria-labelledby="latest-title">
|
||||
<section
|
||||
class:list={['latest-section', variant === 'band' && 'latest-section--band']}
|
||||
aria-labelledby={titleId}
|
||||
>
|
||||
<div class="container">
|
||||
<div class="latest-header">
|
||||
<h2 class="section-title" id="latest-title">{heading}</h2>
|
||||
<h2 class="section-title" id={titleId}>{heading}</h2>
|
||||
{show_more_href && (
|
||||
<a href={show_more_href} class="btn btn--ghost latest-more">{ui.view_all}</a>
|
||||
)}
|
||||
@@ -38,6 +53,11 @@ const heading = title ?? ui.latest_news;
|
||||
padding: var(--space-8) 0;
|
||||
}
|
||||
|
||||
.latest-section--band {
|
||||
background: var(--color-bg-secondary);
|
||||
border-block: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.latest-header {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
|
||||
@@ -20,6 +20,8 @@ export type UiStrings = {
|
||||
next_page: string;
|
||||
min_read: string;
|
||||
featured: string;
|
||||
/** Label for the homepage trending strip */
|
||||
trending_strip: string;
|
||||
privacy: string;
|
||||
terms: string;
|
||||
sitemap: string;
|
||||
@@ -71,6 +73,7 @@ const en: UiStrings = {
|
||||
next_page: 'Next page',
|
||||
min_read: 'min read',
|
||||
featured: 'Featured',
|
||||
trending_strip: 'Trending',
|
||||
privacy: 'Privacy policy',
|
||||
terms: 'Terms',
|
||||
sitemap: 'Sitemap',
|
||||
@@ -121,6 +124,7 @@ const pt: UiStrings = {
|
||||
next_page: 'Página seguinte',
|
||||
min_read: 'min de leitura',
|
||||
featured: 'Destaque',
|
||||
trending_strip: 'Tendências',
|
||||
privacy: 'Política de privacidade',
|
||||
terms: 'Termos',
|
||||
sitemap: 'Mapa do site',
|
||||
|
||||
@@ -5,11 +5,13 @@ import DiscoverSection from '../themes/institutional/DiscoverSection.astro';
|
||||
import FpaAgendaSection from '../themes/institutional/FpaAgendaSection.astro';
|
||||
import { getDomainConfig } from '../lib/domain.js';
|
||||
import { getThemeComponents } from '../lib/theme.js';
|
||||
import { getUiStrings } from '../lib/ui-i18n.js';
|
||||
import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../lib/bridge.js';
|
||||
|
||||
const host = Astro.request.headers.get('host') ?? 'localhost';
|
||||
const site_host = host.split(':')[0];
|
||||
const config = getDomainConfig(host);
|
||||
const ui = getUiStrings(config.locale);
|
||||
const { TrendingBar, HeroSection, CategorySection } = getThemeComponents(config.theme);
|
||||
|
||||
let bridge: BridgeClient;
|
||||
@@ -52,7 +54,7 @@ const website_schema = {
|
||||
description={config.tagline}
|
||||
schema={website_schema}
|
||||
>
|
||||
<TrendingBar articles={homepage.trending} />
|
||||
<TrendingBar articles={homepage.trending} locale={config.locale} />
|
||||
<HeroSection articles={homepage.hero} locale={config.locale} />
|
||||
|
||||
{config.fpa_agenda_enabled && homepage.fpa_agenda != null && (
|
||||
@@ -65,9 +67,22 @@ const website_schema = {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{homepage.featured.length > 0 && (
|
||||
<LatestGrid
|
||||
theme={config.theme}
|
||||
articles={homepage.featured}
|
||||
title={ui.featured}
|
||||
titleId="homepage-featured-title"
|
||||
variant="band"
|
||||
show_more_href="/category/all"
|
||||
locale={config.locale}
|
||||
/>
|
||||
)}
|
||||
|
||||
<LatestGrid
|
||||
theme={config.theme}
|
||||
articles={homepage.latest}
|
||||
titleId="homepage-latest-title"
|
||||
show_more_href="/category/all"
|
||||
locale={config.locale}
|
||||
/>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
---
|
||||
import type { ArticleListItem, CategoryConfig } from '../../types/index.js';
|
||||
import { getUiStrings } from '../../lib/ui-i18n.js';
|
||||
import ArticleCard from './ArticleCard.astro';
|
||||
|
||||
interface Props {
|
||||
@@ -9,13 +10,14 @@ interface Props {
|
||||
}
|
||||
|
||||
const { category, articles, locale = 'en-US' } = Astro.props;
|
||||
const ui = getUiStrings(locale);
|
||||
---
|
||||
{articles.length > 0 && (
|
||||
<section class="cat-section" aria-labelledby={`cat-${category.slug}`}>
|
||||
<div class="container">
|
||||
<div class="cat-section__header">
|
||||
<h2 class="section-title" id={`cat-${category.slug}`}>{category.label}</h2>
|
||||
<a href={`/category/${category.slug}`} class="btn btn--ghost cat-more">More {category.label}</a>
|
||||
<a href={`/category/${category.slug}`} class="btn btn--ghost cat-more">{ui.view_all}</a>
|
||||
</div>
|
||||
|
||||
<div class="cat-grid">
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
---
|
||||
import type { ArticleListItem } from '../../types/index.js';
|
||||
import { getUiStrings } from '../../lib/ui-i18n.js';
|
||||
|
||||
interface Props {
|
||||
articles: ArticleListItem[];
|
||||
locale?: string;
|
||||
}
|
||||
const { articles } = Astro.props;
|
||||
const { articles, locale = 'en-US' } = Astro.props;
|
||||
const ui = getUiStrings(locale);
|
||||
---
|
||||
{articles.length > 0 && (
|
||||
<div class="trending-bar" role="complementary" aria-label="Trending articles">
|
||||
@@ -14,7 +17,7 @@ const { articles } = Astro.props;
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="var(--color-primary)" aria-hidden="true">
|
||||
<path d="M17.66 11.2C17.43 10.9 17.15 10.64 16.89 10.38C16.22 9.78 15.46 9.35 14.82 8.72C13.33 7.26 13 4.85 13.95 3C13 3.23 12.17 3.75 11.46 4.32C8.87 6.4 7.85 10.07 9.07 13.22C9.11 13.32 9.15 13.42 9.15 13.55C9.15 13.77 9 13.97 8.8 14.05C8.57 14.15 8.33 14.09 8.14 13.93C8.08 13.88 8.04 13.83 8 13.76C6.87 12.33 6.69 10.28 7.45 8.64C5.78 10 4.87 12.3 5 14.47C5.06 14.97 5.12 15.47 5.29 15.97C5.43 16.57 5.7 17.17 6 17.7C7.08 19.43 8.95 20.67 10.96 20.92C13.1 21.19 15.39 20.8 17.03 19.32C18.86 17.66 19.5 15 18.56 12.72L18.43 12.46C18.22 12 17.66 11.2 17.66 11.2Z"/>
|
||||
</svg>
|
||||
Trending
|
||||
{ui.trending_strip}
|
||||
</span>
|
||||
<ul class="trending-bar__list" role="list">
|
||||
{articles.map((article, i) => (
|
||||
|
||||
Reference in new issue
Block a user