render posts server side

This commit is contained in:
Anton 2022-03-15 22:49:41 -04:00
parent d75819a02a
commit 79a8f498c5
4 changed files with 58 additions and 2 deletions

View file

@ -12,9 +12,11 @@
"@fec/remark-a11y-emoji": "^3.1.0", "@fec/remark-a11y-emoji": "^3.1.0",
"@geist-ui/core": "^2.3.5", "@geist-ui/core": "^2.3.5",
"@geist-ui/icons": "^1.0.1", "@geist-ui/icons": "^1.0.1",
"@types/cookie": "^0.4.1",
"@types/js-cookie": "^3.0.1", "@types/js-cookie": "^3.0.1",
"client-zip": "^2.0.0", "client-zip": "^2.0.0",
"comlink": "^4.3.1", "comlink": "^4.3.1",
"cookie": "^0.4.2",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",
"js-cookie": "^3.0.1", "js-cookie": "^3.0.1",
"next": "12.1.0", "next": "12.1.0",

View file

@ -13,6 +13,12 @@ export type ThemeProps = {
changeTheme: () => void changeTheme: () => void
} }
export type PostProps = {
renderedPost: any | null, // Still don't have an official data type for posts
theme: "light" | "dark" | string,
changeTheme: () => void
}
type AppProps<P = any> = { type AppProps<P = any> = {
pageProps: P; pageProps: P;
} & Omit<NextAppProps<P>, "pageProps">; } & Omit<NextAppProps<P>, "pageProps">;

View file

@ -6,13 +6,16 @@ import { useEffect, useState } from "react";
import Document from '../../components/document' import Document from '../../components/document'
import Header from "../../components/header"; import Header from "../../components/header";
import VisibilityBadge from "../../components/visibility-badge"; import VisibilityBadge from "../../components/visibility-badge";
import { ThemeProps } from "../_app"; import { PostProps } from "../_app";
import PageSeo from "components/page-seo"; import PageSeo from "components/page-seo";
import Head from "next/head"; import Head from "next/head";
import styles from './styles.module.css'; import styles from './styles.module.css';
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import cookie from "cookie";
import { GetServerSideProps } from "next";
const Post = ({ theme, changeTheme }: ThemeProps) => {
const Post = ({renderedPost, theme, changeTheme}: PostProps) => {
const [post, setPost] = useState<any>() const [post, setPost] = useState<any>()
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
@ -21,6 +24,14 @@ const Post = ({ theme, changeTheme }: ThemeProps) => {
useEffect(() => { useEffect(() => {
async function fetchPost() { async function fetchPost() {
setIsLoading(true); setIsLoading(true);
if (renderedPost) {
console.log('Using Server Side Post');
setPost(renderedPost)
setIsLoading(false)
return;
}
if (router.query.id) { if (router.query.id) {
const post = await fetch(`/server-api/posts/${router.query.id}`, { const post = await fetch(`/server-api/posts/${router.query.id}`, {
method: "GET", method: "GET",
@ -108,4 +119,31 @@ const Post = ({ theme, changeTheme }: ThemeProps) => {
) )
} }
export const getServerSideProps: GetServerSideProps = async (context) => {
const headers = context.req.headers;
const host = headers.host;
const driftToken = cookie.parse(headers.cookie || '')[`drift-token`];
let post;
if (context.query.id) {
post = await fetch('http://' + host + `/server-api/posts/${context.query.id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${driftToken}`
}
}).then(res => res.json());
console.log(post);
}
return {
props: {
renderedPost: post
}
}
}
export default Post export default Post

View file

@ -161,6 +161,11 @@
resolved "https://registry.yarnpkg.com/@rushstack/eslint-patch/-/eslint-patch-1.1.0.tgz#7f698254aadf921e48dda8c0a6b304026b8a9323" resolved "https://registry.yarnpkg.com/@rushstack/eslint-patch/-/eslint-patch-1.1.0.tgz#7f698254aadf921e48dda8c0a6b304026b8a9323"
integrity sha512-JLo+Y592QzIE+q7Dl2pMUtt4q8SKYI5jDrZxrozEQxnGVOyYE+GWK9eLkwTaeN9DDctlaRAQ3TBmzZ1qdLE30A== integrity sha512-JLo+Y592QzIE+q7Dl2pMUtt4q8SKYI5jDrZxrozEQxnGVOyYE+GWK9eLkwTaeN9DDctlaRAQ3TBmzZ1qdLE30A==
"@types/cookie@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.1.tgz#bfd02c1f2224567676c1545199f87c3a861d878d"
integrity sha512-XW/Aa8APYr6jSVVA1y/DEIZX0/GMKLEVekNG727R8cs56ahETkRAy/3DR7+fJyh7oUgGwNQaRfXCun0+KbWY7Q==
"@types/debug@^4.0.0": "@types/debug@^4.0.0":
version "4.1.7" version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82" resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82"
@ -532,6 +537,11 @@ concat-map@0.0.1:
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
cookie@^0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
core-js-pure@^3.20.2: core-js-pure@^3.20.2:
version "3.21.1" version "3.21.1"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.21.1.tgz#8c4d1e78839f5f46208de7230cebfb72bc3bdb51" resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.21.1.tgz#8c4d1e78839f5f46208de7230cebfb72bc3bdb51"