Dynamic Path Before Rendering 404

Hi guys,

I have a little bit of a unique situation…

I have a Hugo website that works great, I would love to make a page fetch an API.

For example: If I go to page: example.com/NotFoundPage, I would love to have a page that catch the “NotFoundPage” so I can make an API call to another app. And all that without triggering the 404 page.

Is there is any way I can do that?

I don’t think that is possible the way you describe. But that’s not a Hugo issue.

There is no way to know in advance (e.g. at the build stage) what unfound pages you might need, only at client access time. That means the client has to send the request to the server and the server has to respond with a 404 error.

There are a couple of possible ways to get round this:

  1. If you have control of the server, you could prevent the 404 response by always responding with some other, known page.
  2. Otherwise, the only option is to use JavaScript in the client to watch for a 404 response and redirect to a known page. That could be done via an API call quite easily.