Skip to content

Commit

Permalink
user page ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Mar 12, 2020
1 parent e92d228 commit 44c0b88
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/common/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const GlobalData = {
apiBase: ""
};

export default GlobalData;
11 changes: 9 additions & 2 deletions src/entry/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import React from "react";
import { renderToString } from "react-dom/server";
import { StaticRouter } from "react-router";
import App from "../App";
import GlobalData from "../common/global";
import { findRoute, prefetch } from "../common/route-utli";

globalThis.React = React;

interface RenderProps {
url?: string;
api_base: string;
data: Object;
}

export default async function render(props: RenderProps) {
const { url = "/" } = props;
console.log(props);
const { url = "/", api_base = "", data } = props;

GlobalData.apiBase = api_base;

let routeInfo = findRoute(url);
let state = prefetch(routeInfo);
let state = { ...data, ...(await prefetch(routeInfo)) };
const html = renderToString(
<StaticRouter location={{ pathname: url }}>
<App {...state} />
Expand Down
3 changes: 0 additions & 3 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
</head>
<body>
<div id="app">${content}</div>
<script>
// INJIECT:__INIT_STATE__
</script>
<script async src="./index.tsx"></script>
</body>
</html>
7 changes: 4 additions & 3 deletions src/pages/User/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { match } from "react-router";
import { BasePage } from "../../common/base-page";
import GlobalData from "../../common/global";
import BasePanel from "../../components/base-panel";
import DefaultLayout from "../../components/layouts/DefaultLayout";
import "./index.less";
Expand All @@ -10,9 +11,9 @@ const User: BasePage<{
}> = {
async prefetch(match: match<{ id: string }>) {
const id = match.params.id;
const { data } = await fetch(`/api/user/info/${id}`).then(res =>
res.json()
);
const { data } = await fetch(
`${GlobalData.apiBase}/api/user/info/${id}`
).then(res => res.json());
return { user: data };
},
page(props) {
Expand Down

0 comments on commit 44c0b88

Please sign in to comment.