Two days before the U.S. Open last August, Roger Federer, done with a series of promotional appearances in New York, took a sad flight back home to Switzerland.

As the Open labored on without the Swiss Maestro for the first time since 1998, Federer and his achy knees hiked in the Swiss Alps with his wife, Mirka, and four young kids.

He contemplated retirement.

Instead, according to sources, he found a new perspective in the altitude. And now the mountain Federer has climbed in 2017 is to the top of the tennis world. Again.

“Nobody’s doing what he’s done before at his age,’’ Brad Gilbert, one of ESPN’s top tennis analysts, told The Post. “He’s putting together the greatest year in the history of tennis, considering his age. I won’t speculate about his retirement. He’s playing phenomenal tennis. We need to appreciate it, run with it and see how far it goes.’’

Flushing Meadows is getting ready to host the Roger Renaissance. Federer, who turned 36 on Aug. 8, goes for his 20th Grand Slam title when the U.S. Open begins Monday.

He has won two majors in 2017 — the Australian Open and Wimbledon — to add a couple more cherries on top of his Slam Sundae record of 17. He also established a record with his eighth Wimbledon title, surpassing Pete Sampras and William Renshaw of the late 1800s.

All this new glory coming after a 2016 campaign ruined by left-knee surgery — the result of a freak accident while running a bath for his kids. During his 2016 Wimbledon semifinal loss to Milos Raonic, Federer aggravated the knee, prompting him to shut it down for the rest of the year, giving himself even more time than doctors recommended.

“I don’t like to compare [his form now] to a few years ago and it is difficult to,’’ his longtime coach, Severin Luthi, told The Post. “Most important for us is that Roger is still hungry and willing to improve every day. After everything he has done and already won, the results this year have been amazing.’’

Flushing travails continue for Federer, who hasn’t won the U.S. Open since 2008 when he won the most recent of his five straight Open crowns. He snuck into New York last week after pulling out of the final major Open tune-up in suburban Cincinnati nursing a tweaked back suffered in his finals loss in Montreal.

Sources say he rehabbed the back last week here and was to return to the practice court this week.

“He’s had this before,’’ his agent, Tony Godsick, said. “Hopefully his back will respond and if everybody’s healthy, he has as good a chance as anyone else. He’s never played under the new roof and he’s the best indoor player in the world if that happens.’’

Federer has mastered the art of smart rehab. After knee surgery in February 2016, he was forced out of last year’s French Open, snapping a streak of 65 consecutive slam appearances.

He returned for Wimbledon and limped away a loser. Knee specialists told Federer he’d need six weeks of not playing and he could return for the Open. Federer wasn’t in a rush, and patience paid off with this magical season.

“He told them, ‘What if I gave you 12 weeks?’’’ Godsick recalled. “He was able to work on his quad stuff, strengthening his legs, his core, other things you’re not able to do as an active player. He was eager to come back. He missed tennis. Sixth months off and it helped physically and mentally after all the years of just going and going.

“He’s never want to go out of the game because of injury. He’s always wanted to go out on his terms.’’

Winning the Australian — his first slam after being stuck on 17 for five years — to open the year was grandiose enough. To do so by rallying against rival Rafael Nadal from 1-3 down in the fifth set to win 6-3 provided him a confidence boost that has yet to burst.

It marked the first time Federer had beaten Nadal in the finals of a major since 2007. Though Nadal will be the Open’s No. 1 seed, he trails Federer by a fat four slams.

In March, in the two biggest U.S. hardcourt events outside of the Open, Federer pummeled Nadal at Indian Wells and again in the finals of the Miami Open.

“Down 1-3 in Australia, that was the best five games I’ve ever seen Roger play,’’ Gilbert said. “It was an amazing field in Indian Wells and Miami and he went through it, hitting his backhand better than he’s ever had. I don’t think I ever saw him play better than at Indian Wells. He’s still moving like a genius at 36. You drop a dime in the service box and he’s hitting it.’’

Indeed, Federer’s backhand, always artistic if defensive in its sliced mastery, has become more aggressive, taking it earlier and with more vigor.

“In Australia, Roger was very successful with his aggressive returning and his backhand, which he played very close to the baseline,’’ Luthi said.

Federer’s new racket, the “RF 97’’ provides 97-square inches at its head — eight more than his previous stringed weapon, granting him extra explosiveness. “For sure it’s helped him in this area,’’ Luthi said.

Federer decided to sit most of the clay-court season this spring, tried to return before the French, but realized it strategically foolish. Godsick admitted it would have “taken a Herculean effort’’ to win it after such little prep on the red dirt. Instead, he honed in on Wimbledon history.

“He scripted the whole season like a genius, takes the clay-court season off, gets ready for grass and doesn’t drop a set,’’ Gilbert said.

After romping over Marian Cilic in the finals, Federer’s eyes filled with tears.

“I honestly didn’t think I was going to be able to run through top-10 players the way I am, win all these breakers, win all these big moments,” Federer said afterward. “I’ve won all the big matches this year. It’s unbelievable.”

Luthi doesn’t think his aim was to distance himself forever from Rafa in slam titles.

“Roger doesn’t think that way — to win a Grand Slam is motivation enough for him to keep on going,’’ Luthi said. “He doesn’t compare himself too much with the others. He makes sure that he is on the highest possible level and he knows what a privilege it is to play in and still win Grand Slams.”

The storybook may or may not carry on into Flushing, but with Novak Djokovic and Stan Wawrinka out and a hurting Andy Murray rusty, the first-ever Federer-Nadal Open showdown could be in the offing.

Though now Federer has new injury to overcome.

“He didn’t call for a trainer, but you could tell in the second set he wasn’t moving in Montreal,’’ Gilbert said. “He’s 36. You don’t recover quite as quickly as 25.’’

Federer vowed to defend his Wimbledon title next July. If this is his last Open, Godsick would be surprised.

“Even if he wins in New York, I don’t see him calling it over,’’ Godsick said. “He still has more tennis — couple of more years’’