AEM React SPA create custom error page name
Is it possible to create custom error pages instead of 400 OR 500 for e.g. 400-error, 500-error?
If yes, can it be done without needing a redirect rule in dispatcher configuration?
Is it possible to create custom error pages instead of 400 OR 500 for e.g. 400-error, 500-error?
If yes, can it be done without needing a redirect rule in dispatcher configuration?
Hi @kamal_kishor ,
Creating custom error pages in an AEM React SPA is possible and can be done without relying solely on redirect rules in the dispatcher configuration. Here's a structured approach to achieve this:
First, create the custom error pages in AEM under a designated path, e.g., /content/your-site/en/errors/400 and /content/your-site/en/errors/500.
Use Sling Resource Type Mappings to serve custom error pages for specific HTTP error codes. This configuration can be done via the OSGi console or by creating a custom Sling Error Handler.
OSGi Configuration:
/errors/400 = /content/your-site/en/errors/400
/errors/500 = /content/your-site/en/errors/500
Sling Resource Type Mapping:
Overlay the Error Handler:
Update sling:resourceType for the error pages:
<jcr:root
xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
sling:resourceType="your-site/components/errorhandler/400">
Ensure your React SPA is configured to handle these error routes appropriately.
React Router Setup:
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Custom400ErrorPage from './components/Custom400ErrorPage';
import Custom500ErrorPage from './components/Custom500ErrorPage';
function App() {
return (
<Router>
<Switch>
{/* Other routes */}
<Route path="/errors/400" component={Custom400ErrorPage} />
<Route path="/errors/500" component={Custom500ErrorPage} />
<Route component={DefaultErrorPage} />
</Switch>
</Router>
);
}
export default App;
Error Boundary Component:
import React, { Component } from 'react';
import Custom400ErrorPage from './components/Custom400ErrorPage';
import Custom500ErrorPage from './components/Custom500ErrorPage';
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false, errorCode: null };
}
static getDerivedStateFromError(error) {
// Update state to trigger fallback UI
return { hasError: true, errorCode: error.code };
}
componentDidCatch(error, errorInfo) {
// You can also log the error to an error reporting service
console.error('Error caught by ErrorBoundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
if (this.state.errorCode === 400) {
return <Custom400ErrorPage />;
} else if (this.state.errorCode === 500) {
return <Custom500ErrorPage />;
} else {
return <DefaultErrorPage />;
}
}
return this.props.children;
}
}
export default ErrorBoundary;
Use ErrorBoundary in your App:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import ErrorBoundary from './ErrorBoundary';
ReactDOM.render(
<ErrorBoundary>
<App />
</ErrorBoundary>,
document.getElementById('root')
);
By following this approach, you can create and manage custom error pages for your AEM React SPA without relying on dispatcher rules. This ensures a more streamlined and maintainable setup.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.