replace initial with serverside
This commit is contained in:
parent
8bb4053990
commit
e34aa1583e
1 changed files with 15 additions and 8 deletions
|
@ -4,6 +4,7 @@ import { ReactElement, useEffect, useState } from "react";
|
||||||
import DashLayout from "../../../layouts/DashLayout";
|
import DashLayout from "../../../layouts/DashLayout";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import Loading from "../../../components/common/Loading";
|
import Loading from "../../../components/common/Loading";
|
||||||
|
import { GetServerSideProps } from "next";
|
||||||
|
|
||||||
interface EmoteURLs {
|
interface EmoteURLs {
|
||||||
"7tv": { [key: string]: string };
|
"7tv": { [key: string]: string };
|
||||||
|
@ -551,20 +552,26 @@ const sidebarItemVariants: Variants = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
UserPage.getInitialProps = async (context: {
|
export const getServerSideProps: GetServerSideProps<UserPageProps> = async (
|
||||||
query: { username: string };
|
context
|
||||||
req: any;
|
) => {
|
||||||
}) => {
|
// cache, currently 30s till stale
|
||||||
// fix weird bug where host env was undefined on layout render, not direct page render
|
context.res.setHeader(
|
||||||
|
"Cache-Control",
|
||||||
|
"public, s-maxage=45, stale-while-revalidate=30"
|
||||||
|
);
|
||||||
|
// data fetch
|
||||||
const url = new URL(
|
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);
|
const res = await fetch(url);
|
||||||
let user = await res.json();
|
let user = await res.json();
|
||||||
|
// return error in user.data if user not found
|
||||||
if (user.error) {
|
if (user.error) {
|
||||||
user = { data: user };
|
user = { data: user };
|
||||||
}
|
}
|
||||||
return { userData: user.data };
|
return { props: { userData: user.data } };
|
||||||
};
|
};
|
||||||
|
|
||||||
UserPage.getLayout = function getLayout(page: ReactElement) {
|
UserPage.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
@ -576,7 +583,7 @@ UserPage.getLayout = function getLayout(page: ReactElement) {
|
||||||
description: !userData.error
|
description: !userData.error
|
||||||
? `${userData.name}'s portfolio on toffee`
|
? `${userData.name}'s portfolio on toffee`
|
||||||
: "Couldn't find that user on toffee... :(",
|
: "Couldn't find that user on toffee... :(",
|
||||||
image: !userData.error ? userData.avatar_url : undefined,
|
imageUrl: !userData.error ? userData.avatar_url : undefined,
|
||||||
misc: {
|
misc: {
|
||||||
"twitter:card": "summary",
|
"twitter:card": "summary",
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue