Skip to content

docs(main): fix server.proxy doc example code#10169

Closed
luckynumberke7in wants to merge 2 commits intovitejs:mainfrom
luckynumberke7in:patch-1
Closed

docs(main): fix server.proxy doc example code#10169
luckynumberke7in wants to merge 2 commits intovitejs:mainfrom
luckynumberke7in:patch-1

Conversation

@luckynumberke7in
Copy link

@luckynumberke7in luckynumberke7in commented Sep 19, 2022

docs(main): fix server.proxy doc example code

wrap path.replace regex in a string to resemble working code

should resolve issues like #1014, #3475, #10168

Description

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

docs(main): fix server.proxy doc example code

wrap `path.replace` regex in a string to resemble working code

should resolve issues like #1014, #3475, #10168
@luckynumberke7in
Copy link
Author

vite working

This is the working file, it had the escape character in it, but when I saved the string, it automatically disappeared (and still works like this).

vite broken

This is the broken version that the docs suggest.

You can see the 404 failure and that the proxy is not working.

I have also set up a proxy using direct path and no rewrite: which worked, and lead me to believe it was an issue with the rewrite path logic.

@luckynumberke7in
Copy link
Author

Note: the working one works with and without secure: false, and the broken version does not work with or without secure: false

@luckynumberke7in
Copy link
Author

Also, I think it's failing for the save discrepancy I mentioned above (when I saved the escape characters vanish with prettier).

@luckynumberke7in
Copy link
Author

I should probably update the PR to be rewrite: (path) => path.replace('/^/api/', ''), and rewrite: (path) => path.replace('/^/fallback/', ''), huh?

This is my first public PR, sorry if I'm all over the place!

@luckynumberke7in
Copy link
Author

For future reference: How would I go about updating my PR? Do I have to refork and redo? or can I push another change to the current PR?

target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
rewrite: (path) => path.replace('/^\/api/', '')
Copy link
Contributor

@tony19 tony19 Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct.

path.replace() is String.prototype.replace(), which can take a regular expression as its first argument.

Your change would cause rewrite to only perform a replacement if path contained /^\/api/ literally, e.g., https://localhost:5173/^\/api/posts.

The reason your change worked in your own code is likely because it effectively prevented the rewrite from occurring, which means you don't need the rewrite in the first place. Just remove rewrite in your config.

Copy link
Author

@luckynumberke7in luckynumberke7in Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, that did work! Sorry, everything I found online included the rewrite, but it just was not working.

I came to the docs and came to the same conclusion since it was in options and regex.

Maybe it could be made a little more clear that proxy matches following routes (i.e. '/api/products', /api/products/1', etc.) by default using:

proxy: {
      '/api': {
        target: 'http://localhost:5000',
        changeOrigin: true,
        secure: false,
      }

It was confusing for me at least.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we could have examples.

// string shorthand: http://localhost:5173/foo -> http://localhost:4567/foo
// with options: http://localhost:5173/api/bar-> http://jsonplaceholder.typicode.com/bar

Copy link
Author

@luckynumberke7in luckynumberke7in Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability, I was thinking something more along the lines of:

export default defineConfig({
  server: {
    proxy: {
      // string shorthand, matches calls to '/foo/bar' by default
      '/foo': 'http://localhost:4567',
      // with options, returns http://jsonplaceholder.typicode.com/bar
      '/api': {
        target: 'http://jsonplaceholder.typicode.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the matching subsequent folders by default is what was confusing me. I thought I needed a rewrite to achieve this.

@tony19
Copy link
Contributor

tony19 commented Sep 20, 2022

For future reference: How would I go about updating my PR? Do I have to refork and redo? or can I push another change to the current PR?

You can continue pushing changes to the PR. If the PR is not ready to be reviewed, mark it as a Draft. In my PRs, if I expect a high number of pushes (which causes a lot of notification noise for reviewers), I make my changes in a branch off my PR branch, and then merge back when ready for review.

@bluwy
Copy link
Member

bluwy commented Sep 23, 2022

@luckynumberke7in do you want to implement #10169 (comment) in this PR? Marking this one as draft for now, once you're ready you can mark this ready for review.

@bluwy bluwy marked this pull request as draft September 23, 2022 09:31
docs(main): add examples to server.proxy doc

add examples to proxy for readability

should resolve issues like #1014, #3475, #10168
// string shorthand, matches calls to '/foo/bar' by default
'/foo': 'http://localhost:4567',
// with options
// with options, returns http://jsonplaceholder.typicode.com/bar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the matching subsequent folders by default is what was confusing me. I thought I needed a rewrite to achieve this.

If this is the reason, I think it's better to update the description (line 80) because these examples are a bit ambiguous.

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.

4 participants