Use page specific stale-while-revalidate Cache-Control headers

This commit is contained in:
2022-09-20 00:20:18 +02:00
parent 39fc74d8e9
commit 5545871c85
12 changed files with 57 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ import { fetchAPI } from '$utils/api'
import { PUBLIC_PREVIEW_COUNT } from '$env/static/public' import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
export const load: PageServerLoad = async ({ setHeaders }) => { export const load: PageServerLoad = async () => {
try { try {
const res = await fetchAPI(`query { const res = await fetchAPI(`query {
locations: location (filter: { status: { _eq: "published" }}) { locations: location (filter: { status: { _eq: "published" }}) {
@@ -87,8 +87,6 @@ export const load: PageServerLoad = async ({ setHeaders }) => {
}`) }`)
if (res) { if (res) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=59' })
const { data: { countPhotos, countLocations, countCountries, ...rest }} = res const { data: { countPhotos, countLocations, countCountries, ...rest }} = res
return { return {

View File

@@ -3,7 +3,7 @@ import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { getRandomItems } from '$utils/functions' import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
// Get total of published photos // Get total of published photos
const totalRes = await fetchAPI(`query { const totalRes = await fetchAPI(`query {
@@ -43,6 +43,8 @@ export const load: PageServerLoad = async () => {
const { data: { photo: photos }} = photosRes const { data: { photo: photos }} = photosRes
if (photos) { if (photos) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=599' })
return { return {
photos, photos,
} }

View File

@@ -4,7 +4,7 @@ import { PUBLIC_LIST_AMOUNT } from '$env/static/public'
import { fetchAPI, photoFields } from '$utils/api' import { fetchAPI, photoFields } from '$utils/api'
export const load: PageServerLoad = async ({ params }) => { export const load: PageServerLoad = async ({ params, setHeaders }) => {
try { try {
const { location: slug } = params const { location: slug } = params
@@ -71,6 +71,8 @@ export const load: PageServerLoad = async ({ params }) => {
throw error(404, "This location is not available… yet!") throw error(404, "This location is not available… yet!")
} }
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
location: location[0], location: location[0],
photos, photos,

View File

@@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async ({ params }) => { export const load: PageServerLoad = async ({ params, setHeaders }) => {
try { try {
// Get the first photo ID // Get the first photo ID
const firstPhoto = await fetchAPI(`query { const firstPhoto = await fetchAPI(`query {
@@ -74,6 +74,8 @@ export const load: PageServerLoad = async ({ params }) => {
if (data) { if (data) {
const currentIndex = data.photos.findIndex((photo: any) => photo.slug === params.photo) const currentIndex = data.photos.findIndex((photo: any) => photo.slug === params.photo)
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
photos: data.photos, photos: data.photos,
location: data.location[0], location: data.location[0],

View File

@@ -3,7 +3,7 @@ import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { getRandomItems } from '$utils/functions' import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
// Get data and total of published photos // Get data and total of published photos
const res = await fetchAPI(`query { const res = await fetchAPI(`query {
@@ -91,6 +91,8 @@ export const load: PageServerLoad = async () => {
if (photosRes) { if (photosRes) {
const { data: { photo: photos }} = photosRes const { data: { photo: photos }} = photosRes
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
about, about,
photos, photos,

View File

@@ -3,7 +3,7 @@ import type { RequestHandler } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request, setHeaders }) => {
try { try {
const body = await request.text() const body = await request.text()
@@ -12,6 +12,8 @@ export const POST: RequestHandler = async ({ request }) => {
const res = await req const res = await req
if (res) { if (res) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=59' })
return new Response(JSON.stringify({ return new Response(JSON.stringify({
...res ...res
})) }))

View File

@@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
const res = await fetchAPI(`query { const res = await fetchAPI(`query {
credits { credits {
@@ -28,11 +28,15 @@ export const load: PageServerLoad = async () => {
} }
}`) }`)
if (res) {
const { data } = res const { data } = res
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
...data ...data
} }
}
} catch (err) { } catch (err) {
throw error(500, err.message) throw error(500, err.message)
} }

View File

@@ -4,7 +4,7 @@ import { fetchAPI } from '$utils/api'
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public' import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public'
export const load: PageServerLoad = async ({ url }) => { export const load: PageServerLoad = async ({ url, setHeaders }) => {
try { try {
// Query parameters // Query parameters
const queryCountry = url.searchParams.get('country') || PUBLIC_FILTERS_DEFAULT_COUNTRY const queryCountry = url.searchParams.get('country') || PUBLIC_FILTERS_DEFAULT_COUNTRY
@@ -66,6 +66,9 @@ export const load: PageServerLoad = async ({ url }) => {
} }
}`) }`)
if (res) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=86399' })
const { data } = res const { data } = res
return { return {
@@ -73,6 +76,7 @@ export const load: PageServerLoad = async ({ url }) => {
filteredCountryExists: data.country.length > 0, filteredCountryExists: data.country.length > 0,
totalPhotos: data.total_published[0].count.id, totalPhotos: data.total_published[0].count.id,
} }
}
} catch (err) { } catch (err) {
throw error(500, err.message) throw error(500, err.message)
} }

View File

@@ -4,7 +4,7 @@ import { fetchAPI } from '$utils/api'
import { getRandomItem } from '$utils/functions' import { getRandomItem } from '$utils/functions'
import { fetchSwell } from '$utils/functions/shopServer' import { fetchSwell } from '$utils/functions/shopServer'
export const load: PageServerLoad = async ({}) => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
// Get content from API // Get content from API
const data = await fetchAPI(`query { const data = await fetchAPI(`query {
@@ -42,6 +42,8 @@ export const load: PageServerLoad = async ({}) => {
const shopProduct: any = await fetchSwell(`/products/${randomPoster.product_id}`) const shopProduct: any = await fetchSwell(`/products/${randomPoster.product_id}`)
if (shopProduct) { if (shopProduct) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=86399' })
return { return {
product: randomPoster, product: randomPoster,
shopProduct, shopProduct,

View File

@@ -3,7 +3,7 @@ import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { fetchSwell } from '$utils/functions/shopServer' import { fetchSwell } from '$utils/functions/shopServer'
export const load: PageServerLoad = async ({ params }) => { export const load: PageServerLoad = async ({ params, setHeaders }) => {
try { try {
// Get content from API // Get content from API
const data = await fetchAPI(`query { const data = await fetchAPI(`query {
@@ -39,6 +39,8 @@ export const load: PageServerLoad = async ({ params }) => {
const shopProduct: any = await fetchSwell(`/products/${poster.product_id}`) const shopProduct: any = await fetchSwell(`/products/${poster.product_id}`)
if (shopProduct) { if (shopProduct) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
product: poster, product: poster,
shopProduct, shopProduct,

View File

@@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
const res = await fetchAPI(`query { const res = await fetchAPI(`query {
settings { settings {
@@ -22,12 +22,16 @@ export const load: PageServerLoad = async () => {
} }
}`) }`)
if (res) {
const { data } = res const { data } = res
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=86399' })
return { return {
...data.settings, ...data.settings,
issues: data.newsletter, issues: data.newsletter,
} }
}
} catch (err) { } catch (err) {
throw error(500, err.message) throw error(500, err.message)
} }

View File

@@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async ({ setHeaders }) => {
try { try {
const res = await fetchAPI(`query { const res = await fetchAPI(`query {
legal { legal {
@@ -11,11 +11,15 @@ export const load: PageServerLoad = async () => {
} }
}`) }`)
if (res) {
const { data } = res const { data } = res
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return { return {
...data ...data
} }
}
} catch (err) { } catch (err) {
throw error(500, err.message) throw error(500, err.message)
} }