diff --git a/src/lib/components/Articles.svelte b/src/lib/components/Articles.svelte index 49baeb4..5844697 100644 --- a/src/lib/components/Articles.svelte +++ b/src/lib/components/Articles.svelte @@ -8,7 +8,7 @@ const { totalArticles, compact = false, classes = [], -}: { articles: Article[]; totalArticles: number; compact: boolean; classes: string[] } = $props(); +}: { articles: Article[]; totalArticles: number; compact: boolean; classes?: string[] } = $props();
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 7e1bd3b..de6d6d5 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -6,55 +6,55 @@ import type { MetaTagsProps } from 'svelte-meta-tags'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ fetch, setHeaders, url }) => { - let baseUrl; - 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'; - } - const currentPageUrl = new URL(url.pathname, url.origin).href; + let baseUrl; + 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'; + } + 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.", - openGraph: { - 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', - 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', - width: 1200, - height: 630 - } - ] - }, - twitter: { - 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' - }, - url: currentPageUrl - }); + const metaTags: MetaTagsProps = Object.freeze({ + 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.", + url: currentPageUrl, + 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', + width: 1200, + height: 630, + }, + ], + }, + twitter: { + 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', + }, + url: currentPageUrl, + }); - // const [albums, articles]: [Album[], ArticlePageLoad] = await Promise.all([ - // await fetchBandcampAlbums(), - // (await fetch('/api/articles?page=1&limit=3')).json() - // ]); + const [albums, articles]: [Album[], ArticlePageLoad] = await Promise.all([ + (await fetch('/api/bandcamp/albums')).json(), + (await fetch('/api/articles?page=1&limit=3')).json(), + ]); - setHeaders({ - 'cache-control': 'max-age=43200' - }); - return { - baseUrl, - metaTagsChild: metaTags, - albums: await fetchBandcampAlbums(), - articlesData: await (await fetch('/api/articles?page=1&limit=3')).json() - }; -}; \ No newline at end of file + setHeaders({ + 'cache-control': 'max-age=43200', + }); + return { + baseUrl, + metaTagsChild: metaTags, + albums, + articlesData: articles, + }; +}; diff --git a/src/routes/api/bandcamp/albums/+server.ts b/src/routes/api/bandcamp/albums/+server.ts new file mode 100644 index 0000000..60a15c7 --- /dev/null +++ b/src/routes/api/bandcamp/albums/+server.ts @@ -0,0 +1,56 @@ +import { json, error } from '@sveltejs/kit'; +import { BANDCAMP_USERNAME, PAGE_SIZE, USE_REDIS_CACHE } from '$env/static/private'; +import { fetchArticlesApi } from '$lib/api'; +import { redis } from '$root/lib/server/redis'; +import type { Album, BandCampResults } from '$lib/types/album'; +import scrapeIt, { type ScrapeResult } from 'scrape-it'; + +export async function GET({ setHeaders, url }) { + try { + if (USE_REDIS_CACHE) { + const cached: string | null = await redis.get('bandcampAlbums'); + + if (cached) { + const response: Album[] = JSON.parse(cached); + const ttl = await redis.ttl('bandcampAlbums'); + + return response; + } + } + + const { data }: ScrapeResult = await scrapeIt(`https://bandcamp.com/${BANDCAMP_USERNAME}`, { + collectionItems: { + listItem: '.collection-item-container', + data: { + url: { + selector: '.collection-title-details > a.item-link', + attr: 'href', + }, + artwork: { + selector: 'div.collection-item-art-container a img', + attr: 'src', + }, + title: { + selector: 'span.item-link-alt > div.collection-item-title', + }, + artist: { + selector: 'span.item-link-alt > div.collection-item-artist', + }, + }, + }, + }); + + const albums: Album[] = data?.collectionItems || []; + + if (albums && albums?.length > 0) { + if (USE_REDIS_CACHE) { + redis.set('bandcampAlbums', JSON.stringify(albums), 'EX', 43200); + } + return json(albums); + } + return json([]); + } catch (error) { + console.error(error); + return json([]); + } +}