on the way back; while returning
Describes an action or event that occurs during the return journey. Often used adverbially with に.
I stopped by the convenience store on the way back.
when about to go back; just before leaving
Refers to the moment just before departing to return. Often used in set phrases like 帰りがけに一言.
I said 'thank you' just before leaving.
Standard spelling using がけ as okurigana.
Alternate spelling with 掛け; less common but still encountered.
Compound of 帰り (return) + がけ (suffix meaning 'on the way' or 'just before'). The suffix がけ derives from the 連用形 of 掛ける, indicating an action in progress or about to happen.