1- import type { DevEnvironment , Plugin } from "vite" ;
1+ import type { DevEnvironment , Plugin , ResolvedServerOptions } from "vite" ;
22import * as path from "@std/path" ;
33import { contentType as getStdContentType } from "@std/media-types/content-type" ;
44import { ASSET_CACHE_BUST_KEY } from "fresh/internal" ;
@@ -10,6 +10,34 @@ function getContentType(ext: string): string {
1010 return getStdContentType ( ext ) ?? "application/octet-stream" ;
1111}
1212
13+ /**
14+ * Handling the user config of proxy
15+ * https://vite.dev/config/server-options#server-proxy
16+ */
17+ function createProxyUrlMatcher (
18+ proxy : ResolvedServerOptions [ "proxy" ] ,
19+ ) : ( ( url : string ) => boolean ) | undefined {
20+ if ( proxy === undefined ) return undefined ;
21+
22+ const matchers = Object . keys ( proxy ) . map ( ( context ) => {
23+ // with RegExp
24+ if ( context [ 0 ] === "^" ) {
25+ const regex = new RegExp ( context ) ;
26+ return ( url : string ) => regex . test ( url ) ;
27+ }
28+ // string shorthand
29+ return ( url : string ) => url . startsWith ( context ) ;
30+ } ) ;
31+
32+ return ( url : string ) => {
33+ for ( const matches of matchers ) {
34+ if ( matches ( url ) ) return true ;
35+ }
36+
37+ return false ;
38+ } ;
39+ }
40+
1341export function devServer ( freshConfig : ResolvedFreshViteConfig ) : Plugin [ ] {
1442 let publicDir = "" ;
1543 return [
@@ -27,15 +55,25 @@ export function devServer(freshConfig: ResolvedFreshViteConfig): Plugin[] {
2755 const IGNORE_URLS = new RegExp (
2856 `^(${ base } )?/(@(vite|fs|id)|\\.vite)/` ,
2957 ) ;
58+ // build proxy url list matcher beofre the request is coming
59+ const matchesProxyUrl = createProxyUrlMatcher (
60+ server . config . server . proxy ,
61+ ) ;
3062
3163 server . middlewares . use ( async ( nodeReq , nodeRes , next ) => {
3264 const serverCfg = server . config . server ;
65+ const rawUrl = nodeReq . url ?? "/" ;
66+
67+ // bypass the request when the proxy specified
68+ if ( matchesProxyUrl ?.( rawUrl ) ) {
69+ return next ( ) ;
70+ }
3371
3472 const protocol = serverCfg . https ? "https" : "http" ;
3573 const host = serverCfg . host ? serverCfg . host : "localhost" ;
3674 const port = serverCfg . port ;
3775 const url = new URL (
38- `${ protocol } ://${ host } :${ port } ${ nodeReq . url ?? "/" } ` ,
76+ `${ protocol } ://${ host } :${ port } ${ rawUrl } ` ,
3977 ) ;
4078
4179 // Don't cache in dev
0 commit comments