The reigning champion squeezed into the quarter-finals after eventually defeating the 28th-ranked Spaniard Roberto Bautista Agut 6-3, 2-6, 6-4.

The top seed will play Japan's eighth seed Kei Nishikori, while Novak Djokovic and Alexander Zverev are also through, albeit with far less hassle.

The 37-year-old Swiss great was forced to fight in his opening match on Wednesday against Russia's Daniil Medvedev. Normal order appeared to have been restored when the 20-time Grand Slam champion comfortably claimed the first set against Bautista Agut. But the Spaniard earned a piece of personal history after that, winning the second set -- the first time he had taken a set against the Swiss maestro.

The 30-year-old Bautista Agut had the whiff of an upset in his nostrils and the prospect of a first victory over the Swiss at the eighth attempt. But in the ninth game of the deciding set, Federer made the breakthrough on his opponent's serve, pumping his fist as another hard-won victory loomed into view.

Federer's wife Mirka and their four children were in the arena and he said afterwards: "I'm not sure if they give me more courage fighting on court, but they were here today. "I was telling myself, particularly tonight I cannot do this (lose) because they were there, but I don't know how much I draw from that, quite honestly."

Federer said that two stern encounters had set him up nicely for Nishikori on Friday. "I hope it's going to help me that I played against Medvedev and also Bautista Agut, two really good baseliners. "From that standpoint, I feel like I found a rhythm."

One player who will not be in the quarter-finals is Juan Martin del Potro after he hobbled out of the tournament. The Argentine world number four, who had already been suffering with a virus, lost his footing at 5-5 in the first set against Croatia's Borna Coric. The 30-year-old Del Potro had lengthy treatment and strapping put on his knee, but conceded the match after losing the first set 7-5.

Coric, the 13th seed, faces Australia's unseeded Matthew Ebden next Second seed Djokovic enjoyed a measure of revenge as he booked his spot in the quarter-finals with a 6-4, 6-0 demolition of Marco Cecchinato. The Serbian was stunned by the Italian -- ranked 72 in the world at the time -- in the French Open quarter-finals earlier this year.

It was part of a wretched run of form Djokovic suffered after elbow surgery in February. But he exploded back to life mid-season, winning Wimbledon, the Cincinnati Masters and US Open. The 31-year-old says he is back close to the kind of blistering form that has brought him 14 Grand Slams.

Djokovic will play South Africa's seventh seed Kevin Anderson and said he was arrowing in on Rafael Nadal's top ranking. The Spaniard is not in Shanghai due to injury. "Obviously, I'm going to try to do everything I can to get to number one," said Djokovic. "That's one of the -- probably the -- biggest goal of the end of the season."

Rising star Zverev was similarly emphatic as he brushed aside Australian teenager Alex de Minaur, 6-1, 6-4. The German fourth seed will next play 11th seed Kyle Edmund.