Spoilers below for Wednesday’s episode, “Chapter Fifty-Eight: In Memoriam.”

“Riverdale” revels in the ridiculous — plots involving stick-monsters, teen-boy fight clubs. So when cast member Luke Perry died suddenly in March, it didn’t seem like the show could be up to the task of writing off his character in a sensitive way that fits the weight of the tragedy.

Turns out it was.

Season 4 kicks off with a stand-alone episode that serves as a simultaneous eulogy for Perry — who died March 4 of a stroke at age 52 — and his character Fred Andrews, father of Archie (KJ Apa).

At the beginning of “In Memoriam,” Archie hears about Fred’s death via phone call. We don’t hear the audio but we see Archie react, dropping the phone. The scene cuts and we hear the news from FP Jones (Skeet Ulrich), the town sheriff and Fred’s friend: the elder Andrews was struck in a hit-and-run accident when he stopped by the side of the road to help a fellow driver having car trouble.

“[It was] literally almost impossible to utter lines,” Ulrich recently told the Post. “The emotion you try to bury from the four months prior of [Perry’s death] happening until we’re asked to re-examine it in the episode…one of the hardest experiences I’d had on film.”

The driver Fred died saving is played by Shannen Doherty, Perry’s former “Beverly Hills 90210” co-star and longtime friend. “Rather than having a nameless extra playing that part, we reached out to Shannen,” showrunner Roberto Aguirre-Sacasa told The Post.

The episode follows everyone reacting, congregating at Archie’s house to reminisce about Fred and calling him a hero. When Archie declares he has to “bring Dad home,” he and his friends — Veronica (Camila Mendes), Jughead (Cole Sprouse) and Betty (Lili Reinhart) — drive to retrieve Fred’s remains. Afterward, they visit the site of the accident, where they meet Doherty’s character paying her respects, and she leads Archie and the gang as they say a roadside prayer.

Molly Ringwald, who plays Mary Andrews (Fred’s ex-wife), echoed Ulrich’s feelings about filming the episode. “I pretty much cried from beginning to end,” she said. “It was like saying goodbye to him and dealing with the sadness of that.”

After a brief subplot in which he tries to track down and attack the driver who hit Fred, Archie brings his remains back to Riverdale on Independence Day. They’re greeted by the entire town holding signs that read, “Welcome home Fred.”

At the end, everyone comes together for a funeral scene, and Archie delivers a eulogy: “It hurts me that I never got to say goodbye, but his spirit and memory live on.”

The episode fades out with an epigraph screen reading, “In loving memory of Luke Perry.”

It’s a fitting and moving tribute. And for a show with evil board games and cults, it’s surprisingly tasteful and solemn.

“Riverdale” airs Wednesdays at 8 p.m. on the CW.