I seem to have seen/heard both.
Japanese Language Stack Exchange is a question and answer site for students, teachers, and linguists wanting to discuss the finer points of the Japanese language. It only takes a minute to sign up.Sign up to join this community
They mean basically the same thing and are practically interchangeable.
Having said that, I feel 帰り道に is closer to “on my way back/home”. The focus here is not so much on “where” as “when”. You were on your way home (or back to some other place) when you saw whatever you did. Where is not important.
帰り道で, on the other hand, sounds like there is a concrete route you take to go home (or go back to some other place) and you saw whatever you did there.
The difference seems to become even smaller in the following pair.
This could be because 途中 doesn’t make one think of a concrete route as much as 道 does.
In the following pair, 道 has a more concrete feeling than in 帰り道, and this makes に not quite idiomatic.
On the other hand, only に is acceptable in the following.