Is it possible to redirect if any request in the waterfall has the status 404?

How can we “monitor” if any request during page load had a 404 status and redirect to the same page but with a query parameter: /?nocache=1 ?

Is this done with Workers or Javascript on the site itself?