umijs / umi-example-ssr-with-egg

example for building frontend app with umi Server-Side Render and PreRender
80 stars 11 forks source link

sessionStorage is not defined #20

Closed jimmywong123 closed 5 years ago

jimmywong123 commented 5 years ago

运行prerender的时候会报一个这个错误:


    at <anonymous>
☒  complete  account.html
/ad/components/Card activeRoute's getInitialProps function not found
☒  complete  /ad/components/Card.html
uncaught at _callee3 at _callee3 
 at _callee6 
 at takeEvery(system/getSeo, _callee) 
 at _callee 
 at _callee 
 Error: ReferenceError: sessionStorage is not defined
    at p (/Users/jimmy/Documents/nodejs/work/u_hc_web/app/web/site/umi.server.js:1:32032)
    at i (/Users/jimmy/Documents/nodejs/work/u_hc_web/app/web/site/umi.server.js:1:57546)
    at runCallEffect (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:524:19)
    at runEffect (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:446:402)
    at next (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:326:9)
    at proc (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:281:3)
    at resolveIterator (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:467:5)
    at runEffect (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:443:178)
    at next (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:326:9)
    at currCb (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:399:7)
    at /Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:510:16
    at exec (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/scheduler.js:25:5)
    at flush (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/scheduler.js:66:5)
    at asap (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/scheduler.js:39:5)
    at Array.<anonymous> (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/channel.js:197:27)
    at Object.emit (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/channel.js:38:13)
    at /Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/lib/index.js:209:17
    at /Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/lib/Plugin.js:102:26
    at onError (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/lib/index.js:210:11)
    at sagaWithCatch$ (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/lib/getSaga.js:188:13)
    at tryCatch (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/node_modules/regenerator-runtime/runtime.js:296:22)
    at Generator.prototype.(anonymous function) [as throw] (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/dva-core/node_modules/regenerator-runtime/runtime.js:114:21)
    at next (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:300:32)
    at Object.currCb [as cont] (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:399:7)
    at end (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:367:23)
    at abort (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:90:5)
    at Object.task.cont (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:103:9)
    at next (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:339:16)
    at currCb (/Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:399:7)
    at /Users/jimmy/Documents/nodejs/work/u_hc_web/node_modules/redux-saga/lib/internal/proc.js:462:14
    at <anonymous>

> u_hc_web@1.0.0 stop /Users/jimmy/Documents/nodejs/work/u_hc_web
> egg-scripts stop --title=u_hc_web

[egg-scripts] stopping egg application with --title=u_hc_web
[egg-scripts] got master pid ["10841"]
[egg-scripts] stopped```
iisbigfish commented 5 years ago

直接 sessionStorage 是往 global 找。。 如果是最新的ssr或者是预渲染我记得是有加入ssr-polyfill,sessionStorage 加上window就不会有这个报错了。

ycjcl868 commented 5 years ago

window.sessionStorage,直接写 sessionStorage,在服务端渲染时,是使用 global.sessionStorage,会找不到的。