SAN FRANCISCO — A record-setting surge of coronavirus cases and hospitalizations in California has pushed the state to the edge of another mandatory shutdown, with officials in San Francisco and several counties in the Bay Area on Friday announcing preemptive stay-at-home orders to take effect Sunday night, affecting millions of people.

“We need to do everything we can to prevent our hospital system from becoming overwhelmed and to save lives. We know that the faster we flatten the curve, the less time it takes us to move out of the danger zone,” San Francisco Mayor London Breed said in a statement.

The situation in California, the country’s most populous state with nearly 40 million residents, reflects a punishing upswing in cases nationwide that has strained hospitals and medical staff. But it also highlights the difficulty of controlling the course of the pandemic even in a place where there has been significant political will to enact public health measures that include mask mandates and lengthy stay-at-home recommendations.

The speed of the new spike in California, the third the state has experienced, has been staggering.

When numbers climbed over the summer, the average number of new cases was roughly 10,000 a day. On Friday, the state announced more than 22,000 new daily cases, which beat another record set just Wednesday. There were nearly 10,000 people hospitalized in the state Friday, which broke another record set the day before.

The country’s leading public health experts have issued increasingly dire warnings as we enter what could be the deadliest period of the pandemic. For the first time, the Centers for Disease Control and Prevention on Friday began to recommend universal indoor mask when not in your own home.

On Friday, there were more than 221,000 new cases reported nationwide and nearly 101,000 people hospitalized with the virus; both were records. Nearly 2,800 people died of the virus.

Anthony S. Fauci, director of the National Institute of Allergy and Infectious Diseases,said on Friday that the record-setting surgein infections doesn’t fully account for the expected increases after Americans traveled and gathered for Thanksgiving.

In California, the main concern now among officials is the health-care system’s capacity. Roughly 12 percent of new cases are ending up in hospitals and intensive care wards.

Gov. Gavin Newsom (D) outlined a wider shutdown order Thursday tied to regional hospital capacity. When the available beds of a region’s intensive care units fall below 15 percent capacity, that region’s restaurants will be forced to shut all but carryout service, retail stores will be limited to 20 percent capacity, and all residents will be told to stay home except for exercise and cases of emergency.

“The bottom line is if we don’t act now, our hospital system will be overwhelmed,” Newsom said.

The order divides the state into five regions and triggers the tightest restrictions on businesses and personal behavior since officials first announced a weeks-long statewide stay-at-home order March 19. None of the five regions have fallen below that 15 percent mark yet, but state health officials expect most to trigger the order by the end of next week. The northern California and southern California regions remain the most in jeopardy — ICU capacity is below 20 percent in both.

By those metrics, the Bay Area is the safest region, with the greatest number of available beds for the moment. But citing a desire to move aggressively before hospital capacity becomes an issue, several counties in the Bay Area jointly announced Friday they would implement stay-home orders beginning Sunday even though they had not yet fallen below 15 percent ICU capacity.

The new rules are not as strict as the March order. But the need for another round of severe restrictions this long into the pandemic has left many residents angry and frustrated, especially after recent missteps by the governor and other prominent political leaders up and down the state.

Newsom (D) was widely criticized last month after it was revealed that he dined in a partially enclosed room at a restaurant in Napa Valley, which cut against the social distancing guidelines he has implored the state’s residents to follow. That he broke those guidelines to take a meal at a famously upscale French restaurant further galled many who have lost paychecks and made sacrifices because of the pandemic.

The governor later apologized, calling it a “bad mistake.”

This week, photos from October surfaced of Breed, the San Francisco mayor, dining at the same restaurant on a different night. “This criticism is fair. It doesn’t matter whether something is technically allowed or not--I need to hold myself to a higher standard and I will do better,” she tweeted.

In Los Angeles, county supervisor Sheila Kuehl was spotted last week dining outdoors in Santa Monica hours after voting to put a temporary ban on the practice.

“The spirit of what I’m preaching all the time was contradicted, and I’ve got to own that, and so I’m going to apologize to you because I need to preach and practice, not just preach and not practice,” Newsom said in a news conference. “I’ve done my best to do that. We’re all human. We all fall short sometimes.”

