Skip to content

Commit bfe5203

Browse files
committed
πŸ› fix(ui): avoid throttling static assets with the spa fallback limiter
1 parent 0840163 commit bfe5203

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

β€Žapp/api/ui.test.tsβ€Ž

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ vi.mock('express', () => ({
1212
},
1313
}));
1414

15+
vi.mock('express-rate-limit', () => ({
16+
default: vi.fn(() => 'rate-limit-middleware'),
17+
}));
18+
1519
vi.mock('../runtime/paths', () => ({
1620
resolveUiDirectory: vi.fn(() => '/app/ui'),
1721
}));
@@ -27,12 +31,27 @@ describe('UI Router', () => {
2731
const router = uiRouter.init();
2832
expect(router).toBeDefined();
2933
expect(router.use).toHaveBeenCalledWith('static-middleware');
30-
expect(router.get).toHaveBeenCalledWith('/{*path}', expect.any(Function));
34+
expect(router.get).toHaveBeenCalledWith(
35+
'/{*path}',
36+
'rate-limit-middleware',
37+
expect.any(Function),
38+
);
39+
});
40+
41+
test('should apply rate limiting only to SPA document fallback requests', () => {
42+
uiRouter.init();
43+
44+
expect(mockRouter.use).not.toHaveBeenCalledWith('rate-limit-middleware');
45+
expect(mockRouter.get).toHaveBeenCalledWith(
46+
'/{*path}',
47+
'rate-limit-middleware',
48+
expect.any(Function),
49+
);
3150
});
3251

3352
test('catch-all should send index.html', () => {
3453
uiRouter.init();
35-
const catchAllHandler = mockRouter.get.mock.calls.find((c) => c[0] === '/{*path}')[1];
54+
const catchAllHandler = mockRouter.get.mock.calls.find((c) => c[0] === '/{*path}')[2];
3655

3756
const res = { sendFile: vi.fn() };
3857
catchAllHandler({}, res);

β€Žapp/api/ui.tsβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ export function init() {
1717
legacyHeaders: false,
1818
validate: { xForwardedForHeader: false },
1919
});
20-
router.use(uiLimiter);
2120
router.use(express.static(uiDirectory));
2221

2322
// Redirect all 404 to index.html (for vue history mode)
2423
const indexFile = path.resolve(path.join(uiDirectory, 'index.html'));
25-
router.get('/{*path}', (req, res) => {
24+
router.get('/{*path}', uiLimiter, (req, res) => {
2625
res.sendFile(indexFile);
2726
});
2827
return router;

0 commit comments

Comments
Β (0)