Personally, I do a little bit of both.
Sometimes, I'll write something, and not care to meticulously plan out the solo, so I'll just improv it when recording. Just so long as you know your fretboard well enough, you can map the parts of solos around the major chords of the progression. That is to say, if your progression is basic C - F - G, and you're playing in a C Major scale, you'll want to start a riff on the 8th fret of the A string when the progression turns to F to give a sense of cohesiveness. With enough practice, improv is a relatively easy thing to do.
Otherwise, I'll do something I like to call "hypnopaedic writing". In other words, I'll instantaneously develop a tune through my subconscious (I can't call it "thinking", because technically, I'm not) and try to transcribe it to the key I'm playing in. Once I've covered all the major sections of the solo, I'll work in transitions to make everything flow smoothly. Essentially, if you want a more organized, efficient solo, this is a good method. Plus, it doesn't require a lot of thinking.
Hope that helps.