Search code examples
vue.jsnpmwebpacksetcookie

`set-cookie` doesn't work in dist generate by `npm run build`


I have a vue frontend server and we use cookie to control login state with a simple backend server. The problem is, set-cookie works fine in production model by running npm run serve.

But after I pack it into a dist folder and serve it by serve -s dist, set-cookie from backend response doesn't work, and there is no warning or hint in the browser.

I not familiar with the frontend, so feel free if you have any thought. Any information may help me a lot, thanks!


Solution

  • I fixed it finally. Thank to @Luke again!

    The biggest mistake I made is I didn't check whether cookie had been set.

    I don't think this will completely solve your problem but it may help a bit: To see whether your cookies are being set in Chrome do the following: 1) Click F12 (this opens the developer tools.) 2) Click on the Application tab. 3) Click on the Cookies option on the left. 4) Keep an eye on what cookies are being set as you interact with your different server environments. – Luke

    Vue proxy will keep you away from cookie problems while you are developing with npm run serve, but it won't give any help if you pack it by npm run build.

    So you need to remember to set withCredentials = true, in your project. I use axios for example, so the good way should be setting axios.defaults.withCredentials = true; in main.js.