[英]Sapper session not setting properly in production build without page reload
I'm working on a project using Sapper, and been struggling with something for a bit now – can't quite figure it out.我正在使用 Sapper 进行一个项目,现在一直在努力解决一些问题 - 不太清楚。 I'm using Polka for my server (it was included with the
sveltejs/sapper-template#rollup
template so I went with it), and cookie-session
for my session middleware.我将 Polka 用于我的服务器(它包含在
sveltejs/sapper-template#rollup
模板中,所以我使用它),以及我的 session 中间件的cookie-session
。
Here is my Polka configuration, where I seed session data also:这是我的 Polka 配置,其中我还播种了 session 数据:
polka()
.use(
compression({ threshold: 0 }),
sirv('static', { dev }),
cookieSession({
name: 'session',
keys: [
'6e818055-d346-4fcb-bf56-4c7d54cb04ab',
'60f3e980-6e9c-460d-8ea7-af1fffbdb92f'
]
}),
sapper.middleware({
session: (req, res) => ({
user: req.session.user
})
})
)
.listen(PORT, err => {
if (err) console.log('error', err);
});
When a user logs in, it's handled in routes/login.js
essentially some profile info and tokens get set on session.user
and the request is redirected back to the root path.当用户登录时,它在
routes/login.js
中处理,本质上是一些配置文件信息和令牌在session.user
上设置,并且请求被重定向回根路径。 I'm using @polka/send-type
to respond.我正在使用
@polka/send-type
来响应。 See below:见下文:
req.session.user = {
accessToken: access_token,
name: first_name,
pic: picture.data.url
};
send(res, 302, '', {
Location: appUrl
});
There, I have a preload module for routes/index.svelte
checks if session.user
exists, and if so redirects to dashboard:在那里,我有一个用于
routes/index.svelte
的预加载模块检查session.user
是否存在,如果存在则重定向到仪表板:
<script context="module">
export async function preload(page, session) {
const { user } = session;
if (user) {
return this.redirect(302, "dashboard");
}
}
</script>
This all works fine in dev, but when I npm run build
and build for production it doesn't so well.这一切在开发中都可以正常工作,但是当我
npm run build
和构建以进行生产时,它并没有那么好。 It seems like the session.user
isn't getting populated after the redirect in login.js
.在
login.js
中重定向后,似乎session.user
没有被填充。
The session
object that I get in preload
doesn't have session.user
set after login, and vice versa on logout where session.user
is simply set to null and the client is redirected to the root, session.user
is still populated. The
session
object that I get in preload
doesn't have session.user
set after login, and vice versa on logout where session.user
is simply set to null and the client is redirected to the root, session.user
is still populated.
If I refresh the browser, session.user
is in the correct state.如果我刷新浏览器,
session.user
位于正确的 state 中。 Without a refresh – if I just logged out I can click around as if I were logged in, and if I had just logged in nothing happens as session.user
is undefined
.无需刷新 - 如果我刚刚注销,我可以像登录一样单击周围,如果我刚刚登录,则不会发生任何事情,因为
session.user
是undefined
。
Just not sure why it'd work on the dev build but not in production.只是不确定为什么它可以在开发版本上工作,但不能在生产中工作。 Given that the
session.user
state is correct on browser refresh, I'm guessing it's not an issue with the cookie middleware configuration.鉴于
session.user
state 在浏览器刷新时是正确的,我猜这不是 cookie 中间件配置的问题。 Seems like I'm missing something else.好像我还缺少其他东西。 Any help would be hugely appreciated!
任何帮助将不胜感激!
Sapper does indeed handle the cache headers differently for dev and production environment. Sapper 确实为开发和生产环境处理不同的缓存头。 That's why you are experiencing it this way.
这就是为什么你会以这种方式体验它。
But there is a simple solution by changing the sapper.middleware function:但是通过更改 sapper.middleware function 有一个简单的解决方案:
sapper.middleware({
session: (req, res) => {
res.setHeader('cache-control', 'no-cache, no-store')
return { user: req.session.user }
}
})
This sets the cache-control as you want it.这会根据需要设置缓存控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.