question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Way to return 404 http error code in render()

See original GitHub issue

Is 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:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

21reactions
adamsoffercommented, Nov 13, 2018

@kachkaev here’s a workaround…

import Error from 'next/error'

export const throw404 = () => {
  if (process.browser) {
    return <Error statusCode={404} />
  }
  const e = new Error()
  e.code = 'ENOENT'
  throw e
}
11reactions
shaunscommented, May 24, 2018

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 inside render as expected (in fact, same as the example Next.JS+Apollo code). So in getInitialProps I don’t have access to the [empty] data yet in order to return a 404 through res.

My current workaround is pretty gross:

<Query query={getResponses} variables={{ foo: this.props.bar }}>
  {({ loading, error, data }) => {
    // Check for error, loading etc.
    const responses = data.allResponses.nodes;
    if (responses.length === 0) {
      const e = new Error("Response not found");
      e.code = "ENOENT";  // Triggers a 404
      throw e;
    }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found