0

I want to have a /status page that returns a 200 status code.

I create a route along with a page, however when I access it I see a 304 code as the response.

Any ideas how I can set the route to just return a 200.

    <Router>
      <Layout>
        <Routes>
          <Route path="/status" element={<AppStatus />} />
        </Routes>
      </Layout>
    </Router>

I'm using v6 of the react-router-dom.

Any ideas?

Thanks.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
userMod2
  • 8,312
  • 13
  • 63
  • 115
  • 3
    `react-router-dom` has nothing to do with HTTP status codes sent from the server. The HTTP **304 Not Modified** client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304 – Drew Reese Feb 02 '22 at 06:32
  • I see so I'm using the sample react create app, I guess I need to specify in the `index.tsx` I can't see where any of the controlling server side code is. – userMod2 Feb 02 '22 at 08:41
  • Figured it out, using fastify-static to return the build directory, then create that status path return the code 200 – userMod2 Feb 07 '22 at 12:25

1 Answers1

0

React can be used on both single-page applications (for example create-react-app) and for server-side rendering (for example NextJS). A http status is sent once per http request.

This means that if your app is a single-page application and you first go to / and then click a link to go to /status, then you won't make a new http request (the full app is loaded on the initial request to /) and so you will not get a http status at all when changing the path. If your initial top-level navigation is directly to /status, then the browser will download the app (like it does when you went to / in the last example) and a status code will be sent at the same time.

If the status code is 304, then it means that your app has been cached. To deal with this, force your backend to set cache headers on the request for the actual resource (the html entrypoint) which indicate that the response must not be cached.

If you're using server-side rendering, then, depending on the framework, you could trigger the /status path to be dynamic and require rerendering on the backend every time, in which case you will get a 200 status every time if nothing unforseen happens.

Are you trying to configure a health endpoint for a load balancer or something?

fast-reflexes
  • 4,891
  • 4
  • 31
  • 44