It's not dangerous to get it right after, but if one isn't immunocompromised, the sweet spot is to wait 2-3 months for some affinity maturation to occur. Getting it immediately after an infection would blunt the booster's effect to some extent. Same goes for not spacing 2 boosters enough apart.