Skip to content

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

Open
Fatpandac wants to merge 2 commits into
vitejs:mainfrom
Fatpandac:main
Open

feat(proxy): add router to support dynamically set the target#13968
Fatpandac wants to merge 2 commits into
vitejs:mainfrom
Fatpandac:main

Conversation

@Fatpandac

Copy link
Copy Markdown

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
  • 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 PR Title 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.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

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

@patak-cat

Copy link
Copy Markdown
Member

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

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-cat patak-cat added the p2-to-be-discussed Enhancement under consideration (priority) label Jul 27, 2023
@sapphi-red

Copy link
Copy Markdown
Member

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

@Fatpandac

Copy link
Copy Markdown
Author

Have any news about this PR?

@Ganbin

Ganbin commented Jan 4, 2024

Copy link
Copy Markdown

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 #2808 seems to be more developed.

@js2me

js2me commented Aug 18, 2024

Copy link
Copy Markdown

any updates?

/**
* dynamically set the target
*/
router?: (req: http.IncomingMessage) => HttpProxy.ServerOptions['target']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of adding a new property, would you change the existing target property to receive a function?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok, I’ll try that.

@sapphi-red sapphi-red moved this from Later to Approved in Team Board Oct 1, 2025
@tbontb-iaq

Copy link
Copy Markdown

It's 2026, any progress?

@sapphi-red

Copy link
Copy Markdown
Member

@tbontb-iaq Feel free to create a new PR that addresses #13968 (comment)

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

Labels

p2-to-be-discussed Enhancement under consideration (priority)

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

6 participants