I write a distraction log. I write things down on a physical notebook whatever is coming to my mind either as a list or just phrases. It helps immensely sometimes. Other times I just give in to the distraction.
The way I do it is I have CODE open, with a today.md and every time a thought comes up when I'm either working or learning I just go to that document and I write it down (as a sort of IOU to pay later) while the desire to check that is strong at that very moment, when I come back to that document after ~2hours, it's something so trivial, that I end up mostly not even look at it.