Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitd31ddfd

Browse files
committed
refactor(SSR): use getServerSideProps for more page
1 parent2eac7ca commitd31ddfd

File tree

11 files changed

+113
-173
lines changed

11 files changed

+113
-173
lines changed

‎src/pages/communities/index.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ export async function getServerSideProps(props) {
8282
theme:{
8383
curTheme:parseTheme(sessionState),
8484
},
85-
route:{
86-
mainPath:ROUTE.COMMUNITIES,
87-
subPath:category,
88-
},
8985
account:{
9086
user:sessionState.user||{},
9187
isValidSession:sessionState.isValid,

‎src/pages/communities/new.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ export async function getServerSideProps(props) {
8181
theme:{
8282
curTheme:parseTheme(sessionState),
8383
},
84-
route:{
85-
mainPath:ROUTE.COMMUNITIES,
86-
subPath:category,
87-
},
8884
account:{
8985
user:sessionState.user||{},
9086
isValidSession:sessionState.isValid,
@@ -104,8 +100,8 @@ function NewCommunityPage(props) {
104100

105101
constseoConfig={
106102
url:`${SITE_URL}/communities`,
107-
title:'coderplanets |社区索引',
108-
description:'coderplanets | 社区索引',
103+
title:'建立新社区 |coderplanets',
104+
description:'建立新社区',
109105
}
110106

111107
return(

‎src/pages/cool-guide.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ export async function getServerSideProps(props) {
4848
theme:{
4949
curTheme:parseTheme(sessionState),
5050
},
51-
route:{
52-
mainPath:ROUTE.RECIPES,
53-
subPath:'',
54-
},
5551
account:{
5652
user:sessionState.user||{},
5753
isValidSession:sessionState.isValid,
@@ -75,7 +71,7 @@ const CoolGuidePage = props => {
7571

7672
return(
7773
<Providerstore={store}>
78-
<GlobalLayoutpage={ROUTE.MEETUPS}seoConfig={seoConfig}noSidebar>
74+
<GlobalLayoutpage={ROUTE.COOL_GUIDE}seoConfig={seoConfig}noSidebar>
7975
<CoolGuideContent/>
8076
</GlobalLayout>
8177
</Provider>

‎src/pages/have-a-drink.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export async function getServerSideProps(props) {
4747
theme:{
4848
curTheme:parseTheme(sessionState),
4949
},
50-
route:{
51-
mainPath:ROUTE.RECIPES,
52-
subPath:'',
53-
},
5450
account:{
5551
user:sessionState.user||{},
5652
isValidSession:sessionState.isValid,

‎src/pages/interview.js‎

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import { merge } from 'ramda'
55
import{SITE_URL}from'@/config'
66
import{ROUTE}from'@/constant'
77

8-
import{
9-
getJwtToken,
10-
makeGQClient,
11-
parseURL,
12-
ssrAmbulance,
13-
parseTheme,
14-
}from'@/utils'
15-
importinitRootStorefrom'@/stores/init'
8+
import{getJwtToken,makeGQClient,ssrAmbulance,parseTheme}from'@/utils'
9+
10+
import{useStore}from'@/stores/init2'
1611

1712
importGlobalLayoutfrom'@/containers/GlobalLayout'
1813
importInterviewContentfrom'@/containers/content/InterviewContent'
@@ -40,66 +35,54 @@ async function fetchData(props, opt) {
4035
}
4136
}
4237

43-
exportdefaultclassInterviewPageextendsReact.Component{
44-
staticasyncgetInitialProps(props){
45-
const{ mainPath, subPath}=parseURL(props)
46-
letresp
47-
try{
48-
resp=awaitfetchData(props)
49-
}catch({response:{ errors}}){
50-
if(ssrAmbulance.hasLoginError(errors)){
51-
resp=awaitfetchData(props,{realname:false})
52-
}else{
53-
return{statusCode:404,target:subPath}
54-
}
55-
}
56-
57-
const{ sessionState, subscribedCommunities}=resp
58-
59-
return{
60-
theme:{
61-
curTheme:parseTheme(sessionState),
62-
},
63-
account:{
64-
user:sessionState.user||{},
65-
isValidSession:sessionState.isValid,
66-
userSubscribedCommunities:subscribedCommunities,
67-
},
68-
route:{ mainPath,subPath:ROUTE.INTERVIEW},
38+
exportasyncfunctiongetServerSideProps(props){
39+
letresp
40+
try{
41+
resp=awaitfetchData(props)
42+
}catch({response:{ errors}}){
43+
if(ssrAmbulance.hasLoginError(errors)){
44+
resp=awaitfetchData(props,{realname:false})
6945
}
7046
}
7147

72-
constructor(props){
73-
super(props)
74-
conststore=props.statusCode
75-
?initRootStore()
76-
:initRootStore({ ...props})
77-
78-
this.store=store
48+
const{ sessionState, subscribedCommunities}=resp
49+
constinitProps={
50+
theme:{
51+
curTheme:parseTheme(sessionState),
52+
},
53+
account:{
54+
user:sessionState.user||{},
55+
isValidSession:sessionState.isValid,
56+
userSubscribedCommunities:subscribedCommunities,
57+
},
7958
}
8059

81-
render(){
82-
const{ statusCode, target}=this.props
60+
return{
61+
props:{errorCode:null,namespacesRequired:['general'], ...initProps},
62+
}
63+
}
8364

84-
constseoConfig={
85-
url:`${SITE_URL}/${ROUTE.INTERVIEW}`,
86-
title:'coderplanets 社区',
87-
description:'最性感的开发者社区',
88-
}
65+
constInterviewPage=props=>{
66+
conststore=useStore(props)
8967

90-
return(
91-
<Providerstore={this.store}>
92-
<GlobalLayout
93-
noSidebar
94-
metric="default"
95-
page={ROUTE.INTERVIEW}
96-
seoConfig={seoConfig}
97-
errorCode={statusCode}
98-
errorPath={target}
99-
>
100-
<InterviewContent/>
101-
</GlobalLayout>
102-
</Provider>
103-
)
68+
constseoConfig={
69+
url:`${SITE_URL}/${ROUTE.INTERVIEW}`,
70+
title:'面经 | coderplanets',
71+
description:'面试题库,面试经验分享交流',
10472
}
73+
74+
return(
75+
<Providerstore={store}>
76+
<GlobalLayout
77+
noSidebar
78+
metric="default"
79+
page={ROUTE.INTERVIEW}
80+
seoConfig={seoConfig}
81+
>
82+
<InterviewContent/>
83+
</GlobalLayout>
84+
</Provider>
85+
)
10586
}
87+
88+
exportdefaultInterviewPage

‎src/pages/meetups.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ export async function getServerSideProps(props) {
4848
theme:{
4949
curTheme:parseTheme(sessionState),
5050
},
51-
route:{
52-
mainPath:ROUTE.RECIPES,
53-
subPath:'',
54-
},
5551
account:{
5652
user:sessionState.user||{},
5753
isValidSession:sessionState.isValid,
@@ -68,7 +64,7 @@ const MeetupsPage = props => {
6864
conststore=useStore(props)
6965

7066
constseoConfig={
71-
url:`${SITE_URL}/works`,
67+
url:`${SITE_URL}/${ROUTE.MEETUPS}`,
7268
title:'线下活动 | coderplanets',
7369
description:'线下分享,行业交流',
7470
}

‎src/pages/recipes.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export async function getServerSideProps(props) {
4747
theme:{
4848
curTheme:parseTheme(sessionState),
4949
},
50-
route:{
51-
mainPath:ROUTE.RECIPES,
52-
subPath:'',
53-
},
5450
account:{
5551
user:sessionState.user||{},
5652
isValidSession:sessionState.isValid,
@@ -67,14 +63,14 @@ const RecipesPage = props => {
6763
conststore=useStore(props)
6864

6965
constseoConfig={
70-
url:`${SITE_URL}/works`,
66+
url:`${SITE_URL}/${ROUTE.RECIPES}`,
7167
title:'代码片段 | coderplanets',
7268
description:'各种语言框架的代码片段和速查手册',
7369
}
7470

7571
return(
7672
<Providerstore={store}>
77-
<GlobalLayoutpage={ROUTE.WORKS}seoConfig={seoConfig}noSidebar>
73+
<GlobalLayoutpage={ROUTE.RECIPES}seoConfig={seoConfig}noSidebar>
7874
<RecipesContent/>
7975
</GlobalLayout>
8076
</Provider>

‎src/pages/sponsor.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export async function getServerSideProps(props) {
4747
theme:{
4848
curTheme:parseTheme(sessionState),
4949
},
50-
route:{
51-
mainPath:ROUTE.RECIPES,
52-
subPath:'',
53-
},
5450
account:{
5551
user:sessionState.user||{},
5652
isValidSession:sessionState.isValid,
@@ -67,7 +63,7 @@ const SponsorPage = props => {
6763
conststore=useStore(props)
6864

6965
constseoConfig={
70-
url:`${SITE_URL}/works`,
66+
url:`${SITE_URL}/${ROUTE.SPONSOR}`,
7167
title:'赞助与广告 | coderplanets',
7268
description:'赞助与广告',
7369
}

‎src/pages/trending.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export async function getServerSideProps(props) {
4747
theme:{
4848
curTheme:parseTheme(sessionState),
4949
},
50-
route:{
51-
mainPath:ROUTE.TRENDING,
52-
subPath:'',
53-
},
5450
account:{
5551
user:sessionState.user||{},
5652
isValidSession:sessionState.isValid,
@@ -67,14 +63,14 @@ const TrendingPage = props => {
6763
conststore=useStore(props)
6864

6965
constseoConfig={
70-
url:`${SITE_URL}/works`,
66+
url:`${SITE_URL}/${ROUTE.TRENDING}`,
7167
title:'热点 | coderplanets',
7268
description:'站内外热门讨论',
7369
}
7470

7571
return(
7672
<Providerstore={store}>
77-
<GlobalLayoutpage={ROUTE.WORKS}seoConfig={seoConfig}noSidebar>
73+
<GlobalLayoutpage={ROUTE.TRENDING}seoConfig={seoConfig}noSidebar>
7874
<TrendingContent/>
7975
</GlobalLayout>
8076
</Provider>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp