Re-connect wifi after wifi loss

I have Pi Pico W’s and Node-Red MQTT for building neat dashboards. Unfortunately, my internet often stops and that can end up crashing the software once it sends too many publishes with nothing at the other end.

I’ve been searching for a while to find a graceful way to detect the internet loss and restoration, then reconnect and test that Node-Red is up & running before publishing anything else?

My projects need to be able to continue running even with no internet, so any clues you have provide would be helpful.

I did figure out how to recover from a power loss, but not gracefully. I don’t want to always have to restart the microcontroller.