Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 8643688

Browse files
committed
refactor(router): use parseURL instead of parse one-by-one
rm old getMainPath, getSubPath, getThridPath
1 parent aa01021 commit 8643688

File tree

11 files changed

+135
-84
lines changed

11 files changed

+135
-84
lines changed

containers/Route/logic.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect } from 'react'
2-
import { Global, buildLog, getMainPath, getSubPath } from '@utils'
2+
import { Global, buildLog, parseURL } from '@utils'
33

44
/* eslint-disable-next-line */
55
const log = buildLog('L:Route')
@@ -20,8 +20,7 @@ export const init = (_store, routeObj) => {
2020

2121
store = _store
2222
// sync init router info
23-
const mainPath = getMainPath(routeObj)
24-
const subPath = getSubPath(routeObj)
23+
const { mainPath, subPath } = parseURL(routeObj)
2524
const { query } = routeObj
2625

2726
store.markState({ mainPath, subPath, query })
@@ -35,8 +34,7 @@ export const useInit = (_store, routeObj) => {
3534
useEffect(() => {
3635
store = _store
3736
// sync init router info
38-
const mainPath = getMainPath(routeObj)
39-
const subPath = getSubPath(routeObj)
37+
const { mainPath, subPath } = parseURL(routeObj)
4038
const { query } = routeObj
4139

4240
store.markState({ mainPath, subPath, query })

pages/community.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,12 @@ import NextSeo from 'next-seo'
66
import { PAGE_SIZE, SITE_URL } from '@config'
77
import initRootStore from '@stores/init'
88

9-
import AnalysisService from '@services/Analysis'
10-
import GlobalLayout from '@containers/GlobalLayout'
11-
import ThemeWrapper from '@containers/ThemeWrapper'
12-
import MultiLanguage from '@containers/MultiLanguage'
13-
import Sidebar from '@containers/Sidebar'
14-
import Preview from '@containers/Preview'
15-
import Doraemon from '@containers/Doraemon'
16-
import Route from '@containers/Route'
17-
import Header from '@containers/Header'
18-
import CommunityBanner from '@containers/CommunityBanner'
19-
import CommunityContent from '@containers/CommunityContent'
20-
import Footer from '@containers/Footer'
21-
import ErrorBox from '@containers/ErrorBox'
22-
import ErrorPage from '@components/ErrorPage'
23-
249
import {
10+
isServerSide,
2511
getJwtToken,
2612
makeGQClient,
2713
queryStringToJSON,
28-
getMainPath,
29-
getSubPath,
14+
parseURL,
3015
akaTranslate,
3116
extractThreadFromPath,
3217
buildLog,
@@ -40,6 +25,21 @@ import {
4025
parseTheme,
4126
} from '@utils'
4227

28+
import AnalysisService from '@services/Analysis'
29+
import GlobalLayout from '@containers/GlobalLayout'
30+
import ThemeWrapper from '@containers/ThemeWrapper'
31+
import MultiLanguage from '@containers/MultiLanguage'
32+
import Sidebar from '@containers/Sidebar'
33+
import Preview from '@containers/Preview'
34+
import Doraemon from '@containers/Doraemon'
35+
import Route from '@containers/Route'
36+
import Header from '@containers/Header'
37+
import CommunityBanner from '@containers/CommunityBanner'
38+
import CommunityContent from '@containers/CommunityContent'
39+
import Footer from '@containers/Footer'
40+
import ErrorBox from '@containers/ErrorBox'
41+
import ErrorPage from '@components/ErrorPage'
42+
4343
import { P } from '@schemas'
4444

4545
/* eslint-disable-next-line */
@@ -59,10 +59,8 @@ async function fetchData(props, opt) {
5959
const { asPath } = props
6060
// schema
6161

62-
// utils: filter, tags staff
63-
const mainPath = getMainPath(props)
62+
const { mainPath, subPath: topic } = parseURL(props)
6463
const community = akaTranslate(mainPath)
65-
const topic = getSubPath(props)
6664
const thread = extractThreadFromPath(props)
6765

6866
let filter = addTopicIfNeed(
@@ -107,8 +105,9 @@ async function fetchData(props, opt) {
107105

108106
export default class CommunityPage extends React.Component {
109107
static async getInitialProps(props) {
110-
const mainPath = getMainPath(props)
111-
const subPath = getSubPath(props)
108+
if (!isServerSide()) return {}
109+
110+
const { mainPath, subPath } = parseURL(props)
112111
const thread = extractThreadFromPath(props)
113112

114113
let resp

pages/home/posts.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import {
2525
getJwtToken,
2626
makeGQClient,
2727
queryStringToJSON,
28-
getMainPath,
29-
getSubPath,
28+
parseURL,
3029
akaTranslate,
3130
extractThreadFromPath,
3231
buildLog,
@@ -60,9 +59,8 @@ async function fetchData(props, opt) {
6059
// schema
6160

6261
// utils: filter, tags staff
63-
const mainPath = getMainPath(props)
62+
const { mainPath, subPath: topic } = parseURL(props)
6463
const community = akaTranslate(mainPath)
65-
const topic = getSubPath(props)
6664
const thread = extractThreadFromPath(props)
6765

6866
let filter = addTopicIfNeed(
@@ -107,8 +105,7 @@ async function fetchData(props, opt) {
107105

108106
export default class HomePage extends React.Component {
109107
static async getInitialProps(props) {
110-
const mainPath = getMainPath(props)
111-
const subPath = getSubPath(props)
108+
const { mainPath, subPath } = parseURL(props)
112109
const thread = extractThreadFromPath(props)
113110

114111
let resp

pages/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Provider } from 'mobx-react'
33
import R from 'ramda'
44

55
import { ROUTE } from '@constant'
6-
import { buildLog, getMainPath } from '@utils'
6+
import { buildLog, parseURL, isServerSide } from '@utils'
77
import AnalysisService from '@services/Analysis'
88

99
import GlobalLayout from '@containers/GlobalLayout'
@@ -37,13 +37,9 @@ global.Intl = require('intl')
3737
*/
3838
export default class PageCommunity extends React.Component {
3939
static async getInitialProps(props) {
40-
const isServer = typeof window === 'undefined'
41-
console.log('page:index isServer: ', isServer)
40+
if (!isServerSide()) return {}
4241

43-
if (!isServer) return {}
44-
45-
const mainPath = getMainPath(props)
46-
const subPath = getMainPath(props)
42+
const { mainPath, subPath } = parseURL(props)
4743
const hideSidebar =
4844
R.contains(mainPath, [ROUTE.USER]) ||
4945
R.contains(subPath, [ROUTE.POST, ROUTE.REPO, ROUTE.VIDEO, ROUTE.JOB])

pages/job.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { TYPE, ROUTE, THREAD } from '@constant'
88
import {
99
getJwtToken,
1010
makeGQClient,
11-
getMainPath,
12-
getSubPath,
13-
getThirdPath,
11+
parseURL,
1412
nilOrEmpty,
1513
ssrAmbulance,
1614
parseTheme,
@@ -45,7 +43,7 @@ async function fetchData(props) {
4543
const userHasLogin = nilOrEmpty(token) === false
4644

4745
// schema
48-
const id = getThirdPath(props)
46+
const { thridPath: id } = parseURL(props)
4947

5048
// query data
5149
const sessionState = gqClient.request(P.sessionState)
@@ -74,17 +72,18 @@ async function fetchData(props) {
7472
export default class JobPage extends React.Component {
7573
static async getInitialProps(props) {
7674
let resp
75+
const { mainPath, subPath } = parseURL(props)
76+
7777
try {
7878
resp = await fetchData(props)
7979
} catch ({ response: { errors } }) {
8080
if (ssrAmbulance.hasLoginError(errors)) {
8181
resp = await fetchData(props, { realname: false })
8282
} else {
83-
return { statusCode: 404, target: getSubPath(props) }
83+
return { statusCode: 404, target: subPath }
8484
}
8585
}
8686

87-
const mainPath = getMainPath(props)
8887
const { sessionState, pagedComments, subscribedCommunities, job } = resp
8988

9089
return {

pages/post.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
} from '@utils'
@@ -46,7 +44,7 @@ async function fetchData(props, opt) {
4644
const userHasLogin = nilOrEmpty(token) === false
4745

4846
// schema
49-
const id = getThirdPath(props)
47+
const { thridPath: id } = parseURL(props)
5048

5149
// query data
5250
const sessionState = gqClient.request(P.sessionState)
@@ -75,22 +73,22 @@ async function fetchData(props, opt) {
7573

7674
export default class PostPage extends React.Component {
7775
static async getInitialProps(props) {
76+
const { mainPath, subPath } = parseURL(props)
7877
let resp
7978
try {
8079
resp = await fetchData(props)
8180
} catch ({ response: { errors } }) {
8281
if (ssrAmbulance.hasLoginError(errors)) {
8382
resp = await fetchData(props, { realname: false })
8483
} else {
85-
return { statusCode: 404, target: getSubPath(props) }
84+
return { statusCode: 404, target: subPath }
8685
}
8786
}
8887

89-
const mainPath = getMainPath(props)
9088
const { sessionState, post, pagedComments, subscribedCommunities } = resp
9189

9290
if (!R.contains(mainPath, R.pluck('raw', post.communities))) {
93-
return { statusCode: 404, target: getSubPath(props) }
91+
return { statusCode: 404, target: subPath }
9492
}
9593

9694
return {

pages/repo.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
} from '@utils'
@@ -45,7 +43,7 @@ async function fetchData(props, opt) {
4543
const gqClient = makeGQClient(token)
4644
const userHasLogin = nilOrEmpty(token) === false
4745

48-
const id = getThirdPath(props)
46+
const { thridPath: id } = parseURL(props)
4947

5048
const sessionState = gqClient.request(P.sessionState)
5149
const repo = gqClient.request(P.repo, { id })
@@ -72,22 +70,22 @@ async function fetchData(props, opt) {
7270

7371
export default class RepoPage extends React.Component {
7472
static async getInitialProps(props) {
73+
const { mainPath, subPath } = parseURL(props)
7574
let resp
7675
try {
7776
resp = await fetchData(props)
7877
} catch ({ response: { errors } }) {
7978
if (ssrAmbulance.hasLoginError(errors)) {
8079
resp = await fetchData(props, { realname: false })
8180
} else {
82-
return { statusCode: 404, target: getSubPath(props) }
81+
return { statusCode: 404, target: subPath }
8382
}
8483
}
8584

86-
const mainPath = getMainPath(props)
8785
const { sessionState, repo, pagedComments, subscribedCommunities } = resp
8886

8987
if (!R.contains(mainPath, R.pluck('raw', repo.communities))) {
90-
return { statusCode: 404, target: getSubPath(props) }
88+
return { statusCode: 404, target: subPath }
9189
}
9290

9391
return {

pages/video.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
getJwtToken,
1010
nilOrEmpty,
1111
makeGQClient,
12-
getMainPath,
13-
getSubPath,
14-
getThirdPath,
12+
parseURL,
1513
ssrAmbulance,
1614
parseTheme,
1715
} from '@utils'
@@ -42,7 +40,7 @@ async function fetchData(props) {
4240
const gqClient = makeGQClient(token)
4341
const userHasLogin = nilOrEmpty(token) === false
4442

45-
const id = getThirdPath(props)
43+
const { thridPath: id } = parseURL(props)
4644

4745
// query data
4846
const sessionState = gqClient.request(P.sessionState)
@@ -70,22 +68,22 @@ async function fetchData(props) {
7068

7169
export default class VideoPage extends React.Component {
7270
static async getInitialProps(props) {
71+
const { mainPath, subPath } = parseURL(props)
7372
let resp
7473
try {
7574
resp = await fetchData(props)
7675
} catch ({ response: { errors } }) {
7776
if (ssrAmbulance.hasLoginError(errors)) {
7877
resp = await fetchData(props, { realname: false })
7978
} else {
80-
return { statusCode: 404, target: getSubPath(props) }
79+
return { statusCode: 404, target: subPath }
8180
}
8281
}
8382

84-
const mainPath = getMainPath(props)
8583
const { sessionState, video, pagedComments, subscribedCommunities } = resp
8684

8785
if (!R.contains(mainPath, R.pluck('raw', video.communities))) {
88-
return { statusCode: 404, target: getSubPath(props) }
86+
return { statusCode: 404, target: subPath }
8987
}
9088

9189
return {

utils/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ export {
5454
// export { default as githubApi } from './github_api'
5555

5656
export {
57-
getMainPath,
58-
getSubPath,
59-
getThirdPath,
57+
parseURL,
6058
akaTranslate,
6159
getParameterByName,
6260
getQueryFromUrl,
@@ -82,6 +80,7 @@ export {
8280
} from './mobx_helper'
8381

8482
export {
83+
isServerSide,
8584
getJwtToken,
8685
ssrPagedSchema,
8786
ssrPagedFilter,

0 commit comments

Comments
 (0)