Skip to content

Commit

Permalink
enable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Jan 29, 2024
1 parent b452b18 commit 2b42f48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
18 changes: 11 additions & 7 deletions app/api/preSearch/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ export const runtime = 'edge'
*/
export async function GET() {
try {
// Generate random device ID.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
const array = new Uint32Array(24)
const deviceId = self.crypto.getRandomValues(array)

// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${deviceId}`,
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -121,7 +116,16 @@ export async function GET() {
)

// Send the response.
return new Response(JSON.stringify(filtered))
return new Response(JSON.stringify(filtered), {
headers: {
'Content-Type': 'application/json',
'Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'Vercel-CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`
},
status: 200,
statusText: 'OK'
})
} catch (error) {
// Issue? Leave a message and bail.
console.error(error)
Expand Down
11 changes: 4 additions & 7 deletions app/api/reddit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,9 @@ export async function GET(request: Request) {
: ''

try {
// Generate random device ID.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
const array = new Uint32Array(24)
const deviceId = self.crypto.getRandomValues(array)

// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${deviceId}`,
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -190,7 +185,9 @@ export async function GET(request: Request) {
return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
'Cache-Control': 's-maxage=300, stale-while-revalidate'
'Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'Vercel-CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`
},
status: 200,
statusText: 'OK'
Expand Down
11 changes: 4 additions & 7 deletions app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ export async function GET(request: Request) {
: config.redditApi.subReddit

try {
// Generate random device ID.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
const array = new Uint32Array(24)
const deviceId = self.crypto.getRandomValues(array)

// Try and fetch a new access token.
const tokenResponse = await fetch(
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${deviceId}`,
`https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&device_id=${config.deviceId}`,
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -133,7 +128,9 @@ export async function GET(request: Request) {
return new Response(JSON.stringify(filtered), {
headers: {
'Content-Type': 'application/json',
'Cache-Control': 's-maxage=300, stale-while-revalidate'
'Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`,
'Vercel-CDN-Cache-Control': `public, s-maxage=${config.cacheTtl}`
},
status: 200,
statusText: 'OK'
Expand Down

0 comments on commit 2b42f48

Please sign in to comment.