Skip to content

fix: bypass the request when the server.proxy is specified#3816

Open
Hajime-san wants to merge 5 commits into
freshframework:mainfrom
Hajime-san:fix-vite-proxy
Open

fix: bypass the request when the server.proxy is specified#3816
Hajime-san wants to merge 5 commits into
freshframework:mainfrom
Hajime-san:fix-vite-proxy

Conversation

@Hajime-san
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Contributor

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Approach looks right — matches Vite's own startsWith/^-prefix semantics, and precomputing the matcher in configureServer is nicer than rebuilding regexes per request. Left a few inline notes; once those are addressed I think this is good to go.

const IGNORE_URLS = new RegExp(
`^(${base})?/(@(vite|fs|id)|\\.vite)/`,
);
// build proxy url list matcher beofre the request is coming
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo: beofrebefore. Also worth rewording — the current phrasing is a bit hard to parse.

Suggested change
// build proxy url list matcher beofre the request is coming
// Precompute the proxy URL matcher; proxy config is fixed at server start.

* Handling the user config of proxy
* https://vite.dev/config/server-options#server-proxy
*/
function createProxyUrlMatcher(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One behavioral gap worth flagging: Vite's proxy options support a bypass(req, res, options) callback per entry that can return false to opt out of proxying for a given request. With this matcher, such requests would still be short-circuited away from Fresh and then fall through Vite's proxy without being proxied, ending up as 404s. Probably uncommon in practice — fine to leave for a follow-up, but a code comment noting the limitation would help future readers.

Comment on lines +536 to +540
{
const res = await fetch(`${address}/api3/pong?z=3`);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("api3");
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider adding a negative-case assertion so we prove the bypass is selective — e.g. that / still hits the Fresh route. Without it, a regression that bypasses every request would still pass these checks.

Suggested change
{
const res = await fetch(`${address}/api3/pong?z=3`);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("api3");
}
{
const res = await fetch(`${address}/api3/pong?z=3`);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("api3");
}
{
const res = await fetch(`${address}/`);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Exactly, thanks!

- Fix typo and rephrase comment ("build proxy url list matcher beofre")
- Add JSDoc note on the bypass() callback limitation
- Add negative-case assertion to prove Fresh routes are still reachable
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.

Bug: server.proxy not working with @fresh/plugin-vite

2 participants