-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support ssr #2543
feat: support ssr #2543
Conversation
}); | ||
if (config.ssr) { | ||
history = ` | ||
__IS_BROWSER ? ${initialHistory} : require('history/createMemoryHistory').default() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
entries 需要给默认值吗?
__IS_BROWSER ? ${initialHistory} : require('history/createMemoryHistory').default() | |
__IS_BROWSER ? ${initialHistory} : require('history/createMemoryHistory').default({ initialEntries: window.g_initialEntries }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用,serverRender 时会 push 一个。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看到了,有个 history.push
let serverRender; | ||
if (!__IS_BROWSER) { | ||
serverRender = async (ctx) => { | ||
const pathname = ctx.req.url; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果是 koa
,可能是 ctx.request.url
。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你觉得这里做兼容好还是直接把参数换成 url 好?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉按照统一的规范,构造一个符合规范的ctx对象比较好例如express事先定义一个const ctx={req: request,}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
除了 url,我们还会用到其他的参数吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
除了 url,我们还会用到其他的参数吗?
与实际业务需求有关,可能有些业务需要拿到ctx.req.host之类的参数
} | ||
}); | ||
return activeRoute; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,我补下
hooks 的问题我猜是多份 react-dom 的问题, |
嗯问题是因为server与client用的react-dom不是同一个,https://zhuanlan.zhihu.com/p/57651697 |
还有个问题是 umijs/umi-request,不支持服务端请求。 AboutLanding.getInitialProps = async () => {
require('es6-promise').polyfill();
require('isomorphic-fetch');
const res = await fetch('https://mock').then(response => response.json());
return {
data: res,
}
} => |
|
|
要的是 csr 的 manifest,为啥打 ssr 的 manifest?ssr 的 manifest 和 csr 的应该会有差异吧。 |
嗯嗯,知道了。有区别,要用户开启 |
chunkMap 整理(不仅是 chunkMap 的问题,也是后面 问题本质是 server 配置依赖 client 端生成的产物(*.json)
方案讨论上面的问题,都是与 // asset-manifest.json
{
"layout.css": "/layout.147eaece.chunk.css",
"layout.js": "/layout.45eefd85.async.js",
"p__About.js": "/p__About.187c069f.async.js",
"p__About__History.css": "/p__About__History.691667cd.chunk.css",
"p__About__History.js": "/p__About__History.cd527e93.async.js",
"umi.css": "/umi.fa5ee7b5.css",
"umi.js": "/umi.f8c632ea.js",
"pages": [
{
"/about": [
// 可通过后续判断 chunk 资源类型
"p__About.187c069f.async.js"
],
"/about/history": [
// 可通过后续判断 chunk 资源类型
"p__About__History.691667cd.chunk.css",
"p__About__History.cd527e93.async.js"
],
...
}
]
"vendors.css": "/vendors.0ff0ea50.chunk.css",
"vendors.js": "/vendors.704be5ad.async.js",
"index.html": "/index.html"
} 但目前的写法,是在编译中执行的 |
* fix: chunkMaps * 📝 must enable manifest when use ssr * fix: htmlTemplateMap use map * fix: publicPath
开始补下用例,改动有点多了。 |
如果用 squash 合并,细节都会丢掉,最好避免一下。 |
嗯嗯,squash merging 配置可以关掉。 @sorrycc |
packages/umi-build-dev/yarn.lock
Outdated
integrity sha512-/lg70HAjtkUgWPVZhZcm+T4hkL8Zbtp1nFNOn3lRrxnlv50SRBv7cR7RqR+GMsd3hUXy9hWBo4CHTbFTcOYwig== | ||
dependencies: | ||
mkdirp "^0.5.1" | ||
>>>>>>> theirs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conflicted
* test: htmlToJSX * 🚧 af-webpack ssr tests cases * test: umi-build-dev service * test: htmlGenerator * test: replaceChunkMaps * test: prerender e2e, fix: ssr not generate manifest * test: add e2e pre-render * fix: server babel not use corejs
* fix: manifest basePath, and html2JSX * fix: pre render a little polyfill * feat: eggjs with umi ssr demo * fix: mock window * 📝 umi ssr faq * rm: manifest * rm: demo * fix: .gitignore
server 时应该禁用 transform-runtime 插件,我先合了,再提 PR 解决吧。 |
ssr 动态路由不支持: npm run start 报错 you should not use exportStatic with dynamic route: ${route.path} |
@lilili001 |
使用
示例:
一、配置里启用 ssr
二、Page Component 新增 getInitialProps 的静态方法,返回 resolve 数据的 Promise
比如,
三、服务端引用
dist/umi.server.js
拿到 Html 文本Checklist
npm test
passesDescription of change