SANTA CLARA, Calif. (AP)The first half of the season has been a bit of a roller coaster for the San Francisco 49ers as they try to build on last season’s trip to the NFC title game.

They have endured the lows of a season-ending ankle injury to quarterback Trey Lance in Week 2, a blowout defeat to Kansas City and lackadaisical losses to teams like Chicago, Atlanta and Denver.

But there also have been some bright spots, highlighted by two convincing wins over the division rival Los Angeles Rams, that give the Niners (4-4) confidence that they can go on a second-half run and still be one of the top contenders in the NFC.

”You just want a chance to get into the tournament and with where we stand now, I still feel we have a very good chance of that,” coach Kyle Shanahan said before the bye week. ”We have to play good football here going forward. The thing that you want most with your team is that you’re getting better as the year goes.”

The 49ers went into the bye off their best performance yet with midseason addition Christian McCaffrey sparking the offense with a TD run, catch and pass, and the usually dominant defense looking that way in the second half of a 31-14 win over the Rams.

With the exception of a two-game stretch against the Falcons and Chiefs when injuries played a role, the Niners have been one of the top defenses in the league, ranking sixth in points allowed (18.4 per game) and third in yards per play allowed (4.75).

The offense has been more inconsistent but showed some positive signs against the Rams, with McCaffrey offering a new element and quarterback Jimmy Garoppolo playing perhaps his best game of the season.

Building off that performance will play a big role in determining how far this Niners team is capable of going a year after falling one game short of the Super Bowl.

WHAT’S WORKING

Pass rush. Led by 8 1/2 sacks from Nick Bosa, the Niners have had one of the best pass-rushing performances in the league even with defensive tackle Arik Armstead missing five games with injuries. San Francisco is tied for fourth with 26 sacks and has gotten good contributions from complementary players like Samson Ebukam (3 1/2 sacks), rookie Drake Jackson (three sacks) and Charles Omenihu (three sacks).

WHAT NEEDS HELP

Deep passing. One of the reasons the Niners decided to move on from Garoppolo and turn the team over to Lance was to add more of a threat from the deep pass. While Garoppolo has played well at times since regaining his starting job, his issues in the deep game are still evident. He has completed just 5 of 18 passes at least 20 yards downfield with one interception. He doesn’t have a TD on a pass at least 30 yards downfield since 2019.

STOCK UP

WR Brandon Aiyuk. The third-year receiver has emerged as San Francisco’s most reliable outside threat. While Deebo Samuel may be more versatile with his ability to run out of the backfield and turn short passes into long gains, Aiyuk has been more consistent. He leads the team in receptions (38), yards receiving (483) and TD catches (four). He has 21 catches for 246 yards and three TDs in his last three games.

STOCK DOWN

WR Danny Gray. The third-round rookie was supposed to bring the speed element to San Francisco’s passing game but has been a non-factor so far. He has played only 33 offensive snaps in the five games he has been active and has no catches on six targets so far this season.

INJURIES

The Niners are hoping to get several players back after the bye week, including WR Samuel, WR Jauan Jennings, FB Kyle Juszczyk, RB Elijah Mitchell, CB Jason Verrett, LB Dre Greenlaw and LB Azeez Al-Shaair. The prognosis for Armstead and DT Javon Kinlaw is less clear.

KEY NUMBER

11. The Niners have turned the ball over 11 times in their four losses, with three against Kansas City, Atlanta and Denver, and two against Chicago. San Francisco has just one giveaway in the four wins. Since Shanahan took over as coach in 2017, San Francisco has committed the second-most turnovers in the NFL with 145.

NEXT STEPS

The 49ers host the Chargers on Sunday night. They don’t need to travel much the rest of the season, with six of their final nine games at home and only one trip out of the Pacific Time Zone when they play Arizona in Mexico City on Nov. 21.

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