Skip to content
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

Fix ssr #1

Merged
merged 5 commits into from
Apr 3, 2019
Merged

Fix ssr #1

merged 5 commits into from
Apr 3, 2019

Conversation

haisheng-lin
Copy link
Owner

学习到章节 5-25-3 时候出现一些问题:

1. 异步加载路由会报错

解决方案是使用 mini-css-extract-plugin 代替 extract-text-webpack-plugin,但是只在 webpack.config.client.js 使用,不要在 webpack.config.server.js 用,issue 链接:webpack-contrib/mini-css-extract-plugin#90

2. 服务端渲染得到的 js 文件内容是 html 代码

老师的解释是:

你这个应该是没有配置webpack的publicPath,导致你在服务端渲染的时候请求路径没有/public/这样的前缀,所以服务器没办法拦截返回js文件,都进入到服务端渲染的流程,所以返回的html

所以我就在 webpack.config.base.js 加上 publicPath: '/public/',不过还没有用,需要配上 devServer 的完整路径://127.0.0.1:8000/public/。因为如果是 /public/ 的话,服务端渲染访问静态资源的路径是 //127.0.0.1:3333/xxx,但是服务端没有这种静态资源,而且根据我们写的中间件,所有路由都匹配到 handleSSR 这里了,所以打开这个路径,里面的内容都是 handleSSR 返回的 html 代码。这一部分其实在之后的章节 5-4 有提到。

还有一些其他小问题和可优化的地方,例如安装一个 nodemon 自动重启服务端渲染服务等等。

@haisheng-lin haisheng-lin merged commit c86f88f into master Apr 3, 2019
@haisheng-lin haisheng-lin deleted the fix-ssr branch April 3, 2019 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant