Skip to content
22 changes: 15 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,22 @@ class Server {
}
}

const bypass = typeof proxyConfig.bypass === 'function';

const bypassUrl =
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;

if (bypassUrl) {
// - Check if we have a bypass function defined
// - In case the bypass function is defined we'll retrieve the
// bypassUrl from it otherwise byPassUrl would be null
const isByPassFuncDefined =
typeof proxyConfig.bypass === 'function';
const bypassUrl = isByPassFuncDefined
Comment thread
mistic marked this conversation as resolved.
? proxyConfig.bypass(req, res, proxyConfig)
: null;

if (typeof bypassUrl === 'boolean') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is not aligning to the documentation

In the function you get access to the request, response and proxy options. It must return either false or a path that will be served instead of continuing to proxy the request.

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.

PR welcome to webpack docs

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

// skip the proxy
req.url = null;
next();
} else if (typeof bypassUrl === 'string') {
// byPass to that url
req.url = bypassUrl;

next();
} else if (proxyMiddleware) {
return proxyMiddleware(req, res, next);
Expand Down
13 changes: 13 additions & 0 deletions test/Proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const proxyOptionPathsAsProperties = {
if (/\.html$/.test(req.path)) {
return '/index.html';
}

return null;
},
},
'/proxyfalse': {
bypass(req) {
if (/\/proxyfalse$/.test(req.path)) {
return false;
}
},
},
};
Expand Down Expand Up @@ -116,6 +125,10 @@ describe('Proxy', () => {
it('should pass through a proxy when a bypass function returns null', (done) => {
req.get('/foo.js').expect(200, /Hey/, done);
});

it('should not pass through a proxy when a bypass function returns false', (done) => {
req.get('/proxyfalse').expect(404, done);
});
});
});

Expand Down