Skip to content

Commit 3ba2fa5

Browse files
authored
fix: pass own logger in historyApiFallback (#3373)
1 parent e3f366f commit 3ba2fa5

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

lib/Server.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,22 @@ class Server {
251251
setupHistoryApiFallbackFeature() {
252252
const historyApiFallback = require('connect-history-api-fallback');
253253

254-
const fallback =
255-
typeof this.options.historyApiFallback === 'object'
254+
const options =
255+
typeof this.options.historyApiFallback !== 'boolean'
256256
? this.options.historyApiFallback
257-
: null;
257+
: {};
258+
259+
let logger;
260+
261+
if (typeof options.verbose === 'undefined') {
262+
logger = this.logger.log.bind(
263+
this.logger,
264+
'[connect-history-api-fallback]'
265+
);
266+
}
258267

259268
// Fall back to /index.html if nothing else matches.
260-
this.app.use(historyApiFallback(fallback));
269+
this.app.use(historyApiFallback({ logger, ...options }));
261270
}
262271

263272
setupStaticFeature() {

test/server/historyApiFallback-option.test.js

+96
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,102 @@ describe('historyApiFallback option', () => {
178178
});
179179
});
180180

181+
describe('as object with the "verbose" option', () => {
182+
let consoleSpy;
183+
184+
beforeAll((done) => {
185+
consoleSpy = jest.spyOn(global.console, 'log');
186+
187+
server = testServer.start(
188+
config,
189+
{
190+
historyApiFallback: {
191+
index: '/bar.html',
192+
verbose: true,
193+
},
194+
port,
195+
},
196+
done
197+
);
198+
req = request(server.app);
199+
});
200+
201+
afterAll(() => {
202+
consoleSpy.mockRestore();
203+
});
204+
205+
it('request to directory and log', (done) => {
206+
req
207+
.get('/foo')
208+
.accept('html')
209+
.expect(200, /Foobar/, (error) => {
210+
if (error) {
211+
done(error);
212+
213+
return;
214+
}
215+
216+
expect(consoleSpy).toHaveBeenCalledWith(
217+
'Rewriting',
218+
'GET',
219+
'/foo',
220+
'to',
221+
'/bar.html'
222+
);
223+
224+
done();
225+
});
226+
});
227+
});
228+
229+
describe('as object with the "logger" option', () => {
230+
let consoleSpy;
231+
232+
beforeAll((done) => {
233+
consoleSpy = jest.spyOn(global.console, 'log');
234+
235+
server = testServer.start(
236+
config,
237+
{
238+
historyApiFallback: {
239+
index: '/bar.html',
240+
logger: consoleSpy,
241+
},
242+
port,
243+
},
244+
done
245+
);
246+
req = request(server.app);
247+
});
248+
249+
afterAll(() => {
250+
consoleSpy.mockRestore();
251+
});
252+
253+
it('request to directory and log', (done) => {
254+
req
255+
.get('/foo')
256+
.accept('html')
257+
.expect(200, /Foobar/, (error) => {
258+
if (error) {
259+
done(error);
260+
261+
return;
262+
}
263+
264+
expect(consoleSpy).toHaveBeenCalledWith(
265+
'Rewriting',
266+
'GET',
267+
'/foo',
268+
'to',
269+
'/bar.html'
270+
);
271+
272+
done();
273+
});
274+
});
275+
});
276+
181277
describe('in-memory files', () => {
182278
beforeAll((done) => {
183279
server = testServer.start(

0 commit comments

Comments
 (0)