Skip to content

Commit 9e54e37

Browse files
authored
fix(browser-logs): deduplicate consecutive runtime error logs (#6263)
1 parent 2922267 commit 9e54e37

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { gotoPage, test } from '@e2e/helper';
2+
3+
test('should not output the same error log consecutively', async ({
4+
devOnly,
5+
page,
6+
}) => {
7+
const rsbuild = await devOnly();
8+
await gotoPage(page, rsbuild, '/', { hash: 'test1' });
9+
await rsbuild.expectLog('Uncaught Error: #test1');
10+
rsbuild.clearLogs();
11+
12+
await page.reload();
13+
await gotoPage(page, rsbuild, '/', { hash: 'test2' });
14+
await page.reload();
15+
await rsbuild.expectLog('Uncaught Error: #test2');
16+
rsbuild.expectNoLog('Uncaught Error: #test1');
17+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error(location.hash);

e2e/cases/plugin-api/plugin-before-start-dev-server-hook/index.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildEntryUrl, expect, test } from '@e2e/helper';
1+
import { expect, gotoPage, test } from '@e2e/helper';
22
import type { RsbuildPlugin } from '@rsbuild/core';
33

44
test('should run onBeforeStartDevServer hooks and add custom middleware', async ({
@@ -36,12 +36,9 @@ test('should run onBeforeStartDevServer hooks and add custom middleware', async
3636
},
3737
});
3838

39-
const testUrl = buildEntryUrl('test', rsbuild.port);
40-
const test2Url = buildEntryUrl('test2', rsbuild.port);
41-
42-
await page.goto(testUrl);
39+
await gotoPage(page, rsbuild, 'test');
4340
await expect(page.content()).resolves.toContain('Hello, world!');
4441

45-
await page.goto(test2Url);
42+
await gotoPage(page, rsbuild, 'test2');
4643
await expect(page.content()).resolves.toContain('Hello, world2!');
4744
});

e2e/helper/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export const gotoPage = async (
2929
page: Page,
3030
rsbuild: { port: number },
3131
path = 'index',
32+
{ hash = '' } = {},
3233
) => {
33-
const url = buildEntryUrl(path, rsbuild.port);
34+
const url = `${buildEntryUrl(path, rsbuild.port)}${hash ? `#${hash}` : ''}`;
3435
return page.goto(url);
3536
};
3637

packages/core/src/server/browserLogs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ const formatErrorLocation = async (
109109
return rawLocation;
110110
};
111111

112+
let lastRuntimeErrorLog: string;
113+
112114
/**
113115
* Processes runtime errors received from the browser and logs them with
114116
* source location information.
@@ -127,5 +129,11 @@ export const reportRuntimeError = async (
127129
}
128130
}
129131

132+
// Avoid outputting the same log consecutively
133+
if (log === lastRuntimeErrorLog) {
134+
return;
135+
}
136+
130137
logger.error(log);
138+
lastRuntimeErrorLog = log;
131139
};

0 commit comments

Comments
 (0)