SANTA CLARA, Calif. (AP)Brock Purdy passed his latest test and now has the San Francisco 49ers in position for a possible playoff bye.

On a day when San Francisco’s usually stellar defense struggled against a backup quarterback, Purdy showed he can thrive just as much when coming from behind as he has playing with a lead.

Purdy had his fifth straight game with two TD passes and threw for 284 yards to help the Niners rally from a 10-point deficit in the second half to beat the Las Vegas Raiders 37-34 in overtime on Sunday for their ninth straight win.

”I don’t think anybody wants to make a game close or anything,” Purdy said. ”You always want to put up points and win in a comfortable way. But for us to go through that kind of scenario and for our offense and then even for myself, game is on the line, we have to move down the field and put up points and trust the defense, all that kind of stuff.

”I think it was great for seeing where we’re at in terms of what our goal is in all of this. It’s good for us to go through that.”

The 49ers (12-4) can clinch the No. 2 seed in the NFC with a win against Arizona next week or a loss by Minnesota. San Francisco also has a shot at the No. 1 seed and a bye if Philadelphia loses to the Giants in Week 18.

Purdy hadn’t taken a single snap on offense in the second half of a game with San Francisco trailing since taking over when Jimmy Garoppolo got hurt in the first quarter in Week 13.

But Purdy was unflappable after the Raiders took a 24-14 lead early in the third quarter. Purdy led San Francisco on four scoring drives on the next five possessions and then drove the 49ers into position for a potential winning field goal that Robbie Gould missed at the end of regulation.

San Francisco won it after Tashaun Gipson intercepted Jarrett Stidham to set up Gould’s game-winner.

That was one of the few big defensive plays from the Niners on a day when they allowed 34 points and 500 yards in their worst performance since a 44-23 loss to the Chiefs in Week 7.

”Obviously a ton to correct defensively, but for the offense to take control and win it for us was ginormous,” Bosa said. ”We needed that. I’m proud of them.

WHAT’S WORKING

Getting the ball to Christian McCaffrey. The Niners running back had 19 carriers for 121 yards and six catches for 72 yards and scored a TD for the fifth straight game. McCaffrey also generated nine first downs in the game, giving him a team-best 60 on the season despite only playing 10 games with San Francisco.

WHAT NEEDS WORK

Defending deep passes. Stidham completed five passes thrown at least 20 yards downfield for the most allowed in a game by the Niners since Josh Allen also had five on Dec. 7, 2020. The Raiders gained 177 yards and had two TDs on those plays.

STOCK UP

Offensive line. The Niners allowed no sacks for the fifth time this season with right tackle Mike McGlinchey doing a good job neutralizing Raiders star edge rusher Maxx Crosby. The line also helped San Francisco rush for 170 yards, marking the fourth time in six seasons under Shanahan that the Niners rushed for that many yards in a game without allowing a sack.

STOCK DOWN

Gould missed a potential winning field goal when his try from 41 yards on the final play of regulation went wide right. Gould has missed five field goals this season, with all of the misses coming on kicks from less than 50 yards. The last Niners kicker to miss that many from inside 50 yards in a season was David Akers with nine in 2012.

INJURIES

G Aaron Backs (knee, ankle) will likely miss the game this week, but could be back for the playoffs. … The Niners are awaiting more information on LB Dre Greenlaw’s back injury but he could play this week. … McCaffrey (ankle) is day to day. … WR Deebo Samuel (knee, ankle) and RB Elijah Mitchell (knee) will practice this week and could return Sunday.

KEY NUMBERS

2-33 – The Niners generated a rare double-digit comeback under coach Kyle Shanahan. This marked just the second in 35 tries since Shanahan took over that San Francisco won a game after trailing by at least 10 points in the second half. The other came in Week 18 against the Rams last season.

WHAT’S NEXT

San Francisco hosts Arizona on Sunday.

AP NFL: https://apnews.com/hub/nfl and https://twitter.com/AP-NFL