diff --git a/server/sitemap.js b/server/sitemap.js index 934cc53e8..c9d5b3e05 100644 --- a/server/sitemap.js +++ b/server/sitemap.js @@ -1,3 +1,5 @@ +import { createRequire } from 'node:module' + /** * @returns {Promise} */ @@ -5,9 +7,14 @@ export async function sitemap() { const manifest = await import('../build/server/manifest.js'); const prerendered = manifest.prerendered; + const routes = [ + ...prerendered, + ...collectThreads() + ]; + const sitemap = ` - ${[...prerendered].filter(route => !route.endsWith('.json')).map(route => ` + ${[...routes].filter(route => !route.endsWith('.json')).map(route => ` https://appwrite.io${route} `).join('')} @@ -20,4 +27,10 @@ export async function sitemap() { } next(); } +} + +function collectThreads() { + const threads = createRequire(import.meta.url)('../build/prerendered/threads/data.json'); + + return threads.map(id => `/threads/${id}`); } \ No newline at end of file diff --git a/src/routes/threads/[id]/+page.server.ts b/src/routes/threads/[id]/+page.server.ts index b979cc441..c5db57928 100644 --- a/src/routes/threads/[id]/+page.server.ts +++ b/src/routes/threads/[id]/+page.server.ts @@ -5,7 +5,7 @@ export const prerender = 'auto'; export const entries = async () => { const ids = []; - for await (const thread of iterateAllThreads()) { + for await (const thread of iterateAllThreads(100)) { ids.push({ id: thread.$id }); } diff --git a/src/routes/threads/data.json/+server.ts b/src/routes/threads/data.json/+server.ts new file mode 100644 index 000000000..7263a20a9 --- /dev/null +++ b/src/routes/threads/data.json/+server.ts @@ -0,0 +1,14 @@ +import { iterateAllThreads } from '../helpers'; +import type { RequestHandler } from './$types'; +import { json } from '@sveltejs/kit'; + +export const prerender = true; + +export const GET: RequestHandler = async () => { + const ids = []; + for await (const thread of iterateAllThreads()) { + ids.push(thread.$id); + } + + return json(ids); +}; diff --git a/src/routes/threads/helpers.ts b/src/routes/threads/helpers.ts index a1d71d579..0f6485088 100644 --- a/src/routes/threads/helpers.ts +++ b/src/routes/threads/helpers.ts @@ -112,7 +112,7 @@ export async function getThreadMessages(threadId: string) { ); } -export async function* iterateAllThreads() { +export async function* iterateAllThreads(total: number|undefined = undefined) { let offset = 0; const limit = 100; while (true) { @@ -131,5 +131,9 @@ export async function* iterateAllThreads() { } offset += limit; + + if (total !== undefined && offset >= total) { + break; + } } } diff --git a/src/routes/threads/types.ts b/src/routes/threads/types.ts index b778bcec1..6714c048a 100644 --- a/src/routes/threads/types.ts +++ b/src/routes/threads/types.ts @@ -18,7 +18,7 @@ export interface DiscordMessage extends Pick { timestamp: string; } -export interface DiscordThread extends Pick { +export interface DiscordThread extends Models.Document { discord_id: string; author: string; tags?: string[];