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

vite can not get cookie from set-cookie #18025

Open
7 tasks done
fishermess opened this issue Sep 4, 2024 · 5 comments
Open
7 tasks done

vite can not get cookie from set-cookie #18025

fishermess opened this issue Sep 4, 2024 · 5 comments

Comments

@fishermess
Copy link

Describe the bug

When I send /api/login request to server, it sends me Set-cookie, but I can get cookie in browser.I tried change proxy '/api' to '/', it is useful in vue-cli, however it doesn't send to server any more rather than visit local static resources.

Reproduction

https://gitee.com/kooji/vue3-demo

Steps to reproduce

  1. problem 1
    set proxy
    '/': {
    target: env.VITE_BASE_URL,
    secure: false,
    headers: {
    Connection: 'keep-alive',
    },
    changeOrigin: true,
    rewrite: (path: string) => path.replace(/^/api/, ''),
    },
    can not send request and get local static resources.

2.problem 2
set proxy
'/a': {
target: env.VITE_BASE_URL,
secure: false,
headers: {
Connection: 'keep-alive',
},
changeOrigin: true,
rewrite: (path: string) => path.replace(/^/api/, ''),
},
or
'/ap': {
target: env.VITE_BASE_URL,
secure: false,
headers: {
Connection: 'keep-alive',
},
changeOrigin: true,
rewrite: (path: string) => path.replace(/^/api/, ''),
},
When i send /api/login it should not send successfully but it did.

3.problem 3
I have to set proxy as
'/api': {
target: env.VITE_BASE_URL,
secure: false,
headers: {
Connection: 'keep-alive',
},
changeOrigin: true,
rewrite: (path: string) => path.replace(/^/api/, ''),
},

It works, but it cannot get Set-cookie from server.Because cookie path has been setted as '/api'.

System Info

No info, just doesn't work when proxy setted '/', can not send request and get local static resources.

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

  1. problem 1
    can not send request and get local static resources.

This is expected. You are proxying all paths so the request doesn't hit Vite's internal middlewares.

  1. problem 2
    When i send /api/login it should not send successfully but it did.

This is also expected. It's a prefix match.

  1. problem 3
    It works, but it cannot get Set-cookie from server.Because cookie path has been setted as '/api'.

AFAIK Vite doesn't change Set-Cookie header as long as cookiePathRewrite is not set. I guess the server behind the proxy sets the cookie with path.

@fishermess
Copy link
Author

fishermess commented Sep 11, 2024

  1. problem 1
    can not send request and get local static resources.

This is expected. You are proxying all paths so the request doesn't hit Vite's internal middlewares.

  1. problem 2
    When i send /api/login it should not send successfully but it did.

This is also expected. It's a prefix match.

  1. problem 3
    It works, but it cannot get Set-cookie from server.Because cookie path has been setted as '/api'.

AFAIK Vite doesn't change Set-Cookie header as long as cookiePathRewrite is not set. I guess the server behind the proxy sets the cookie with path.

1.The write way of problem1 is works in vue-Cli.
2.Why does vite choose prefix match rather than equal?
3.The server send set-cookie without path, and I think it is too unconvinent without rewritecookie interface for vite developers.

@sapphi-red
Copy link
Member

1.The write way of problem1 is works in vue-Cli.

Vite does not aim for one-to-one compatibility with Vue CLI. This behavior has been existed for a long time and IMHO not worth making a breaking change unless there's many advantages.

2.Why does vite choose prefix match rather than equal?

It is common to proxy all paths under a specific path and most of the case that way works with people that want to proxy a single path.

3.The server send set-cookie without path, and I think it is too unconvinent without rewritecookie interface for vite developers.

I don't get what you mean. cookiePathRewrite option does exist in Vite. If you set that option, you can rewrite the path of the Set-Cookie header. If you don't set that option, then Vite will pass the header as-is. If your server behind the proxy sends Set-Cookie without a path and you don't set cookiePathRewrite, then the header won't include a path.

@fishermess
Copy link
Author

fishermess commented Sep 13, 2024

3.The server send set-cookie without path, and I think it is too unconvinent without rewritecookie interface for vite developers.

I don't get what you mean. cookiePathRewrite option does exist in Vite. If you set that option, you can rewrite the path of the Set-Cookie header. If you don't set that option, then Vite will pass the header as-is. If your server behind the proxy sends Set-Cookie without a path and you don't set cookiePathRewrite, then the header won't include a path.

This method I tried, but it doesn't work for me, because the set-cookie of response don't set path.
cookiePathRewrite: {
'': '/',
}, or cookiePathRewrite: {
'*': '/',
},
doesn't work.

@fishermess
Copy link
Author

2.Why does vite choose prefix match rather than equal?

It is common to proxy all paths under a specific path and most of the case that way works with people that want to proxy a single path.

I can't see eye to eye with you to this design. if I set proxy '/a', it works on ‘/a/login’, it is OK.However it can also works on '/api/login'.It is not reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants