vite icon indicating copy to clipboard operation
vite copied to clipboard

feat(proxy): add router to support dynamically set the target

Open Fatpandac opened this issue 2 years ago • 6 comments

Description

add router option to support dynamically set the target

Additional context

URL example: /dynamicProxy/localhost:9607/anotherApp

Configuration

server: {
	proxy: {
		'/dynamicProxy': {
			target: 'http://localhost:9600',
			router: (req) => {
				const target = req.url.match(/\/dynamicProxy\/(.*?)\//)[1]
				
				return `http://${target}`
			},
			rewrite: (path) => path.replace(/^\/dynamicProxy\/.*?\//, ''),
			changeOrigin: true,
		}
	}
}

What is the purpose of this pull request?

  • [ ] Bug fix
  • [X] New Feature
  • [ ] Documentation update
  • [ ] Other

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

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

Fatpandac avatar Jul 27 '23 16:07 Fatpandac

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Thanks for the PR. There is another PR implementing the same but also supporting a more extensive API here:

  • https://github.com/vitejs/vite/pull/2808

It seems we never ended up discussing this one in a team meeting. I'll add it to the Team board. Just to set expectations, it may take us a few weeks as we just met today.

patak-dev avatar Jul 27 '23 18:07 patak-dev

related: #13932 (The difference is that #13968 supports changing the target but #13932 doesn't)

sapphi-red avatar Aug 16 '23 06:08 sapphi-red

Have any news about this PR?

Fatpandac avatar Dec 02 '23 01:12 Fatpandac

That would be very nice to have this feature. I wonder what are the workaround in the current situation. I have see one custom plugin vite-plugin-proxy-middleware but didn't tested it yet.

This PR https://github.com/vitejs/vite/pull/2808 seems to be more developed.

Ganbin avatar Jan 04 '24 12:01 Ganbin

any updates?

js2me avatar Aug 18 '24 07:08 js2me