SANTA CLARA, Calif. (AP) — Christian McCaffrey ran for the go-ahead touchdown with 7:54 to play, and the San Francisco 49ers beat the Los Angeles Chargers 22-16 Sunday night in their first game back from a bye week.

The Niners (5-4) returned from the midseason break as healthy as they’ve been all season on offense and with hopes of making a second-half run. Their performance against the short-handed Chargers (5-4) was a slog at times but proved to be just enough to come out on top.

San Francisco fell behind by 10 points in the second quarter before rallying. Jimmy Garoppolo scored on a 1-yard sneak in the second quarter and then made the biggest play when he hit Brandon Aiyuk on a 24-yard pass on third-and-10 from the Chargers 26 midway through the fourth quarter.

McCaffrey ran it in on the next play and has now accounted for four touchdowns in three games since arriving from Carolina in a midseason trade.

Garoppolo went 19 for 28 for 240 yards while San Francisco relied heavily on the ground game, rushing 40 times for 158 yards.

Justin Herbert went 21 for 35 for 196 yards with a touchdown pass for the Chargers, who were playing without both starting tackles and receivers.

After scoring a touchdown on their opening possession, the Chargers managed just three field goals — two on drives that started in Niners territory — the rest of the way against a stout San Francisco defense.

The Chargers failed on two late chances, They took over at their 1 with 2:03 to play but turned it over on downs when Herbert’s pass on fourth-and-3 from his own 8 went off Joshua Palmer’s hands for an incompletion.

After holding San Francisco to a field goal, Herbert got another chance with 53 seconds to go but was intercepted by Talanoa Hufanga on the first play to seal the game.

San Francisco held the opposition scoreless in the second half for the second straight game.

FAST START

The Chargers flipped the script from their recent games, when they became the first team since 1988 to trail by at least 10 points following the first quarter in four straight outings. Herbert completed all four passes on the opening drive, capped by a 32-yard TD strike to DeAndre Carter.

Los Angeles then held the Niners to a field goal after a drive inside the 10 and led 7-3 after one.

EJECTED

Niners linebacker Dre Greenlaw was ejected late in the second quarter when he delivered a helmet-to-helmet hit on Herbert. Herbert was forced to leave the game to be checked for a concussion and Chase Daniel came in for the final three plays of the half. The Chargers’ drive stalled and they settled for a field goal that made it 16-10.

Herbert was cleared and returned at the start of the second half.

INJURIES

Chargers DL Otito Ogbonnia (knee) and TE Gerald Everett (groin) left in the first half and didn’t return. … Los Angeles DL Christian Covington (pectoral) left in the fourth quarter.

UP NEXT

Chargers: Host Kansas City next Sunday night.

49ers: Play Arizona in Mexico City on Monday, Nov. 21.