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

Move signpost to client-side fetching #761

Closed
wants to merge 7 commits 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
19 changes: 8 additions & 11 deletions src/app/harbor/signpost/feed-items.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
'use client'

import { SignpostFeedItem } from '@/app/utils/data'
import { fetchSignpostFeed, SignpostFeedItem } from '@/app/utils/data'
import JaggedCardSmall from '@/components/jagged-card-small'
import Cookies from 'js-cookie'
import { useEffect } from 'react'
import Markdown from 'react-markdown'
import useLocalStorageState from '../../../../lib/useLocalStorageState'

export default function FeedItems() {
const cookie = Cookies.get('signpost-feed')
if (!cookie) return null
const [feedItems, setFeedItems] = useLocalStorageState<SignpostFeedItem[]>([])

let feedItems: SignpostFeedItem[]
try {
feedItems = JSON.parse(cookie).sort((a, b) => a?.autonumber < b?.autonumber)
} catch (e) {
console.error("Could't parse signpost feed cookie into JSON:", e)
return null
}
useEffect(() => {
fetchSignpostFeed().then((r) => setFeedItems(r))
}, [])

if (!feedItems || feedItems.length === 0) {
return <p>No feed updates yet! Check back soon.</p>
return null
}

return (
Expand Down
37 changes: 18 additions & 19 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
person,
} from './app/utils/data'

function checkSize(obj: any) {
return Buffer.byteLength(JSON.stringify(obj))
}

function tooBig(obj: any) {
console.log(checkSize(obj))
// https://vercel.com/docs/errors/REQUEST_HEADER_TOO_LARGE
return checkSize(obj) > 16384
}

export async function userPageMiddleware(request: NextRequest) {
const session = await getSession()
const slackId = session?.slackId
Expand All @@ -20,7 +30,9 @@ export async function userPageMiddleware(request: NextRequest) {
try {
const shipyardPage = request.nextUrl.pathname.startsWith('/shipyard')
if (shipyardPage && !request.cookies.get('ships')) {
const ships = await fetchShips(slackId, 3)
const ships = await fetchShips(slackId, 2)
console.log("ships too big:", tooBig(ships))
ships.map(s => console.log(JSON.stringify(s, null, 2)))
response.cookies.set({
name: 'ships',
value: JSON.stringify(ships),
Expand All @@ -37,10 +49,12 @@ export async function userPageMiddleware(request: NextRequest) {
console.log('Checking for waka cookie')
if (!request.cookies.get('waka')) {
const wakaData = await fetchWaka(session)
console.log("found waka cookie")
let expiration = 60 * 60 * 1000 // In 1 hour
if (wakaData?.hasHb) {
expiration = 7 * 24 * 60 * 60 * 1000 // In 7 days
}
console.log("waka too big:", tooBig(wakaData))
response.cookies.set({
name: 'waka',
value: JSON.stringify(wakaData),
Expand All @@ -53,24 +67,6 @@ export async function userPageMiddleware(request: NextRequest) {
console.log('Middleware errored on waka cookie step', e)
}

// Signpost base
try {
console.log('Checking for signpost-feed cookie')
const signpostPage = request.nextUrl.pathname.startsWith('/signpost')
if (signpostPage && !request.cookies.get('signpost-feed')) {
const signpostFeed = await fetchSignpostFeed()
response.cookies.set({
name: 'signpost-feed',
value: JSON.stringify(signpostFeed),
path: '/',
sameSite: 'strict',
expires: new Date(Date.now() + 30 * 60 * 1000), // In 30 minutes
})
}
} catch (e) {
console.log('Middleware errored on signpost-feed cookie step', e)
}

// Person base
try {
if (
Expand All @@ -81,6 +77,7 @@ export async function userPageMiddleware(request: NextRequest) {
const p = (await person()).fields

const tickets = Number(p['settled_tickets'])
console.log("tickets too big:", tooBig(tickets))
response.cookies.set({
name: 'tickets',
value: JSON.stringify(tickets),
Expand All @@ -95,6 +92,7 @@ export async function userPageMiddleware(request: NextRequest) {
if (verificationStatus.startsWith('Eligible')) {
verifExpiration = 24 * 60 * 60 * 1000 // In 1 day
}
console.log("verification too big:", tooBig(verificationStatus))
response.cookies.set({
name: 'verification',
value: JSON.stringify({
Expand All @@ -110,6 +108,7 @@ export async function userPageMiddleware(request: NextRequest) {
if (academyCompleted) {
acadExpiration = 7 * 24 * 60 * 60 * 1000 // In 7 days
}
console.log("academy too big:", tooBig(academyCompleted))
response.cookies.set({
name: 'academy-completed',
value: JSON.stringify(academyCompleted),
Expand Down
Loading