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'
export const load: PageServerLoad = async ({ setHeaders }) => {
export const load: PageServerLoad = async () => {
try {
const res = await fetchAPI(`query {
locations: location (filter: { status: { _eq: "published" }}) {
@@ -87,8 +87,6 @@ export const load: PageServerLoad = async ({ setHeaders }) => {
}`)
if (res) {
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=59' })
const { data: { countPhotos, countLocations, countCountries, ...rest }} = res
return {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ setHeaders }) => {
try {
const res = await fetchAPI(`query {
credits {
@@ -28,11 +28,15 @@ export const load: PageServerLoad = async () => {
}
}`)
if (res) {
const { data } = res
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
return {
...data
}
}
} catch (err) {
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'
export const load: PageServerLoad = async ({ url }) => {
export const load: PageServerLoad = async ({ url, setHeaders }) => {
try {
// Query parameters
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
return {
@@ -73,6 +76,7 @@ export const load: PageServerLoad = async ({ url }) => {
filteredCountryExists: data.country.length > 0,
totalPhotos: data.total_published[0].count.id,
}
}
} catch (err) {
throw error(500, err.message)
}

View File

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

View File

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

View File

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

View File

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