Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache people page #213

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[images]
remote_images = ["https://v5.airtableusercontent.com.*"]
11 changes: 9 additions & 2 deletions src/routes/api/people/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { options } from '$lib/api.js'
import type { Person } from '$lib/types.js'
import { error, json } from '@sveltejs/kit'

export const prerender = true

function airtableToCache(url: string): string {
return `/.netlify/images?url=${url}`
}

function recordToPerson(record: any): Person {
return {
id: record.id || 'noId',
name: record.fields.Name,
bio: record.fields.bio,
title: record.fields.title,
image: record.fields.image && record.fields.image[0].thumbnails.large.url,
image: record.fields.image && airtableToCache(record.fields.image[0].thumbnails.large.url),
privacy: record.fields.privacy,
org: record.fields.organisation,
checked: record.fields.checked
Expand Down Expand Up @@ -42,7 +48,7 @@ async function fetchAllPages(fetch: any, url: any) {
return allRecords
}

export async function GET({ fetch }) {
export async function GET({ fetch, setHeaders }) {
const url = `https://api.airtable.com/v0/appWPTGqZmUcs3NWu/tblZhQc49PkCz3yHd`

try {
Expand All @@ -52,6 +58,7 @@ export async function GET({ fetch }) {
.filter(filter)
// Shuffle the array, although not truly random
.sort(() => 0.5 - Math.random())

return json(out)
} catch (e) {
return error(500, 'err')
Expand Down
7 changes: 5 additions & 2 deletions src/routes/people/+page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Person } from '$lib/types'

export const prerender = false
/** Airtable image links need to be refreshed every 2 hours */
export const prerender = true

export const load = async ({ fetch }) => {
const response = await fetch('api/people')
const people: Person[] = await response.json()

return {
people: people
people: people,
maxage: 60
}
}