Way to return 404 http error code in render()
See original GitHub issueIs your feature request related to a problem? Please describe. Right now you can return 404 http error code only in getInitialProps, which is not nice, since you have to fetch the data twice. Once in getInitialProps to return the right http response code and once in the component itself when using apollo, so you will have loading property etc.
Describe the solution you’d like With react-router-dom you can have a component like this:
import React from 'react';
import { Route } from 'react-router-dom';
const NotFound = () => {
return (
<Route render={({ staticContext }) => {
if (staticContext) {
staticContext.status = 404;
}
return (
<div>
<h1>404 : Not Found</h1>
</div>
)
}}/>
);
};
export default NotFound;
With this component, you can simply have your apollo HOC near the components and SSR returns the correct http codes without the need for getInitialProps.
It would be great to be able to return http codes from render()
Issue Analytics
- State:
- Created 5 years ago
- Reactions:11
- Comments:23 (12 by maintainers)
Top Results From Across the Web
How to let react router respond with 404 status code?
The HTTP error code is in the headers for serving a new page, while all the content is in a named <div> that...
Read more >HTTP Status Codes, Network and DNS Errors, and Google ...
See how different HTTP status codes, and network and DNS errors can affect ... A soft 404 error is when a URL that...
Read more >Error Request Failed With Status Code 404 Axios React
Successful requests return HTTP status codes in the 2xx range. ... get the resource that it requested. js 404; useEffect React Hook rendering...
Read more >FAQ - Express.js
How do I handle 404 responses? In Express, 404 responses are not the result of an error, so the error-handler middleware will not...
Read more >Django shortcut functions
Defaults to 'text/html' . status: The status code for the response. ... You can use the redirect() function in a number of ways....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kachkaev here’s a workaround…
Similar use-case – I use Apollo for GraphQL and I would like to return a 404 on server renders where the result of a query is (for instance) empty.
My
<Query>
lives insiderender
as expected (in fact, same as the example Next.JS+Apollo code). So ingetInitialProps
I don’t have access to the [empty] data yet in order to return a 404 throughres
.My current workaround is pretty gross: