data endpoint for entity 90008 (aka. a website)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: improve guestbook page perf by not fetching posts and replies every single load

dusk af1cdcab 43c23359

+34 -21
+2
src/hooks.server.ts
··· 18 18 } from '$lib/visits'; 19 19 import { testUa } from '$lib/robots'; 20 20 import { error } from '@sveltejs/kit'; 21 + import { _fetchEntries } from './routes/guestbook/+page.server'; 21 22 22 23 const UPDATE_LAST_JOB_NAME = 'update steam game, lastfm track, bsky posts, git activity'; 23 24 ··· 34 35 steamUpdateNowPlaying(), 35 36 lastFmUpdateNowPlaying(), 36 37 updateLastPosts(), 38 + _fetchEntries(), 37 39 updateCommits(), 38 40 sendAllMetrics() // send all metrics every minute 39 41 ]);
+32 -21
src/routes/guestbook/+page.server.ts
··· 23 23 }; 24 24 25 25 const postTokens = writable<Set<string>>(new Set()); 26 + const entries = writable<NoteData[]>([]); 27 + 28 + export const _fetchEntries = async () => { 29 + const newEntries: NoteData[] = []; 30 + const { posts } = await getUserPosts('did:web:guestbook.gaze.systems', 16); 31 + const fetchPostReplies = async (post: Post) => { 32 + if ((post.replyCount ?? 0) === 0) return { post, replies: [] }; 33 + return { post, replies: await post.fetchChildren({ depth: 1, force: true }) }; 34 + }; 35 + const postsWithReplies = await Promise.all(posts.map(fetchPostReplies)); 36 + for (const { post, replies } of postsWithReplies) { 37 + const note = noteFromBskyPost(post); 38 + note.children = replies.map((reply) => { 39 + const replyNote = noteFromBskyPost(reply); 40 + replyNote.purposeAction = 'reply'; 41 + replyNote.outgoingLinks = [{ name: 'bsky-reply', link: reply.uri }]; 42 + return replyNote; 43 + }); 44 + newEntries.push(note); 45 + } 46 + entries.set(newEntries); 47 + return newEntries; 48 + }; 26 49 27 50 export const actions = { 28 51 post: async (event: RequestEvent) => { ··· 55 78 export async function load({ cookies }) { 56 79 const scopedCookies = scopeCookies(cookies); 57 80 const data = { 58 - entries: [] as NoteData[], 81 + entries: get(entries), 59 82 sendError: scopedCookies.get('sendError') || '', 60 83 getError: '', 61 84 sendRatelimited: scopedCookies.get('sendRatelimited') || '', 62 85 getRatelimited: false, 63 86 fillText: fancyText(getVisitorId(cookies) ?? nanoid()) 64 87 }; 88 + let refetchEntries = data.entries.length === 0; 65 89 const rawPostData = scopedCookies.get('postData') || null; 66 90 const postAuth = scopedCookies.get('postAuth') || null; 67 91 if (rawPostData !== null && postAuth !== null) { ··· 97 121 }, 98 122 { resolveFacets: false } 99 123 ); 124 + refetchEntries = true; 100 125 // eslint-disable-next-line @typescript-eslint/no-explicit-any 101 126 } catch (err: any) { 102 127 scopedCookies.set('sendError', err.toString()); ··· 107 132 // delete the cookies after we get em since we dont really need these more than once 108 133 scopedCookies.delete('sendError'); 109 134 scopedCookies.delete('sendRatelimited'); 110 - // actually get posts 111 - try { 112 - const { posts } = await getUserPosts('did:web:guestbook.gaze.systems', 16); 113 - const fetchPostReplies = async (post: Post) => { 114 - if ((post.replyCount ?? 0) === 0) return { post, replies: [] }; 115 - return { post, replies: await post.fetchChildren({ depth: 1, force: true }) }; 116 - }; 117 - const postsWithReplies = await Promise.all(posts.map(fetchPostReplies)); 118 - for (const { post, replies } of postsWithReplies) { 119 - const note = noteFromBskyPost(post); 120 - note.children = replies.map((reply) => { 121 - const replyNote = noteFromBskyPost(reply); 122 - replyNote.purposeAction = 'reply'; 123 - replyNote.outgoingLinks = [{ name: 'bsky-reply', link: reply.uri }]; 124 - return replyNote; 125 - }); 126 - data.entries.push(note); 135 + if (refetchEntries) { 136 + try { 137 + data.entries = await _fetchEntries(); 138 + // eslint-disable-next-line @typescript-eslint/no-explicit-any 139 + } catch (err: any) { 140 + data.getError = err.toString(); 127 141 } 128 - // eslint-disable-next-line @typescript-eslint/no-explicit-any 129 - } catch (err: any) { 130 - data.getError = err.toString(); 131 142 } 132 143 133 144 return data;