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

Check hourly for etag header change on the hours data #200

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 25 additions & 0 deletions app/api/library-hours/cron/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {NextResponse} from "next/server"
import {revalidateTag, unstable_cache as nextCache} from "next/cache"
import {fetchLibraryHours} from "../fetch-library-hours"

export const revalidate = 0

const checkEtagHeader = async () => {
const getPreviousEtag = nextCache(
async () => {
const fetchData = await fetchLibraryHours()
return fetchData.headers.get("etag")
},
["library-hours-etag"],
{tags: ["library-hours"]}
)

const fetchData = await fetchLibraryHours()
return fetchData.headers.get("etag") != (await getPreviousEtag())
}

export const GET = async () => {
const invalidated = await checkEtagHeader()
if (invalidated) revalidateTag("library-hours")
return NextResponse.json({invalidated})
}
14 changes: 14 additions & 0 deletions app/api/library-hours/fetch-library-hours.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const fetchLibraryHours = async () => {
const from = new Date()
from.setDate(from.getDate() - from.getDay())
const to = new Date()
to.setDate(to.getDate() + 6)

const params = new URLSearchParams()
params.set("from", from.toISOString().replace(/T.*/, ""))
params.set("to", to.toISOString().replace(/T.*/, ""))

return fetch(`https://library-hours.stanford.edu/libraries.json?${params.toString()}`, {
cache: "no-cache",
})
}
35 changes: 4 additions & 31 deletions app/api/library-hours/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {NextResponse} from "next/server"
import {DayHours} from "@/lib/hooks/useLibraryHours"
import {LibraryHours} from "@/lib/drupal/drupal"
import {revalidateTag, unstable_cache as nextCache} from "next/cache"
import {fetchLibraryHours} from "./fetch-library-hours"

type FetchedData = {
data: []
Expand All @@ -17,28 +18,16 @@ type FetchedData = {
}[]
}

export const revalidate = 3600
export const revalidate = false

const getLibraryHours = nextCache(
async () => {
const from = new Date()
from.setDate(from.getDate() - from.getDay())
const to = new Date()
to.setDate(to.getDate() + 6)

const params = new URLSearchParams()
params.set("from", from.toISOString().replace(/T.*/, ""))
params.set("to", to.toISOString().replace(/T.*/, ""))

const data: FetchedData = await fetch(`https://library-hours.stanford.edu/libraries.json?${params.toString()}`, {
cache: "no-cache",
})
const data: FetchedData = await fetchLibraryHours()
.then(res => res.json())
.catch(e => {
console.error(e)
return NextResponse.json([])
})

const deserializedData = deserialize(data) as LibraryHours[]
if (!deserializedData) {
return NextResponse.json([])
Expand Down Expand Up @@ -82,28 +71,12 @@ const getLibraryHours = nextCache(
["library-hours"],
{
tags: ["library-hours"],
// Revalidate at 1 second after midnight. Calculate how many seconds since midnight, and subtract from total seconds
// in a day.
revalidate:
60 * 60 * 24 +
1 -
(parseInt(
new Date().toLocaleTimeString("en-us", {
hour12: false,
hour: "numeric",
timeZone: "America/Los_Angeles",
})
) *
60 *
60 +
new Date().getMinutes() * 60 +
new Date().getSeconds()),
}
)

export const GET = async () => {
const hours = await getLibraryHours()
// If no data, try to invalidate the cached data so we can re-try fetching the data.
// If no data, try to invalidate the cached data, so we can re-try fetching the data.
if (!hours) revalidateTag("library-hours")
return NextResponse.json(hours)
}
6 changes: 6 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@
"source": "/assets/:path*",
"destination": "https://discover.stanford.edu/assets/:path*"
}
],
"crons": [
{
"path": "/api/library-hours/cron",
"schedule": "0 * * * *"
}
]
}
Loading