- {userData.name}
+ {props.userData.name}
{/* User's badges */}
- {userData.badges ? (
- userData.badges.map(
+ {props.userData.badges ? (
+ props.userData.badges.map(
(badge: {
name: string;
color: string;
@@ -206,7 +190,7 @@ function UserPage() {
$
- {userData.net_worth.toLocaleString("en-US")}
+ {props.userData.net_worth.toLocaleString("en-US")}
@@ -227,7 +211,7 @@ function UserPage() {
#
- {userData.rank.toLocaleString("en-US")}
+ {props.userData.rank.toLocaleString("en-US")}
@@ -245,7 +229,7 @@ function UserPage() {
$
- {userData.net_worth.toLocaleString("en-US")}
+ {props.userData.net_worth.toLocaleString("en-US")}
@@ -264,7 +248,7 @@ function UserPage() {
{errorCode === 20000 ? (
{`Could not load assets`}
) : (
- userData.assets.map(
+ props.userData.assets.map(
(asset: {
name: string;
count: number;
@@ -353,19 +337,22 @@ function UserPage() {
>
{/* User's Stats, left side is label, right side is value */}
Points
- {userData.points.toLocaleString("en-US")}
+ {props.userData.points.toLocaleString("en-US")}
Shares
- {userData.shares.toLocaleString("en-US")}
+ {props.userData.shares.toLocaleString("en-US")}
Trades
- {(userData.trades ?? 0).toLocaleString("en-US")}
+ {(props.userData.trades ?? 0).toLocaleString("en-US")}
Peak rank
- {(userData.peak_rank ?? 0).toLocaleString("en-US")}
+ {(props.userData.peak_rank ?? 0).toLocaleString("en-US")}
Joined
- {new Date(userData.joined ?? 0).toLocaleDateString("en-US", {
- year: "numeric",
- month: "short",
- })}
+ {new Date(props.userData.joined ?? 0).toLocaleDateString(
+ "en-US",
+ {
+ year: "numeric",
+ month: "short",
+ }
+ )}
{/* User's Favorite Emote */}
@@ -564,8 +551,32 @@ const sidebarItemVariants: Variants = {
},
};
+UserPage.getInitialProps = async (context: { query: { username: string } }) => {
+ const res = await fetch(
+ `${process.env.NEXT_PUBLIC_URL}/api/fakeUsers?u=${context.query.username}`
+ );
+ let user = await res.json();
+ if (user.error) {
+ user = { data: user };
+ }
+ return { userData: user.data };
+};
+
UserPage.getLayout = function getLayout(page: ReactElement) {
- return
{page};
+ const { userData } = page.props;
+ const metaTags = {
+ title: !userData.error
+ ? `${userData.name ?? "User 404"} - toffee`
+ : "User 404 - toffee",
+ description: !userData.error
+ ? `${userData.name}'s portfolio on toffee`
+ : "Couldn't find that user on toffee... :(",
+ image: !userData.error ? userData.avatar_url : undefined,
+ misc: {
+ "twitter:card": "summary",
+ },
+ };
+ return
{page};
};
export default UserPage;
From 635fa9d5c599bad96f04f6e3db0cacdf4b530a93 Mon Sep 17 00:00:00 2001
From: 3zachm <3zachm2@gmail.com>
Date: Thu, 26 Jan 2023 02:52:52 -0800
Subject: [PATCH 5/7] maybe fix?
---
pages/user/[username]/index.tsx | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/pages/user/[username]/index.tsx b/pages/user/[username]/index.tsx
index 7a46772..c0ee448 100644
--- a/pages/user/[username]/index.tsx
+++ b/pages/user/[username]/index.tsx
@@ -551,10 +551,18 @@ const sidebarItemVariants: Variants = {
},
};
-UserPage.getInitialProps = async (context: { query: { username: string } }) => {
- const res = await fetch(
- `${process.env.NEXT_PUBLIC_URL}/api/fakeUsers?u=${context.query.username}`
- );
+UserPage.getInitialProps = async (context: {
+ query: { username: string };
+ req: any;
+}) => {
+ let host = process.env.NEXT_PUBLIC_URL;
+ console.log(host);
+ if (context.req) {
+ // if env breaks ???
+ let host = context.req.headers.host;
+ }
+ const url = new URL(`${host}/api/fakeUsers?u=${context.query.username}`);
+ const res = await fetch(url);
let user = await res.json();
if (user.error) {
user = { data: user };
From 8bb40539900fb9c55529d5e5b584d3f538efb21e Mon Sep 17 00:00:00 2001
From: 3zachm <3zachm2@gmail.com>
Date: Thu, 26 Jan 2023 03:00:10 -0800
Subject: [PATCH 6/7] nope, fix tmrw
---
pages/user/[username]/index.tsx | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/pages/user/[username]/index.tsx b/pages/user/[username]/index.tsx
index c0ee448..6d69f4c 100644
--- a/pages/user/[username]/index.tsx
+++ b/pages/user/[username]/index.tsx
@@ -555,13 +555,10 @@ UserPage.getInitialProps = async (context: {
query: { username: string };
req: any;
}) => {
- let host = process.env.NEXT_PUBLIC_URL;
- console.log(host);
- if (context.req) {
- // if env breaks ???
- let host = context.req.headers.host;
- }
- const url = new URL(`${host}/api/fakeUsers?u=${context.query.username}`);
+ // fix weird bug where host env was undefined on layout render, not direct page render
+ const url = new URL(
+ `https://invest.3zachm.dev/api/fakeUsers?u=${context.query.username}`
+ );
const res = await fetch(url);
let user = await res.json();
if (user.error) {
From e34aa1583ec787d92aa9cb519e724f06800fc664 Mon Sep 17 00:00:00 2001
From: 3zachm <3zachm2@gmail.com>
Date: Thu, 26 Jan 2023 18:54:42 -0800
Subject: [PATCH 7/7] replace initial with serverside
---
pages/user/[username]/index.tsx | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/pages/user/[username]/index.tsx b/pages/user/[username]/index.tsx
index 6d69f4c..61406e5 100644
--- a/pages/user/[username]/index.tsx
+++ b/pages/user/[username]/index.tsx
@@ -4,6 +4,7 @@ import { ReactElement, useEffect, useState } from "react";
import DashLayout from "../../../layouts/DashLayout";
import Image from "next/image";
import Loading from "../../../components/common/Loading";
+import { GetServerSideProps } from "next";
interface EmoteURLs {
"7tv": { [key: string]: string };
@@ -551,20 +552,26 @@ const sidebarItemVariants: Variants = {
},
};
-UserPage.getInitialProps = async (context: {
- query: { username: string };
- req: any;
-}) => {
- // fix weird bug where host env was undefined on layout render, not direct page render
+export const getServerSideProps: GetServerSideProps
= async (
+ context
+) => {
+ // cache, currently 30s till stale
+ context.res.setHeader(
+ "Cache-Control",
+ "public, s-maxage=45, stale-while-revalidate=30"
+ );
+ // data fetch
const url = new URL(
- `https://invest.3zachm.dev/api/fakeUsers?u=${context.query.username}`
+ `/api/fakeUsers?u=${context.query.username}`,
+ process.env.NEXT_PUBLIC_URL
);
const res = await fetch(url);
let user = await res.json();
+ // return error in user.data if user not found
if (user.error) {
user = { data: user };
}
- return { userData: user.data };
+ return { props: { userData: user.data } };
};
UserPage.getLayout = function getLayout(page: ReactElement) {
@@ -576,7 +583,7 @@ UserPage.getLayout = function getLayout(page: ReactElement) {
description: !userData.error
? `${userData.name}'s portfolio on toffee`
: "Couldn't find that user on toffee... :(",
- image: !userData.error ? userData.avatar_url : undefined,
+ imageUrl: !userData.error ? userData.avatar_url : undefined,
misc: {
"twitter:card": "summary",
},