Next.js site 404s are triggering a "Next.js SSR handler" function invocation

Hi,

I have a Next.js site running at celebrated-lollipop-2ef174.netlify.app and have received a warning email telling me that I’ve used 50% of my monthly function invocations.

Looking at the function logs, these all appear to be to the “Next.js SSR handler” function, and the URLs listed are for files and pages which don’t exist. The majority are to old image paths (/files/images/*) which no longer exist since a site rebuild (presumably from websites which are hotlinking my images) but there are also lots to paths like /wp-admin.php and /xmlrpc.php which are also non-existent pages.

I have two questions:

  1. Is it expected behaviour for these pages to be invoking a function call? I’m guessing there’s something about Next.js’s 404 page which requires a function invocation on every request?
  2. Is there a way I can prevent the function invocation?

As a workaround for the missing images I’ve set up a rewrite from the legacy path to a placeholder image:

[[redirects]]
  from = "/files/images/*"
  to = "/netlify-redirects/image.png"
  status = 200

This seems to have stopped the function invocations and it wouldn’t be too hard to set up something similar for the other requests in the list, but is this the correct approach or is there a better way? Ideally I’d like to show the Next.js 404 page but without the function invocation.

Many thanks,

Pete

Hi @petewritescode,

Unfortunately, this is currently true. Due to some platform limitations which are being worked on, any asset that’s supposed to throw a 404 should first go through the handler function in order to let Next.js run and show output.

Your approach of redirecting to a different image is great.

Hi @hrishikesh, thanks for confirming. Great, the workaround has solved my issue for the time being anyway, but good to know a more permanent solution is on the way.

Hi @hrishikesh. Are there any solutions to this issue yet? A site on my account is getting spammed with all kinds of different paths, triggering this function. It’s not possible to add redirects for all of them.

No solution so far, I’m afraid. The platform-level solution would take some time to be released. Till then, if you’re sure about some specific paths that are being exploited, you can add a forced 404 redirect for those.

Hello @hrishikesh,

With the 404 going through the handler function, I am facing a problem where a website that I built has a considerable number of requests because a bot was trying to access non-existing URLs on the website which makes the handle function called multiple times and adds unnecessary extra costs.

any suggestion about this?

which of your sites is impacted?

this site: caa21bfa-f8af-4f4f-be1d-502b75f8fa94

@Ala, this has already been answered above:

this bot keeps generating random URLs, so it’s impossible to block them manually using redirects.

There’s no current solution then. If you have a list of expected paths, you can redirect others using Edge Functions or Next.js middleware.

For example, if you know the URLs would be like:

/users/
/api/
/blog/

you can redirect the rest using Edge Functions.