Nextjs似乎不可以使用Suspense
今天准备把一个异步加载的组件用Suspense
,结果似乎不行。
报错如下:
ReactDOMServer does not yet support Suspense.
后来又搜到一个帖子,看来是确定了:
https://spectrum.chat/next-js/general/suspense-support~f83c5c32-cb4d-419e-ba3d-f08948e63584
今天准备把一个异步加载的组件用Suspense
,结果似乎不行。
报错如下:
ReactDOMServer does not yet support Suspense.
后来又搜到一个帖子,看来是确定了:
https://spectrum.chat/next-js/general/suspense-support~f83c5c32-cb4d-419e-ba3d-f08948e63584
- It is rendered once on the server - it is the root of your app
- App's getInitialProps is called before anything else - even before getInitialProps in _document.js
- It is created and mounted ONLY ONCE on the client - when the page first loads. Thereafter, on page navigation, App.getInitialProps is called before each page, and then its render function is called, to render the incoming page.
最近沉迷 SSR,因为考虑到了一些需求:
当然了,谷歌牛逼,都能直接解析 SPA 了,但问题我主要还是一个用户体验上:用户点击一个 url,SPA 就 404 了,但一个体验很好的网站,显然希望是能够加载出来的。下面是Vuejs
的SSR网站对SEO问题的解释:
Better SEO, as the search engine crawlers will directly see the fully rendered page.Note that as of now, Google and Bing can index synchronous JavaScript applications just fine. Synchronous being the key word there. If your app starts with a loading spinner, then fetches content via Ajax, the crawler will not wait for you to finish. This means if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary.
所以就开始了迁移,从 React SPA 迁移到 nextjs。
nextjs 很多大坑,比如说:
getInitialProps 只能在pages这个目录下用
关于 router:
PM2 部署
Webpack配置的坑:
next.config.jsde 问题
.env
,而要用这个配置文件里的env
一些迷
首先,Next.js
是个好学校,啊不,是个好框架;
其次,Next.js
的文档也不算烂,但确实有点简陋,好多都是undocumented
。
Next.js
有个feature叫做file-system routing
:
By default, Next will serve each file in/pages
under a pathname matching the filename (eg,/pages/some-file.js
is served atsite.com/some-file
.
你可以设置一个option来turn it off:
// next.config.js
module.exports = {
useFileSystemPublicRoutes: false,
}
我今天谈论的重点是这个问题:当我在Components/SomeComponenet.jsx
里使用router(不论是useRouter还是withRouter),都会报错,错误信息这样的:
useRouter
用在functional component
里;withRouter
用在class component
里。
React做的SPA,非常快,然而,对SEO很不友好,社交分享更是不行。
所以就必须考虑服务端渲染了,就是所谓的server side rendering。
最近在做这个迁移工作,很久了,都没搞定。一方面不太熟悉nextjs,一方面工作量也确实不小,代码复用性不是特别好,所以应该从一开始就考虑ssr,而不是做到一半再迁移过去。