The Yankees set an MLB record by hitting a homer in a 28th straight game on Tuesday.

And in a 4-3 win over Toronto, they needed all the power they could get, using four solo homers to provide just enough offense in The Bronx.

They didn’t waste any time, as DJ LeMahieu led off the bottom of the first with a homer to break the mark they shared with the 2002 Rangers, sending the Yankees to their 10th win in their last 11 games.

“We got off to a great start and weren’t able to pile on,’’ Aaron Boone said. “We couldn’t get that [big] hit and made outs on the bases. It wasn’t perfect tonight, but the long ball played a huge role.”

That’s been the case all year.

Aaron Judge followed LeMahieu against left-hander Clayton Richard with an opposite field shot into the seats in right to give the Yankees a quick 2-0 lead. It was Judge’s first homer since missing two months with a strained left oblique.

Gleyber Torres and Edwin Encarnacion also went deep, but Toronto scored twice in the sixth and Aroldis Chapman nearly faltered in the ninth, allowing a run before getting Freddy Galvis to ground out with two runners on to end it.

Giancarlo Stanton left the game after being tagged out by Richard, who was covering third base when Stanton made an awkward slide. Stanton was diagnosed with a right knee contusion after departing in the fourth and was replaced in left field by Brett Gardner.

Chad Green — who tossed a scoreless inning as the opener — Nestor Cortes Jr., Tommy Kahnle, Stephen Tarpley, Zack Britton and Chapman held Toronto down for most of the night, as the Yankees improved to 7-0 when using an opener.

The success they’ve had with the unorthodox pitching strategy is due largely to the offense, which has mostly pounded opposing pitchers when Green has taken the mound in the first inning.

They seemed poised to do that again Tuesday, with a chance to extend the lead in the fourth after Luke Voit singled with one out and moved to second on a deep fly ball by Encarnacion. Aaron Hicks singled to left, but third-base coach Phil Nevin made a bad read and waved Voit home, where he was thrown out easily by Lourdes Gurriel Jr.

Green and Cortes didn’t allow a base runner until Cavan Biggio led off the fifth with a double to right. Cortes responded by retiring the next three batters to preserve the shutout.

That changed in the sixth, as Toronto scored a pair of runs to cut the Yankees’ lead to one run before Kahnle came in and gave up a single to Gurriel, but struck out Biggio and Teoscar Hernandez to end the threat with the Yankees still up by a run.

The Yankees threatened off Richard in the bottom of the inning, loading the bases with two out, but Hicks flied to right.

Richard gave up just the three runs on three solo homers before being replaced in the seventh by ex-Yankee David Phelps.

Britton tossed a perfect eighth and Encarnacion hit the Yankees’ fourth solo homer in the bottom of the frame to make it 4-2 before Chapman entered.

Chapman gave up a single to Gurriel and fired a wild pitch before Biggio reached on catcher’s interference. Chapman struck out Hernandez and Rowdy Tellez for the first two outs before Randal Grichuk singled in Gurriel to make it 4-3, but he held on for the save.

“We all knew what was kind of on the line,’’ LeMahieu said of the homer record. “It’s pretty cool. … From 1-through-9 [in the lineup], you have a pretty dang good hitter. There’s no easy outs. The middle of the lineup is pretty scary.”