What to do, then?
1. Make sure you're strongly motivated. If you have strong motivation you'll get back to work soon after the distraction, instead of going down the rabbit hole of wikipedia articles or whatever.
2. Write everything down: what you're doing next, where you are, etc.. That way when you get distracted and then get back to work, you'll have an easier time getting back to work immediately: just read your notes.
Longer version here: https://codewithoutrules.com/2017/08/03/stay-focused/