Send 404 status and redirect to component 404 in the responder router

I have a response-router 3.0.0 setup with the NotFound component, which I show as a backup:

<Route component={NotFound} path="*"/> 

However, this returns a redirect, and the google crawler complains about soft 404s. Can I redirect to my NotFound component and send 404 like Github does? I tried the following sentence from here: How to respond to the router response to the 404 status code?

 <Route component={NotFound} path="*" status={404}/> 

But that just gives me 404 without displaying the component.

How can the above be done?

+5
source share
1 answer

Just to close the loop on this - I didn’t want to go on the server side rendering track, so I applied the following in my middleware error handler:

  if (err.name === 'UnauthorizedError') { res.status(401).send(prepareErrorResponse(isXhr, 'Acess Denied')); } else if (err.status === 404) { res.status(404).send(prepareErrorResponse(isXhr, 'Not Found')); if (req.accepts('html')) { // Respond with html page. fs.readFile(__dirname + '/../404.html'), 'utf-8', function(err, page) { res.writeHead(404, {'Content-Type': 'text/html'}); res.write(page); res.end(); }); } else { if (req.accepts('json')) { // Respond with json. res.status(404).send({ error: 'Not found' }); } else { // Default to plain-text. send() res.status(404).type('txt').send('Not found'); } } } 

Basically, instead of showing the NotFound component on the client (with soft 404), I serve the NotFound page directly from the server (with status 404).

0
source

Source: https://habr.com/ru/post/1264213/


All Articles