naslovnica: /api/articles dobi limit, offset in light=1 (brez content in targetUrl); home naloži prvih 30 lahkih zapisov, ostalo dolije v ozadju; determinističen vrstni red namesto Math.random, da se mreža ob dolivanju ne premeša
This commit is contained in:
@@ -711,9 +711,24 @@ export default function Home() {
|
||||
};
|
||||
}, []);
|
||||
|
||||
const { data: allArticles, isLoading } = useQuery<Article[]>({
|
||||
queryKey: ["/api/articles"],
|
||||
// Prvi paket je majhen in brez polja content, da se naslovnica izrise takoj;
|
||||
// ostalo se dolije v ozadju. Prej je sla naslovnica po vseh 550 clankov s
|
||||
// celotnim besedilom (1,77 MB).
|
||||
const { data: firstBatch, isLoading } = useQuery<Article[]>({
|
||||
queryKey: ["/api/articles?limit=30&light=1"],
|
||||
});
|
||||
const { data: restBatch } = useQuery<Article[]>({
|
||||
queryKey: ["/api/articles?offset=30&light=1"],
|
||||
enabled: !!firstBatch,
|
||||
});
|
||||
const allArticles = useMemo(() => {
|
||||
const seen = new Set<number>();
|
||||
const out: Article[] = [];
|
||||
for (const a of [...(firstBatch || []), ...(restBatch || [])]) {
|
||||
if (!seen.has(a.id)) { seen.add(a.id); out.push(a); }
|
||||
}
|
||||
return out;
|
||||
}, [firstBatch, restBatch]);
|
||||
|
||||
// Splosne novice imajo svojo rubriko /aktuelles in ne sodijo na naslovnico,
|
||||
// ki ostaja Volksmusik in Schlager.
|
||||
@@ -738,12 +753,14 @@ export default function Home() {
|
||||
|
||||
const shuffled = useMemo(() => {
|
||||
if (!articles) return [];
|
||||
const arr = [...articles];
|
||||
for (let i = arr.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[arr[i], arr[j]] = [arr[j], arr[i]];
|
||||
}
|
||||
return arr;
|
||||
const key = (id: number) => {
|
||||
let h = id * 2654435761 % 4294967296;
|
||||
h ^= h >>> 13; h = (h * 1274126177) % 4294967296;
|
||||
return h;
|
||||
};
|
||||
const head = articles.slice(0, 30).sort((a, b) => key(a.id) - key(b.id));
|
||||
const tail = articles.slice(30).sort((a, b) => key(a.id) - key(b.id));
|
||||
return [...head, ...tail];
|
||||
}, [articles]);
|
||||
|
||||
const widgets = useMemo(() => [
|
||||
|
||||
Reference in New Issue
Block a user