Skip to content

Commit 909f61a

Browse files
committed
fix(web): allow builds without Graphite; harden streaks/emoji flows
Use a noop StatsD client when GRAPHITE_HOST is unset to prevent build/runtime failures; make streaks and emoji APIs/pages robust by fixing error handling and ensuring JSON-serializable defaults.
1 parent 43308d7 commit 909f61a

File tree

7 files changed

+54
-31
lines changed

7 files changed

+54
-31
lines changed

apps/web/metrics.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import StatsD from "node-statsd";
2-
import { config } from "dotenv";
32

4-
const environment = process.env.NODE_ENV;
3+
const environment = process.env.NODE_ENV || "development";
54
const graphite = process.env.GRAPHITE_HOST;
65

7-
if (graphite == null) {
8-
throw new Error("Graphite host not configured");
9-
}
10-
11-
const options = {
12-
host: graphite,
13-
port: 8125,
14-
prefix: `${environment}.scrapbook.`
6+
// Provide a no-op metrics client when Graphite is not configured,
7+
// so builds and local dev do not fail.
8+
const noopMetrics = {
9+
timing: () => {},
10+
increment: () => {}
1511
};
1612

17-
const metrics = new StatsD(options);
13+
let metricsClient;
14+
if (graphite && graphite.length > 0) {
15+
metricsClient = new StatsD({
16+
host: graphite,
17+
port: 8125,
18+
prefix: `${environment}.scrapbook.`
19+
});
20+
} else {
21+
metricsClient = noopMetrics;
22+
}
1823

19-
export default metrics;
24+
export default metricsClient;

apps/web/pages/api/clubs/[slug]/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ export const getPosts = async (club, max = null) => {
5353
}
5454
})
5555

56-
if (!allUpdates)
56+
if (!allUpdates) {
5757
// console.error('Could not fetch posts');
58+
}
5859

5960
const users = await getRawUsers()
6061

apps/web/pages/api/r/[emoji].js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ export const getPosts = async (emoji, maxRecords = 256, where = {}) => {
2525
.map(p => transformPost(p))
2626

2727
} catch (err) {
28-
if (!allUpdates)
29-
// console.error('Could not fetch posts');
28+
// console.error(err)
3029
throw new Error(err);
3130
}
3231
}

apps/web/pages/api/streaks.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ export const getUserStreaks = async () => {
99
}
1010
},
1111
select: {
12+
id: true,
1213
username: true,
1314
slackID: true,
1415
avatar: true,
1516
streakCount: true,
1617
maxStreaks: true
1718
}
1819
})
19-
return streaks;
20+
// Ensure values are JSON-serializable (no undefined)
21+
return streaks.map(user => ({
22+
id: user.id ?? null,
23+
username: user.username ?? null,
24+
slackID: user.slackID ?? null,
25+
avatar: user.avatar ?? null,
26+
streakCount: user.streakCount ?? 0,
27+
maxStreaks: user.maxStreaks ?? 0
28+
}));
2029
}
2130
catch (err) {
2231
throw Error(err)
@@ -26,9 +35,9 @@ export const getUserStreaks = async () => {
2635

2736
export default async (req, res) => {
2837
try {
29-
const streaks = getUserStreaks();
38+
const streaks = await getUserStreaks();
3039
res.json(streaks);
31-
} catch {
32-
res.status(404).json(streaks);
40+
} catch (err) {
41+
res.status(404).json([]);
3342
}
3443
}

apps/web/pages/api/users/[username]/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ export const getPosts = async (user, max = null) => {
5151
}
5252
})
5353

54-
if (!allUpdates)
54+
if (!allUpdates) {
5555
// console.error('Could not fetch posts');
56+
}
5657

5758
return allUpdates.map(p => transformPost(p))
5859
}
@@ -67,8 +68,9 @@ export const getMentions = async user => {
6768
}
6869
})
6970

70-
if (!allUpdates)
71+
if (!allUpdates) {
7172
// console.error('Could not fetch posts');
73+
}
7274

7375
const mentions = allUpdates
7476
.map(p => {

apps/web/pages/r/[emoji].js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const Page = ({ status, emoji, related = [], posts = [], css }) => {
152152
} else if (emoji) {
153153
return (
154154
<Feed
155-
initialData={SuperJSON.parse(posts)}
155+
initialData={typeof posts === 'string' ? SuperJSON.parse(posts) : (posts || [])}
156156
src={`/api/r/${emoji.name}`}
157157
footer={related.length > 1 && <Footer reactions={related} />}
158158
>
@@ -237,6 +237,6 @@ export const getStaticProps = async ({ params }) => {
237237
return { props: { emoji, posts: SuperJSON.stringify(posts), related, css }, revalidate: 1 }
238238
} catch (error) {
239239
// console.error(error)
240-
return { props: { emoji: { name }, css }, revalidate: 1 }
240+
return { props: { emoji: { name }, posts: SuperJSON.stringify([]), css }, revalidate: 1 }
241241
}
242242
}

apps/web/pages/streaks.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Meta from '@hackclub/meta'
33
import Mention from '../components/mention'
44
import { orderBy } from 'lodash-es'
55

6-
const StreaksPage = ({ users }) => {
6+
const StreaksPage = ({ users = [] }) => {
77
return (
88
<>
99
<Meta
@@ -112,12 +112,19 @@ const StreaksPage = ({ users }) => {
112112
export default StreaksPage
113113

114114
export const getStaticProps = async () => {
115-
const streaks = require('./api/streaks')
116-
const users = await streaks.getUserStreaks()
117-
return {
118-
props: {
119-
users
120-
},
121-
revalidate: 10
115+
try {
116+
const streaks = require('./api/streaks')
117+
const users = await streaks.getUserStreaks()
118+
return {
119+
props: {
120+
users: users ?? []
121+
},
122+
revalidate: 10
123+
}
124+
} catch (err) {
125+
return {
126+
props: { users: [] },
127+
revalidate: 10
128+
}
122129
}
123130
}

0 commit comments

Comments
 (0)