This (naive) example demonstrates how to create a gradient that follows a stroke or path. This method breaks the stroke into segments and uses stroke-linecap: round;
to make the segments overlay and appear connected. The naive approach might useful if your stroke or path is relatively simple and you do not need a high level of precision. See Gradient Along Stroke (Naive) Spiral for same logic but spiral layout.
For a more robust solution see Mike Bostock's miter joint method demonstrated in Gradient Along Stroke.