diff --git a/netlify.toml b/netlify.toml index c4d74854..0aece3d7 100644 --- a/netlify.toml +++ b/netlify.toml @@ -10,7 +10,7 @@ [[redirects]] from = "/*" - to = "/.netlify/functions/server" + to = "/.netlify/edge-functions/server" status = 200 [[headers]] diff --git a/src/entry.server.tsx b/src/entry.server.tsx index 94b1acf9..6630113c 100644 --- a/src/entry.server.tsx +++ b/src/entry.server.tsx @@ -30,22 +30,22 @@ export default async function handleRequest( responseHeaders: Headers, remixContext: EntryContext ) { - // // check if we have a cached response in memory - // const cachedResponse = cache.get(request.url); - // if (cachedResponse) { - // console.log('Serving from cache', request.url); - // // if we have a cached response, check if it's less than 5 seconds old - // const now = new Date(); - // const diff = now.getTime() - cachedResponse.date.getTime(); - // if (true || diff < 5000) { - // // if it's less than 5 seconds old, return the cached response - // responseHeaders.set('Content-Type', 'text/html'); - // return new Response(cachedResponse.html, { - // headers: responseHeaders, - // status: responseStatusCode, - // }); - // } - // } + // check if we have a cached response in memory + const cachedResponse = cache.get(request.url); + if (cachedResponse) { + console.log('Serving from cache', request.url); + // if we have a cached response, check if it's less than 5 seconds old + const now = new Date(); + const diff = now.getTime() - cachedResponse.date.getTime(); + if (true || diff < 5000) { + // if it's less than 5 seconds old, return the cached response + responseHeaders.set('Content-Type', 'text/html'); + return new Response(cachedResponse.html, { + headers: responseHeaders, + status: responseStatusCode, + }); + } + } let didError = false; const chunks: Uint8Array[] = []; @@ -60,10 +60,15 @@ export default async function handleRequest( } ); + // tee the stream so we can cache it and send it to the client const [toReponse, toCache] = body.tee(); streamToText(toCache).then(html => { - console.log('I have the HTML!', html.length); + console.log('Caching', request.url); + cache.set(request.url, { + html: html.replace('Rendered Fresh',`Rendered from cache ${new Date().toISOString()}`), + date: new Date(), + }); }); const headers = new Headers(responseHeaders); diff --git a/src/util/stats.ts b/src/util/stats.ts index 92bcb14c..b0f040c3 100644 --- a/src/util/stats.ts +++ b/src/util/stats.ts @@ -64,7 +64,6 @@ export function tags() { const counts = allTags.reduce(countInstances, {}); // sort and filter for any tags that only have 1 const tags = Object.entries(counts) - .sort(([, countA], [, countB]) => countB - countA) // Only show the tag if this topic has 3 or more people in it .filter(([, count]) => count >= 3) .map(([name, count]) => ({ name, count })); @@ -85,8 +84,11 @@ export function tags() { delete lowercaseTagMap[normalizedName]; } return acc; - }, []); - + }, []) + // Sort by name first + .sort((a, b) => b.name.toLowerCase() > a.name.toLowerCase()) + // Sort by count + .sort((a, b) => b.count - a.count); return [{ name: 'all', count: people.length }, ...normalizedTags]; } @@ -116,12 +118,12 @@ const normalizedTagMap = tags().reduce((acc, tag) => { }, {}); export function getPeople(tag?: string) { - return people + return [...people] .sort(() => Math.random() - 0.5) .map((person) => { const normalizedPerson = { ...person, - // Clean out people that added basically the same tags twice + // Clean out people that added basically the same tags twice tags: unique( person.tags.map((tag) => normalizedTagMap[normalizeTag(tag)] || tag) ),