LONGWY, France (AP) - Shaking off an equipment glitch in the furious finishing sprint, Peter Sagan bagged his eighth career Tour de France stage victory on Monday with an impressive display of power and quick-thinking on a short, sharp final uphill dash.

Geraint Thomas of Team Sky retained the yellow jersey he's held since the opening stage in Germany, as the race swung into France, to the former steel town of Longwy. But he is not planning to hold onto it for ever: The team's goal is for three-time champion Chris Froome to be wearing the jersey when the Tour rolls into Paris on July 23.

Thomas and Froome got through Stage 3 unscathed, the main goal for them and others eyeing overall victory rather than stage wins.

The pack of riders stretched like a piece of string into single file on the fast, winding downhill into Longwy before the final climb on Nuns' Hill that seemed tailor-made for the strengths of Sagan, the world champion.

Australian rider Richie Porte, a contender for overall victory, showed he's in fearsome form by powering away from the pack in the first stages of the climb. But Sagan was watching closely behind him and never let Porte get too far ahead.

Sagan seemed to be cruising to victory until his right foot slipped out of his pedal in the last few hundred meters.

"I said to myself, 'What's happening?'" the Slovak star of the German Bora-Hansgrohe team said.

But he quickly recovered without losing speed, clipping his foot back into place and holding off to the line Michael Matthews, an Australian with Sunweb, and Dan Martin, an Irishman with Quickstep.

Froome was ninth on the stage, showing no apparent side effects from a crash on wet roads on Sunday that shaved skin off his buttocks. He moved up from sixth to second overall, 12 seconds behind Thomas.

Although a mere speed bump compared to the mountain ascents to come in the Alps and Pyrenees, the 1.6-kilometer (one-mile) uphill in Longwy, with a short very steep section, was testing enough to give an early inkling of the fitness of some of the top riders. Porte, who used to be a teammate of Froome's at Sky before switching to BMC, impressed with his attack.

"It showed that he's in great form and will always look to use his legs to gain some time," Thomas said.

Porte finished the stage in the same group as Froome and Thomas - two seconds behind Sagan and other riders who went all out for the stage win. Porte, who is 35 seconds behind Froome overall, was pleased to test his legs.

"It's good for the confidence just to have a bit of a crack," Porte said.

Other overall contenders who finished in the same group included Nairo Quintana, a three-time podium finisher, former two-time winner Alberto Contador, and Romain Bardet, runner-up last year to Froome. A sterner test of their relative strengths will come on Wednesday on a tougher climb to the Planche des Belles Filles ski station.

For the moment, their main goal is not crashing and losing time. That was easier said than done as the pack picked up the pace toward the end of what otherwise was a fairly leisurely 212.5-kilometer (132-mile) ride through the dense forests and plunging valleys of the Ardennes, from Belgium, through Luxembourg to France.

The last hour "was a hell of a fight," Porte said. "It was quite a dangerous tricky final, so it's nice to get through that one unscathed."

With his long hair, quick humor and taste for showmanship, Sagan is one of the Tour's most colorful characters. His ultimate goal is to win, for a sixth consecutive time, the green jersey. It isn't as coveted as the yellow one but is still a solid measure of overall strength.

For the moment, however, Sagan planned to allow himself a celebratory drink.

"You've got to celebrate every victory," he said. "But if I drink alone they call me a drunkard. I prefer to drink with my teammates."