Andrew Noymer, an epidemiologist at the University of California at Irvine, is among the Californians dismayed by Newsom’s decision to share a meal with people from outside his household last month at the French Laundry, a posh Michelin three-star restaurant.

“I’ve been cooking my own meals for nine months. So I do think it takes away some of his moral authority to issue strict new public health orders,” Noymer said.

But Noymer also pointed out that many people simply refuse to believe the pandemic is a serious public health crisis and are ignoring the mandates on wearing masks and social distancing.

“There’s a lot of people who are actively defying the public health orders. They think it’s a hoax,” Noymer said. “There’s a part of the country that’s treating the pandemic as though it’s a conspiracy theory foisted on them.”

Nonetheless, the political missteps have been seized by conservatives to accuse Democratic politicians of failing to live up to their own standards. In a scathing presentation, White House press secretary Kayleigh McEnany this week seized on the instances to accuse Democrats of hypocrisy from the podium in the White House briefing room.

“Quite clearly these Democrats do not follow their own edicts. They act in a way that their own citizens are barred from acting,” she said. “These images behind me make clear Democrats’ mind-set: rules for thee but not for me.”

But those critiques carefully focused on political hypocrisy rather than on the behaviors alone.

President Trump and the first lady, Melania Trump, who themselves contracted the virus after attending numerous indoor events, plan to host two dozen indoor holiday parties this month. Secretary of State Mike Pompeo and his wife, Susan, are planning to host hundreds of people at a party on the eighth floor of the State Department this month.

Throughout much of the country, anti-mask animus is strongest among political conservatives who claim mask mandates are a violation of their freedom. Republican governors in some of the country’s worst-hit states, which are far less populous than California, have long resisted compulsory mask ordinances and restrictions on indoor dining, which are widely supported by public health experts and officials.

When enacted, those measures can be extremely effective. Facing a critical strain on hospital capacity, North Dakota Gov. Doug Burgum (R) announced a mask mandate and indoor dining restrictions three weeks ago. North Dakota has since seen a dramatic decline in cases and is no longer leading the country in new cases of coronavirus per capita.

That distinction now goes to its neighbor and twin, South Dakota, where the state’s governor has resisted requiring masks be worn in public places even as some cities in the state have considered enacting their own.

Meanwhile, efforts in California and New York to ban large gatherings in places of worship were dealt a setback this week by the courts.

On Thursday, the Supreme Court sided with a California church challenging restrictions by Newsom on indoor church services and ordered a federal-district court to reexamine similar previous rulings in California.

With cases spiraling nationally, many are placing their hopes in the quick approval and distribution of several vaccine candidates. Vice President Pence said on Friday that he is “very optimistic” a coronavirus vaccine may be approved as early as within a week-and-a-half, which would mean as many as 20 million Americans could get vaccinated by the end of the year.

“Because of all of you, help is on the way and we can see light at the end of the tunnel,” Pence said during a roundtable discussion on the coronavirus vaccine progress, after a visit with CDC’s Emergency Operations Center personnel in Georgia.

The first batch of 40 million doses should go to health-care workers and prioritize the most vulnerable sectors of society, including elderly residents in long-term care facilities, said Jay C. Butler, the CDC deputy director for infectious diseases.

For now, even in places where people are trying to follow safety guidelines, confusing political messaging has at times left residents scratching their heads.

On Wednesday night, Los Angeles Mayor Eric Garcetti issued a complicated 12-page set of restrictions — a “Safer at Home” order — that generated confusion about what exactly the rules are.

The city banned gatherings, even private ones, of people from more than one household — with an exception for outdoor faith-based services or protests, provided that people wear masks and engage in social distancing.

The list of other exceptions ran on for many pages.

Noymer, the epidemiologist, said many people were initially confused about whether they were still allowed to walk their dogs. The city’s order closely matches a similar one issued Monday from Los Angeles County. But these orders require people to wade through many thousands of words about what’s permissible.

The Los Angeles Times editorialized, “The formula used by the state and all the exemptions in the order by the county and city of Los Angeles are confusing and capricious.”

Taylor Telford, Lena H. Sun and Paulina Villegas contributed to this report.