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

Commit 9bcfc41

Browse files
committed
refactor(router): move onClient to ssr_helper & rename for convention
rename to isClientSide
1 parent 9eada2e commit 9bcfc41

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

containers/Route/store.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import R from 'ramda'
88
// import Router from 'next/router'
99

1010
import { PAGE_SIZE } from '@config'
11-
import { Global, onClient, markStates, buildLog, serializeQuery } from '@utils'
11+
import {
12+
Global,
13+
isClientSide,
14+
markStates,
15+
buildLog,
16+
serializeQuery,
17+
} from '@utils'
1218

1319
/* eslint-disable-next-line */
1420
const log = buildLog('S:RouteStore')
@@ -42,7 +48,7 @@ const RouteStore = t
4248
// TODO: if current url is subdomain, then we should
4349
// reload to that page directly
4450
markRoute(query) {
45-
if (!onClient) return false
51+
if (!isClientSide) return false
4652
const { mainPath, subPath, page } = query
4753
query = R.pickBy(v => !R.isEmpty(v), query)
4854

pages/community.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async function fetchData(props, opt) {
5959
const { asPath } = props
6060
// schema
6161

62-
const { mainPath, subPath: topic } = parseURL(props)
63-
const community = akaTranslate(mainPath)
62+
const { communityPath, threadPath: topic } = parseURL(props)
63+
const community = akaTranslate(communityPath)
6464
const thread = extractThreadFromPath(props)
6565

6666
let filter = addTopicIfNeed(
@@ -105,9 +105,9 @@ async function fetchData(props, opt) {
105105

106106
export default class CommunityPage extends React.Component {
107107
static async getInitialProps(props) {
108-
if (!isServerSide()) return {}
108+
if (!isServerSide) return {}
109109

110-
const { mainPath, subPath } = parseURL(props)
110+
const { communityPath, threadPath } = parseURL(props)
111111
const thread = extractThreadFromPath(props)
112112

113113
let resp
@@ -119,7 +119,7 @@ export default class CommunityPage extends React.Component {
119119
} else {
120120
return {
121121
statusCode: 404,
122-
target: mainPath,
122+
target: communityPath,
123123
viewing: { community: {} },
124124
route: {},
125125
}
@@ -156,7 +156,12 @@ export default class CommunityPage extends React.Component {
156156
repo: {},
157157
user: {},
158158
},
159-
route: { mainPath: community.raw, subPath },
159+
route: {
160+
communityPath: community.raw,
161+
mainPath: community.raw,
162+
threadPath,
163+
subPath: threadPath,
164+
},
160165
tagsBar: { tags: partialTags },
161166
},
162167
contentsThread
@@ -179,7 +184,7 @@ export default class CommunityPage extends React.Component {
179184
viewing: { community },
180185
route,
181186
} = this.props
182-
const { mainPath, subPath } = route
187+
const { communityPath, threadPath } = route
183188

184189
const seoTitle =
185190
community.raw === 'home'
@@ -200,7 +205,7 @@ export default class CommunityPage extends React.Component {
200205
<React.Fragment>
201206
<NextSeo
202207
config={{
203-
url: `${SITE_URL}/${mainPath}/${subPath}`,
208+
url: `${SITE_URL}/${communityPath}/${threadPath}`,
204209
title: seoTitle,
205210
description: `${community.desc}`,
206211
}}

utils/functions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { nilOrEmpty } from './validator'
99
/* eslint-disable */
1010
// TODO: document ?
1111
export const Global = typeof window !== 'undefined' ? window : global
12-
export const onClient = typeof window !== 'undefined' ? true : false
1312
/* eslint-enable */
1413

1514
// see https://github.com/ramda/ramda/issues/1361

utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export {
1313
mapKeys,
1414
getRandomInt,
1515
Global,
16-
onClient,
1716
cutFrom,
1817
prettyNum,
1918
numberWithCommas,
@@ -81,6 +80,7 @@ export {
8180

8281
export {
8382
isServerSide,
83+
isClientSide,
8484
getJwtToken,
8585
ssrPagedSchema,
8686
ssrPagedFilter,

utils/route_helper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const getThirdPath = args => {
6161
*/
6262
const parseSubDomain = args => {
6363
let communityPath = ''
64-
if (isServerSide()) {
64+
if (isServerSide) {
6565
// on server side
6666
const { subdomains } = args.req
6767
console.log('subdomains: ', subdomains)
@@ -94,20 +94,24 @@ export const parseURL = args => {
9494
let subPath = ''
9595
let thridPath = ''
9696
let communityPath = parseSubDomain(args)
97+
let threadPath = ''
9798

9899
if (communityPath === '') {
99100
mainPath = getMainPath(args)
100101
subPath = getSubPath(args)
101102
thridPath = getThirdPath(args)
102103
communityPath = mainPath
104+
threadPath = subPath
103105
} else {
104106
mainPath = communityPath
105107
subPath = getMainPath(args)
106108
thridPath = getSubPath(args)
109+
threadPath = subPath
107110
}
108111

109112
return {
110113
communityPath,
114+
threadPath,
111115
mainPath,
112116
subPath,
113117
thridPath,

utils/ssr_helper.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import BStore from './bstore'
88
/*
99
* check if current is on server side
1010
*/
11-
export const isServerSide = () => {
12-
const isOnserver = typeof window === 'undefined'
13-
return isOnserver
14-
}
11+
export const isServerSide = typeof window === 'undefined'
12+
export const isClientSide = !isServerSide
1513

1614
// get jwt from cookie or localStorage
1715
// props has to be getInitialProps's arg
1816
export const getJwtToken = props => {
19-
if (isServerSide()) return BStore.cookie.from_req(props.req, 'jwtToken')
17+
if (isServerSide) return BStore.cookie.from_req(props.req, 'jwtToken')
2018

2119
return BStore.get('token')
2220
}

0 commit comments

Comments
 (0)