Skip to content

Commit 7020947

Browse files
committed
feat: code review & changelog
1 parent 107ce3c commit 7020947

File tree

9 files changed

+77
-108
lines changed

9 files changed

+77
-108
lines changed

docs/更新日志.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ abbrlink: 179nqpxt
66

77
`create-react-doc` 严格遵循 [Semantic Versioning 2.0.0](http://semver.org/lang/zh-CN/) 语义化版本规范。
88

9+
### 1.8.0
10+
11+
`2022-01-15`
12+
13+
- **Feature**
14+
15+
- 🚀 支持 SSR 首屏直出方案(基于 gp-pages 服务)以避免预渲染带来的二次刷新产生页面抖动的问题。[issue](https://github.com/MuYunyun/create-react-doc/issues/103)
16+
- 🚀 新增 crd-client-utils 包以收录公用方法。
17+
918
### 1.7.0
1019

1120
`2022-01-02`

packages/crd-client-utils/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import { useLayoutEffect, useEffect } from 'react'
2+
13
const ifDev = env === 'dev'
24
const ifProd = env === 'prod'
35
const ifPrerender = window.__PRERENDER_INJECTED && window.__PRERENDER_INJECTED.prerender
46

7+
const useEnhancedEffect = typeof window !== 'undefined'
8+
? useLayoutEffect
9+
: useEffect
10+
511
export {
612
ifDev,
713
ifProd,
8-
ifPrerender
14+
ifPrerender,
15+
useEnhancedEffect
916
}

packages/crd-scripts/src/web/Router.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ const RoutersContainer = withRouter(({ ...props }) => {
6565
)
6666
})
6767

68-
export default function RouterRoot({
69-
pointRender
70-
}) {
68+
export default function RouterRoot() {
7169
return (
7270
<BrowserRouter>
73-
<RoutersContainer pointRender={pointRender} />
71+
<RoutersContainer />
7472
</BrowserRouter>
7573
)
7674
}

packages/crd-seed/component/Affix/affix.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import { useState, useLayoutEffect, useRef } from 'react'
21
import { useState, useEffect, useRef } from 'react'
32
import { throttle } from './utils'
43

@@ -23,15 +22,29 @@ const Affix = ({
2322
// 是否是绝对布局模式
2423
const fixedRef = useRef(false)
2524
const [fixed, setFixed] = useState(fixedRef.current)
26-
// useLayoutEffect(() => {
25+
2726
useEffect(() => {
2827
widthRef.current = width
2928
}, [width])
29+
30+
useEffect(() => {
31+
// 在子节点移开父节点后保持原来占位
32+
setWrapperDimension()
33+
}, [fixed, width])
34+
35+
useEffect(() => {
36+
if (target) scrollElm = target()
37+
scrollElm.addEventListener('scroll', scroll)
38+
return () => {
39+
if (target) scrollElm = target()
40+
scrollElm.removeEventListener('scroll', scroll)
41+
}
42+
}, [offsetTop, offsetBottom])
43+
3044
const validValue = (value) => {
3145
return typeof value === 'number'
3246
}
3347
const setWrapperDimension = () => {
34-
// eslint-disable-next-line no-shadow
3548
const { width: wrapperRefWidth, height: wrapperRefHeight } = wrapperRef.current
3649
? wrapperRef.current.getBoundingClientRect()
3750
: {}
@@ -100,22 +113,6 @@ const Affix = ({
100113

101114
const scroll = throttle(handleScroll, 20)
102115

103-
// useLayoutEffect(() => {
104-
useEffect(() => {
105-
// 在子节点移开父节点后保持原来占位
106-
setWrapperDimension()
107-
}, [fixed, width])
108-
109-
// useLayoutEffect(() => {
110-
useEffect(() => {
111-
if (target) scrollElm = target()
112-
scrollElm.addEventListener('scroll', scroll)
113-
return () => {
114-
if (target) scrollElm = target()
115-
scrollElm.removeEventListener('scroll', scroll)
116-
}
117-
}, [offsetTop, offsetBottom])
118-
119116
return (
120117
<div ref={placeholderRef} style={style} className={className}>
121118
<div

packages/crd-seed/component/Menu/SubMenu.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// import { useState, useRef, useLayoutEffect, Fragment, Children, cloneElement } from 'react'
2-
import { useState, useRef, useEffect, Fragment, Children, cloneElement } from 'react'
1+
import { useState, useRef, Fragment, Children, cloneElement } from 'react'
32
import cx from 'classnames'
3+
import { useEnhancedEffect } from 'crd-client-utils'
44
import Transition from './transition'
55
import { getMenuStyle } from './util'
66
import { useMenuContext } from './context'
@@ -42,9 +42,8 @@ function SubMenu({
4242
const [getParentMenuHover, setParentMenuHover] = useCurrent(false)
4343

4444
const gapDistance = 4
45-
/** 使用 useLayoutEffect 可以避免 useEffect 产生可见的位移痕迹 */
46-
// useLayoutEffect(() => {
47-
useEffect(() => {
45+
46+
useEnhancedEffect(() => {
4847
if (popupSubMenu.current && curSubmenu.current) {
4948
popupSubMenu.current.style.left = `${curSubmenu.current.getBoundingClientRect().right +
5049
gapDistance}px`

packages/crd-seed/component/Menu/transition.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import { useLayoutEffect, useRef, useCallback } from 'react'
21
import { useEffect, useRef, useCallback } from 'react'
32
import styles from './style/index.less'
43

@@ -60,7 +59,6 @@ export default function Transition({
6059
}, [afterLeave])
6160

6261
const triggerChange = useCallback(
63-
// eslint-disable-next-line no-shadow
6462
(isShow) => {
6563
clearTimeout(timer.current.enterTimer)
6664
clearTimeout(timer.current.leaveTimer)
@@ -75,7 +73,6 @@ export default function Transition({
7573
[beforeLeave, enter, leave]
7674
)
7775

78-
// useLayoutEffect(() => {
7976
useEffect(() => {
8077
if (!mounted.current) {
8178
mounted.current = true

packages/crd-seed/layout/index.js

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ function BasicLayout({
2222
routeData,
2323
menuSource,
2424
indexProps,
25-
// render area
26-
pointRender
2725
}) {
2826
const { pathname } = location
2927
const { user, repo, branch = 'main', language = 'en', menuOpenKeys } = DOCSCONFIG || {}
3028
const [inlineCollapsed, setInlineCollapsed] = useState(true)
3129
const [selectedKey, setSelectedKey] = useState('')
32-
const [monted, setMounted] = useState(false)
33-
const [routeChanged, setRoutedChanged] = useState(false)
3430
const curOpenKeys = getOpenSubMenuKeys({
3531
pathname,
3632
menuSource,
@@ -64,17 +60,6 @@ function BasicLayout({
6460
setSelectedKey(newPathName || defaultPath)
6561
}, location.pathname)
6662

67-
useEffect(() => {
68-
setMounted(true)
69-
}, [])
70-
71-
useEffect(() => {
72-
const { pathname } = location
73-
if (monted) {
74-
setRoutedChanged(true)
75-
}
76-
}, location.pathname)
77-
7863
const scrollToTop = () => {
7964
document.body.scrollTop = 0
8065
document.documentElement.scrollTop = 0
@@ -245,7 +230,6 @@ function BasicLayout({
245230
)
246231
}
247232

248-
// Target: The first screen not to do renderContent logic. The second screen do.
249233
const renderContent = () => {
250234
return (
251235
<div
@@ -279,38 +263,26 @@ function BasicLayout({
279263
)
280264
}
281265

282-
console.log('routeChanged', routeChanged)
283266
return (
284-
<>
285-
{
286-
// pointRender === 'menu' && !routeChanged
287-
// prod render
288-
// ? renderMenuContainer()
289-
// pre & dev render
290-
// : <div className={styles.wrapper}>
291-
<div className={styles.wrapper}>
292-
<Header
293-
logo={logo}
294-
href={ifAddPrefix ? `/${repo}` : `/`}
295-
location={location}
296-
indexProps={indexProps}
297-
menuSource={menuSource}
298-
/>
299-
<div
300-
className={cx(styles.wrapperContent, {
301-
[styles.wrapperMobile]: isMobile,
302-
})}
303-
>
304-
{renderPageHeader()}
305-
{/* <div id="menuPosition"> */}
306-
{renderMenuContainer()}
307-
{/* </div> */}
308-
{renderContent()}
309-
<Footer inlineCollapsed={inlineCollapsed} />
310-
</div>
311-
</div>
312-
}
313-
</>
267+
<div className={styles.wrapper}>
268+
<Header
269+
logo={logo}
270+
href={ifAddPrefix ? `/${repo}` : `/`}
271+
location={location}
272+
indexProps={indexProps}
273+
menuSource={menuSource}
274+
/>
275+
<div
276+
className={cx(styles.wrapperContent, {
277+
[styles.wrapperMobile]: isMobile,
278+
})}
279+
>
280+
{renderPageHeader()}
281+
{renderMenuContainer()}
282+
{renderContent()}
283+
<Footer inlineCollapsed={inlineCollapsed} />
284+
</div>
285+
</div>
314286
)
315287
}
316288

packages/crd-theme/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ import Markdown from './markdown'
22
import './index.less'
33

44
export default function (Lazyload, props) {
5-
// const LoadableComponent = Lazyload({
6-
// component: () => import('./routes/Pages'),
7-
// LoadingComponent: Loading,
8-
// })
9-
105
// routing load component
116
if (props.routeData && props.routeData.length > 0) {
127
props.routeData.map((item) => {
13-
// item.component = LoadableComponent
148
item.component = Markdown
159
return item
1610
})

packages/crd-theme/markdown/index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { MDXProvider } from '@mdx-js/react'
44
import { Helmet } from 'react-helmet'
55
import CodeBlock from './codeBlock'
66
import Link from './Link'
7-
import Loading from '../component/Loading'
87
import styles from './style/index.less'
98

10-
// const { useState, useLayoutEffect, useRef } = React
119
const { useState, useEffect, useRef } = React
1210

1311
const components = {
@@ -19,31 +17,31 @@ function Markdown(markdownProps) {
1917
const { props } = markdownProps
2018
const { relative, name } = props
2119

22-
const getInitMarkdownCP = () => {
20+
const getRmFirstSlashMarkdownName = () => {
2321
const relativeMd = relative
2422
if (!relativeMd) return null
23+
return relative.slice(1, relative.length - 3)
24+
}
2525

26-
const rmFirstSlash = relative.slice(1, relative.length - 3)
27-
return () => require(`__project_root__/${rmFirstSlash}.md`).default
26+
const getInitMarkdownCP = () => {
27+
const markdownName = getRmFirstSlashMarkdownName()
28+
if (!markdownName) return
29+
return () => require(`__project_root__/${markdownName}.md`).default
2830
}
29-
getInitMarkdownCP()
3031

3132
const [MarkdownCP, setMarkdownCP] = useState(getInitMarkdownCP())
3233
const markdownWrapperRef = useRef(null)
3334

3435
const renderMarkdown = () => {
35-
const relativeMd = relative
36-
if (!relativeMd) return null
37-
38-
const rmFirstSlash = relative.slice(1, relative.length - 3)
36+
const markdownName = getRmFirstSlashMarkdownName()
37+
if (!markdownName) return
3938
// it must be writen with / & .md in dynamic import
40-
import(`__project_root__/${rmFirstSlash}.md`).then((data) => {
39+
import(`__project_root__/${markdownName}.md`).then((data) => {
4140
// data.default is a function, so we should write () => data.default in setState here.
4241
setMarkdownCP(() => (data.default || data))
4342
})
4443
}
4544

46-
// useLayoutEffect(() => {
4745
useEffect(() => {
4846
renderMarkdown()
4947
}, [])
@@ -60,17 +58,15 @@ function Markdown(markdownProps) {
6058
</Helmet>
6159
{
6260
MarkdownCP
63-
? <div className={cx('markdown', styles.markdown, styles.markdownwrapper)} ref={markdownWrapperRef}>
64-
{
65-
// MarkdownCP
66-
// ?
67-
<MDXProvider
68-
components={components}
69-
>
70-
<MarkdownCP />
71-
</MDXProvider>
72-
// : <Loading />
73-
}
61+
? <div
62+
className={cx('markdown', styles.markdown, styles.markdownwrapper)}
63+
ref={markdownWrapperRef}
64+
>
65+
<MDXProvider
66+
components={components}
67+
>
68+
<MarkdownCP />
69+
</MDXProvider>
7470
</div>
7571
: null
7672
}

0 commit comments

Comments
 (0)