From 036b5ae2908abdd37b5a27daebda87627cf8224c Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Fri, 13 Dec 2024 11:58:30 -0800 Subject: [PATCH 1/4] Moving base page to load data on return. --- src/routes/+page.server.ts | 68 +++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index af5bdec..7ca1d88 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,60 +1,60 @@ -import type { MetaTagsProps } from 'svelte-meta-tags'; -import { PUBLIC_SITE_URL } from '$env/static/public'; -import type { PageServerLoad } from './$types'; -import { fetchBandcampAlbums } from '$lib/util/fetchBandcampAlbums'; -import type { Album } from '$lib/types/album'; -import type { ArticlePageLoad } from '$lib/types/article'; +import type { MetaTagsProps } from "svelte-meta-tags"; +import { PUBLIC_SITE_URL } from "$env/static/public"; +import type { PageServerLoad } from "./$types"; +import { fetchBandcampAlbums } from "$lib/util/fetchBandcampAlbums"; +import type { Album } from "$lib/types/album"; +import type { ArticlePageLoad } from "$lib/types/article"; export const load: PageServerLoad = async ({ fetch, setHeaders, url }) => { let baseUrl; - if (url.origin.includes('prerender')) { - baseUrl = PUBLIC_SITE_URL || 'https://bradleyshellnut.com'; + if (url.origin.includes("prerender")) { + baseUrl = PUBLIC_SITE_URL || "https://bradleyshellnut.com"; } else { - baseUrl = new URL(url.origin).href || PUBLIC_SITE_URL || 'https://bradleyshellnut.com'; + baseUrl = + new URL(url.origin).href || + PUBLIC_SITE_URL || + "https://bradleyshellnut.com"; } const currentPageUrl = new URL(url.pathname, url.origin).href; const metaTags: MetaTagsProps = Object.freeze({ - title: 'Home', - description: "My name is Bradley Shellnut and I'm a Full Stack Software Engineer.", + title: "Home", + description: + "My name is Bradley Shellnut and I'm a Full Stack Software Engineer.", openGraph: { - title: 'Home', - description: "My name is Bradley Shellnut and I'm a Full Stack Software Engineer.", + title: "Home", + description: + "My name is Bradley Shellnut and I'm a Full Stack Software Engineer.", url: currentPageUrl, - siteName: 'Bradley Shellnut Personal Website', - type: 'website', - locale: 'en_US', + siteName: "Bradley Shellnut Personal Website", + type: "website", + locale: "en_US", images: [ { url: `${baseUrl}og?header=Home | bradleyshellnut.com&page=Hi I'm Bradley Shellnut.&content=I'm a full stack software engineer currently working on Java Spring, PostgreSQL, and React / Angular JS.`, - alt: 'Bradley Shellnut Website Home Page', + alt: "Bradley Shellnut Website Home Page", width: 1200, - height: 630 - } - ] + height: 630, + }, + ], }, twitter: { - title: 'Home', - description: 'Home page', - card: 'summary_large_image', + title: "Home", + description: "Home page", + card: "summary_large_image", image: `${baseUrl}og?header=Home | bradleyshellnut.com&page=Hi I'm Bradley Shellnut.&content=I'm a full stack software engineer currently working on Java Spring, PostgreSQL, and React / Angular JS.`, - imageAlt: 'Bradley Shellnut Website Logo' + imageAlt: "Bradley Shellnut Website Logo", }, - url: currentPageUrl + url: currentPageUrl, }); - const [albums, articles]: [Album[], ArticlePageLoad] = await Promise.all([ - await fetchBandcampAlbums(), - (await fetch(`/api/articles?page=1&limit=3`)).json() - ]); - setHeaders({ - 'cache-control': 'max-age=43200' + "cache-control": "max-age=43200", }); return { baseUrl, metaTagsChild: metaTags, - albums, - articlesData: articles + albums: await fetchBandcampAlbums(), + articlesData: await (await fetch("/api/articles?page=1&limit=3")).json(), }; -}; \ No newline at end of file +}; From 17d8498e2ab8bc13607eb64d5a400a4e9327ecca Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Fri, 13 Dec 2024 13:02:55 -0800 Subject: [PATCH 2/4] Trying a different await approach. --- src/routes/+page.server.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 7ca1d88..64e95ec 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -48,6 +48,8 @@ export const load: PageServerLoad = async ({ fetch, setHeaders, url }) => { url: currentPageUrl, }); + const articles = await fetch("/api/articles?page=1&limit=3"); + setHeaders({ "cache-control": "max-age=43200", }); @@ -55,6 +57,6 @@ export const load: PageServerLoad = async ({ fetch, setHeaders, url }) => { baseUrl, metaTagsChild: metaTags, albums: await fetchBandcampAlbums(), - articlesData: await (await fetch("/api/articles?page=1&limit=3")).json(), + articlesData: await articles.json(), }; }; From 9ec7b8c1886f67e85b77815f3f02de66121d65ae Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Fri, 13 Dec 2024 13:21:19 -0800 Subject: [PATCH 3/4] Console log. --- src/lib/api.ts | 4 ++++ src/routes/api/articles/+server.ts | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index 807291a..bf06f61 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -44,12 +44,16 @@ export async function fetchArticlesApi( }); if (USE_REDIS_CACHE) { + console.log('Using redis cache'); const cached = await redis.get(entriesQueryParams.toString()); if (cached) { + console.log("Cache hit!"); const response = JSON.parse(cached); const ttl = await redis.ttl(entriesQueryParams.toString()); + console.log(`Response ${JSON.stringify(response)}`); + console.log(`Returning cached response with ttl of ${ttl} seconds`); return { ...response, cacheControl: `max-age=${ttl}` }; } } diff --git a/src/routes/api/articles/+server.ts b/src/routes/api/articles/+server.ts index d0733b1..cbad32e 100644 --- a/src/routes/api/articles/+server.ts +++ b/src/routes/api/articles/+server.ts @@ -10,11 +10,13 @@ export async function GET({ setHeaders, url }) { } try { - const response = await fetchArticlesApi('get', `fetchArticles`, { + const response = await fetchArticlesApi('get', 'fetchArticles', { page, limit }); + console.log(`JSON articles response: ${JSON.stringify(response)}`); + if (response?.articles) { if (response?.cacheControl) { if (!response.cacheControl.includes('no-cache')) { From ccee1690787974429f9ac90d318cce36cc1992c2 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Fri, 13 Dec 2024 13:39:13 -0800 Subject: [PATCH 4/4] ttl for bandcamp albums. --- src/lib/util/fetchBandcampAlbums.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/util/fetchBandcampAlbums.ts b/src/lib/util/fetchBandcampAlbums.ts index dbeca6b..817cc62 100644 --- a/src/lib/util/fetchBandcampAlbums.ts +++ b/src/lib/util/fetchBandcampAlbums.ts @@ -11,10 +11,10 @@ export async function fetchBandcampAlbums() { if (cached) { const response: Album[] = JSON.parse(cached); - console.log(`Cache hit!`); + console.log('Albums cache hit!'); const ttl = await redis.ttl('bandcampAlbums'); - return response; + return { ...response, cacheControl: `max-age=${ttl}` }; } }