The pulpy, absurdist “Riverdale” faced a challenge in Wednesday’s season premiere, a tribute to late co-star Luke Perry.

“We wanted to be very respectful and honor both Luke and [his character] Fred Andrews,” says showrunner Roberto Aguirre-Sacasa, 46. “That suggested we should take a more grounded, character-based episode that allowed us to track the emotions.

“So it does still feel like ‘Riverdale,’” he says. “There’s a little bit of gothic foolishness with Cheryl [Madelaine Petsch] at the beginning, a bit of a crime investigation story and a weird dream sequence. [The episode] is in the vocabulary of the show, but we definitely wanted it to feel different.”

Perry, who died of a stroke in March at age 52, co-starred as Fred Andrews, father of Archie (KJ Apa). Wednesday night’s Season 4 opener, “Chapter Fifty-Eight: In Memoriam” (8 p.m. on The CW) is a stand-alone episode centering on Fred’s death and his family and friends’ reactions to his passing.

Molly Ringwald, who plays Mary Andrews (Fred’s ex-wife), says filming experience was like touching a raw nerve. “I pretty much cried from beginning to end,” she says. “It was like saying goodbye to [Perry] and dealing with the sadness of that.”

Skeet Ulrich, who co-stars as Fred’s friend, FP Jones, had a similar reaction. “[It was] literally almost impossible to utter lines,” he says. “The emotion you try to bury from the four months prior to [Perry’s death] happening until we’re asked to re-examine it in the episode … it’s one of the hardest experiences I’d had [to do] on film.”

Wednesday night’s episode will also feature a small but pivotal appearance from Shannen Doherty, Perry’s former “Beverly Hills, 90210” co-star and longtime friend.

“Luke had been trying to get her on ‘Riverdale’ for years, and the timing never worked out,” says Aguirre-Sacasa. “We sent her the script and she said, ‘I’d love to be a part of this.’ [The episode is] a meditation on what Fred meant to the town of ‘Riverdale’ and what Luke meant to the cast and crew — and the audience that grew up watching ‘90210’ and the audience that watches ‘Riverdale.’ ”

As difficult as it was to film, Aguirre-Sacasa says there was never a chance that the series would not acknowledge Perry’s passing. “We weren’t going to say Fred was on a business trip indefinitely, or never mention him again,” he says. “He’s such a huge part of the show, and of Archie’s story. We knew we’d have to meet this challenge head-on.”

“In Memoriam” could have aired at the end of Season 3 last spring, closer to Perry’s death, but Aguirre-Sacasa says they didn’t want to rush the episode.

“We decided we couldn’t do it earlier than Season 4,” he says. “We wanted time to figure out what the episode was going to be. Rather than start [Season 4] and interrupt it at Episode 3 or something, it felt like we should immediately address it.

“I’m so proud of this episode,” he says. “It really was cathartic for us to work on it — and I hope Luke’s fans like it and see the love that was poured into it.”