Frontend · Observability · JavaScript · React
Frontend Error Monitoring: Catching Production Errors and Reading Them with Source Maps
Catching browser errors in production: window.onerror, unhandledrejection, React and Angular error handlers, hidden source maps, release tags, noise filtering and ChunkLoadError.

In a product that does as much work in the browser as LiveTesting, you will not find the cause of an "the screen froze" complaint in the backend logs: the error happened in the user's browser and stayed there. Error handling on the server is a habit for everyone by now, but what happens in the browser is still a black box for many teams. In this post I explain how I collect frontend errors, how I turn minified stack traces into readable ones and how I filter out the noise.
Catching errors
The browser has two global entry points:
window.addEventListener('error', (event) => {
report({ message: event.message, stack: event.error?.stack, source: event.filename });
});
window.addEventListener('unhandledrejection', (event) => {
const reason = event.reason;
report({ message: String(reason?.message ?? reason), stack: reason?.stack });
});
unhandledrejection is often forgotten, yet an un-awaited fetch failure lands exactly there.
Frameworks swallow their own errors, so you need their hooks too:
// React: errors during render
class ErrorBoundary extends React.Component<{ children: React.ReactNode }, { failed: boolean }> {
state = { failed: false };
static getDerivedStateFromError() { return { failed: true }; }
componentDidCatch(error: Error, info: React.ErrorInfo) {
report({ message: error.message, stack: error.stack, componentStack: info.componentStack });
}
render() { return this.state.failed ? <FallbackScreen /> : this.props.children; }
}
// Angular: application-wide errors
@Injectable()
export class ReportingErrorHandler implements ErrorHandler {
handleError(error: unknown) {
report(normalize(error));
console.error(error);
}
}
// providers: [{ provide: ErrorHandler, useClass: ReportingErrorHandler }]
Sending the error
An error can happen while the page is closing, and a normal fetch gets cut off. sendBeacon completes the request even during unload:
function report(payload: Record<string, unknown>) {
const body = JSON.stringify({
...payload,
release: import.meta.env.VITE_RELEASE,
url: location.pathname,
userAgent: navigator.userAgent,
});
navigator.sendBeacon('/api/client-errors', body);
}
If you use a hosted service (Sentry and the like), its SDK provides this layer; but knowing what it collects helps you configure it properly.
The "Script error." message
For security reasons, an error in a script loaded from another origin (a CDN) is reported by the browser only as Script error., with no line and no stack. The fix has two parts: add crossorigin="anonymous" to the script tag and make sure the CDN returns an Access-Control-Allow-Origin header.
Source maps: making the unreadable stack readable
A typical stack from production:
TypeError: Cannot read properties of undefined (reading 'id')
at Kf (index-3f2a9c.js:1:48213)
Without a source map this line tells you nothing. In Vite:
// vite.config.ts
export default defineConfig({
build: { sourcemap: 'hidden' },
});
'hidden' generates the .map files but does not append the sourceMappingURL comment to the JavaScript. The browser never looks for the map and your source code is not served publicly. The flow should be:
- The build generates
.mapfiles. - CI uploads them to the error tracking service with a release tag.
- The
.mapfiles are deleted fromdistbefore deploying.
npm run build
upload-sourcemaps --release "$GITHUB_SHA" dist/assets # your tool's command
find dist -name '*.map' -delete
To add these steps to a pipeline, see the GitHub Actions frontend CI post.
A release tag is mandatory
Every error report must say which build it came from. Otherwise the source map is applied to the wrong file and the stack points at the wrong line. Passing the commit SHA as an environment variable at build time is enough: VITE_RELEASE=$GITHUB_SHA npm run build. The release tag also answers "which deploy did this error start with?" at a glance.
Filtering the noise
Most of the raw error stream has nothing to do with your code:
| Noise | Cause | What to do |
|---|---|---|
Errors from chrome-extension:// | Browser extensions | Filter by source |
ResizeObserver loop completed with undelivered notifications | Usually a harmless browser warning | Ignore it |
AbortError, Failed to fetch | User navigated away, network dropped | Count as a separate metric |
| Errors specific to old browsers | Versions you do not support | Separate by your supported browser list |
The same error arrives thousands of times; group by message and the first application frame and watch the count. The trend matters, not each individual record.
ChunkLoadError after a deploy
Every app that uses code splitting hits this scenario: a user leaves a tab open, you ship a new version, the old hashed chunks are deleted and the dynamic import fails when the user navigates. Vite emits an event for this case:
window.addEventListener('vite:preloadError', (event) => {
event.preventDefault();
if (!sessionStorage.getItem('reloaded')) {
sessionStorage.setItem('reloaded', '1');
location.reload();
}
});
The sessionStorage check prevents an endless reload loop on a genuinely broken deploy. Another safeguard is keeping the previous version's files on the server for a few deploys.
Privacy
Form values, tokens or email addresses can leak into error reports. Strip query parameters from URLs, attach the user only as an anonymous ID and mask sensitive field names in the report body.
Conclusion
Frontend error monitoring has four parts: catching errors at every entry point, sending them reliably, making them readable with source maps and release tags, and filtering the noise. When the stack alone does not explain why an error happened, session replay with rrweb, which shows what the user did up to that moment, is a good complement. For the server-side counterpart, see the Node.js API error handling post.


