The Supreme Court on Wednesday ruled for a Pennsylvania cheerleader whose profane off-campus rant cost her a spot on the squad, saying the First Amendment rights of public school students are not to be easily cast aside.

The court ruled 8 to 1 that the school district’s punishment was too severe, although it declined to adopt a broader rule saying schools never have a role in disciplining students for off-campus speech.

“It might be tempting to dismiss B.L.’s words as unworthy of the robust First Amendment protections discussed herein,” Justice Stephen G. Breyer wrote in his 11-page majority opinion, using the initials of the minor in question.

“But sometimes it is necessary to protect the superfluous in order to preserve the necessary,” he wrote.

The short opinion, with Justice Clarence Thomas as the lone dissenter, was the court’s latest attempt to clarify the free-speech rights of the nation’s 50 million public school students, and a rare win for student speech.

“It marks the first time in more than five decades — all the way back to the Vietnam era — that a high school student has prevailed in a free-speech case at the Supreme Court,” said Justin Driver, a Yale law professor and author of “The Schoolhouse Gate: Public Education, the Supreme Court, and the Battle for the American Mind.”

“Given the range of plausible outcomes, public school students should be dancing in the streets due to this decision,” Driver said.

At the same time, the justices said that a lower court had gone too far in saying off-campus speech was off-limits and that school officials do have a role to play when speech is threatening or disruptive.

“The court made clear that it is not a question of how or where speech is made — whether on school grounds or online — but its impact or effect,” the National School Boards Association said in a statement.

“So while the school district lost on the facts of this particular case, it represents a win for schools, as well as students, who can still be protected from off-campus student speech that bullies, harasses, threatens, disrupts, or meets other circumstances outlined by the Court,” the association said.

Brandi Levy — now an 18-year-old college student — was a frustrated ninth-grader when she vented about being passed over for the varsity cheerleading squad at Mahanoy Area High School. On a spring Saturday in her freshman year, she posted on Snapchat a photo of herself and a friend with upraised middle fingers and this rant:

“F--- school, f--- softball, f--- cheer, f--- everything.” It was sent to about 250 friends, including fellow cheerleaders at her school.

It was supposed to disappear in 24 hours, but her cheerleading coaches were alerted to it, and Levy was suspended from cheerleading for a year.

In a statement Wednesday, Levy said: “Young people need to have the ability to express themselves without worrying about being punished when they get to school. I never could have imagined that one simple snap would turn into a Supreme Court case, but I’m proud that my family and I advocated for the rights of millions of public school students.”

Breyer’s opinion attempted to find middle ground, saying school authorities have a duty to monitor speech but also to encourage it as part of the educational process.

Despite ruling for Levy, Breyer said that a panel of the U.S. Court of Appeals for the 3rd Circuit had gone too far and that the Supreme Court would not endorse its view of off-campus speech.

“We do not now set forth a broad, highly general First Amendment rule stating just what counts as ‘off campus’ speech and whether or how ordinary First Amendment standards must give way off campus to a school’s special need to prevent . . . substantial disruption of learning-related activities or the protection of those who make up a school community,” Breyer wrote.

But Levy’s speech was not disruptive or threatening, he wrote.

“The vulgarity in B.L.’s posts encompassed a message, an expression of B.L.’s irritation with, and criticism of, the school and cheerleading communities,” Breyer wrote, adding that “the school’s interest in teaching good manners is not sufficient, in this case, to overcome B.L.’s interest in free expression.”

The American Civil Liberties Union, which argued the case on Levy’s behalf, cheered the ruling. David Cole, the ACLU’s legal director, called the outcome “a huge victory for the free speech rights of millions of students” who attend public school.

“The school in this case asked the court to allow it to punish speech that it considered ‘disruptive,’ regardless of where it occurs,” Cole said in a statement. “If the court had accepted that argument, it would have put in peril all manner of young people’s speech, including their expression on politics, school operations, and general teen frustrations.”

In 1969, the Supreme Court famously held in Tinker v. Des Moines Independent Community School District that students and teachers do not “shed their constitutional rights to freedom of speech or expression at the schoolhouse gate.”

But it also held that schools have broader authority over students than the state generally does when restricting speech, and that authorities can discipline students for speech that causes or is likely to cause “material and substantial” disruption of school functions. (The court ruled 7 to 2 for Mary Beth Tinker because, it said, the black armband she wore to protest the Vietnam War was not disruptive.)

In the half-century since, the Supreme Court’s decisions have been few and lean toward school administrators. The justices have upheld disciplinary action regarding a student newspaper that operated at the direction of school officials, lewd speech by students at school events and a nonsensical sign with a seemingly pro-marijuana message — “Bong Hits 4 Jesus” — held by a student at a school activity.

Breyer said that especially with the increase in online learning, it would be wrong to ignore disruptive speech just because it occurs off-campus.

“Serious or severe bullying or harassment targeting particular individuals; threats aimed at teachers or other students; the failure to follow rules concerning lessons, the writing of papers, the use of computers, or participation in other online school activities; and breaches of school security devices, including material maintained within school computers” are all areas where a school’s “regulatory interests remain significant,” Breyer wrote.

But he said that courts considering such cases in the future should remember that schools do not always stand in place of ­parents regarding off-campus speech, and that monitoring student speech 24 hours a day would give students little room for expression.

Also: “The school itself has an interest in protecting a student’s unpopular expression, especially when the expression takes place off campus,” Breyer wrote. “America’s public schools are the nurseries of democracy. Our representative democracy only works if we protect the ‘marketplace of ideas.’ ”

Breyer said Levy’s case provides courts with one example of when school administrators went too far.

The case attracted enormous attention in part because of the profanity involved. No lawyer uttered the words during oral argument, but Breyer provided an unadulterated version in his opinion, which was joined by Chief Justice John G. Roberts Jr. and Justices Samuel A. Alito Jr., Sonia Sotomayor, Elena Kagan, Neil M. Gorsuch, Brett M. Kavanaugh and Amy Coney Barrett.

Alito penned a long concurring opinion and said he did not understand the opinion to apply to students at public colleges and universities.

Thomas repeated his view that “schools historically could discipline students in circumstances like those presented here.”

He said that “because the majority does not attempt to explain why we should not apply this historical rule and does not attempt to tether its approach to anything stable, I respectfully dissent.”

The decision left many questions unanswered, but Frank D. LoMonte, director of the Brechner Center for Freedom of Information at the University of Florida, called it “surprisingly good.”

“What’s really important about the decision is what the Court did not do,” LoMonte, former director of the Student Press Law Center, wrote in an email. The justices did not say that “speech becomes punishable just because it’s about the school or just because it causes people at school to have a strong emotional reaction. And they did not declare that sports or extracurricular activities are some type of Constitution-free-zone where you waive all of your rights as a condition of participating.”

LoMonte summed up: “Two cheers for the Court — rah rah — with the third ‘rah’ withheld until, in the inevitable future case, they fill in the many blanks they’ve left.”

The case is Mahanoy Area School District v. B.L.