Get this, add a couple of 18650 rechargeable lithium-ion batteries, and you have a UPS (Uninterruptible Power Supply) for your Raspberry Pi.
Now, when the power goes off, the batteries kick in and keep your Raspberry Pi running until you can figure out what's wrong. Once the power is restored, the batteries will recharge, ready for the next power drop.
This one works on the Raspberry Pi 4 Model B, 3B+, and 3B. If you want something similar for the Raspberry Pi Zero, then a HAT like this is a good choice.