When the U.S. defense secretary, Lloyd J. Austin III, visited Indonesia in November, he pressed his counterpart there about a deal to buy 36 American fighter jets. He left without an agreement.

Just days before, the same Indonesian official, Prabowo Subianto, met with China’s defense minister, and the two countries pledged to resume joint military exercises.

Located across the southern edge of the South China Sea, Indonesia, the resource-laden nation with a fast-growing trillion-dollar economy and a large population, is a big prize in the geopolitical battle between Washington and Beijing for influence in Asia. And its strategic location, with about 17,000 islands straddling thousands of miles of vital sea lane, is a defensive necessity as both sides gear up for a possible conflict over Taiwan, the island democracy that China claims it possesses.

In wooing Indonesia, Beijing increasingly appears to have the edge.

China has delivered sizable investments to win over a wary populace in Indonesia, pouring billions of dollars into developing the world’s biggest nickel deposits and expediting shipments of Covid-19 vaccines at a critical time. It has been a major partner in the country’s infrastructure push, including building a high-speed train, albeit late and over budget.

China invested more than $5 billion in Indonesia in the first nine months of 2022, compared to around $2 billion from the United States.

“They never, ever dictate,” Luhut Binsar Pandjaitan, coordinating minister of maritime and investment affairs, said of the Chinese during a recent interview.

He said that American officials often come with a list of onerous conditions before an investment can be approved. “I told Washington about this: ‘The way you deal with us, forget it,’” said Mr. Pandjaitan, who is also the chief lieutenant of Indonesia’s leader, Joko Widodo.

Indonesia, in turn, has delivered for China. The majority Muslim nation has voted in favor of China’s position at the United Nations on Beijing’s persecution of Uyghurs, a largely Muslim group. In the halls of the leading regional bloc, the Association of Southeast Asian Nations, diplomats say Indonesia is a consistent cheerleader for China’s untrammeled economic involvement in all 10 member nations.

Mr. Joko likes to say he remains independent of either country’s influence. But he and his top lieutenants have shown a special affinity for China’s leader, Xi Jinping.

A month after he came to power in the fall of 2014, Mr. Joko traveled to Beijing for his first overseas trip. Since then, he has met Mr. Xi one-on-one eight times, and with former President Donald J. Trump and President Biden on just four occasions in total, according to Teuku Faizasyah, a spokesman for Indonesia’s Ministry of Foreign Affairs.

Indonesia’s warmth with China is based in part on the confluence of their leaders’ political interests. From the start of his presidency, Mr. Joko made infrastructure a recurring theme of his tenure, and Mr. Xi had made infrastructure investments a backbone of his diplomatic strategy. During his first visit to Beijing, Mr. Joko was ushered onto the high-speed train from Beijing to Tianjin, a port city, and in October 2015, he signed a multibillion-dollar deal for China to build one in Indonesia.

Historically, Indonesia has demonstrated a strong anti-China streak. In 1965, mobs made up of military, paramilitary and religious groups rampaged against Indonesia’s Communist Party, the largest outside of China. The mobs killed at least half a million people, including many ethnic Chinese. Hard-line generals accused Beijing of being behind a coup attempt that they said was organized by the Indonesian Communist Party. As a result, relations between Indonesia and China were frozen for decades.

Memories from the slaughter remain, and China’s ambassador to Indonesia, Lu Kang, a former spokesman for the Ministry of Foreign Affairs in Beijing, appears careful not to stoke smoldering suspicions, choosing diplomatic niceties over nationalistic bluster on social media. On his Twitter account, Mr. Lu showcases his visits to the balmy landscapes of Bali and friendly footage of China’s prime minister, Zhou Enlai, visiting Indonesia in 1955, before the tensions erupted.

“China is by far the No. 1 trading partner, No. 1 foreign investor and, before the pandemic, the No. 1 source of international tourists,” said Tom Lembong, a former trade and investment minister in the early years of Mr. Joko’s tenure. “Many Indonesian business and political elites believe that China is the relevant superpower and the U.S. is in relative decline — and, geographically, far away.”

In less than a decade, China has deepened its ties with Indonesia, in many cases in direct competition with the United States. A Chinese company, Tsingshan, dominates the country’s nickel mining, for example, and China is also building coal-fired power stations and processing raw nickel into forms suitable for stainless steel and electric vehicle batteries. In doing so, China has answered Mr. Joko’s call for additional processing in Indonesia, creating more high-value products for nickel, albeit with more environmental concerns.

Indonesia, hit hard by the pandemic, also was able to secure early supplies of Chinese-made vaccines. At the time, President Trump had made it clear that Americans would be vaccinated before American-made vaccines would be exported.

In early December 2020, the first planeload of Sinovac, the Chinese-made vaccine, landed in Indonesia. Television footage of the vaccine’s arrival appeared across the country. Indonesian Muslim clerics declared the vaccine halal-certified.

Still, China and Indonesia’s relationship is not without challenges.

When Indonesia announced that China would build an 88-mile, $5.5 billion high-speed train from Jakarta to Bandung, a provincial capital, the project was promised to be completed by 2019.

However, the project’s financials didn’t make sense from the start, said Faisal Basri, a prominent economist at the University of Indonesia, and a critic of the project. Ticket sales wouldn’t provide sufficient revenue, the land was prohibitively expensive and the final station would stop miles from Bandung, forcing passengers to finish their trip by other means.

The project is now three years late, and the cost overrun could be as much $1.9 billion, according to Katadata, a research firm in Jakarta.

A refinancing deal that the Indonesian government and Beijing are discussing is likely to result in China increasing its ownership stake in the rail from 40 percent to 60 percent, Mr. Basri said.

A trial run to showcase the train during the Group of 20 meeting in November with Mr. Xi and Mr. Joko was canceled. A complete set of shiny new carriages shipped from China for the event sit idle in a hangar.

As Washington works to bolster ties in Asia to counter China’s influence, Indonesia remains cautious, careful not to anger Beijing.

Much to the chagrin of the Biden administration, Indonesia has strongly opposed the U.S. plan to arm its ally, Australia, with nuclear-powered submarines. Indonesian officials have said they want to have a nuclear-free zone around its territory. Those boats would need to sail through or past Indonesian waters in a battle between the United States and China over Taiwan.

“We would stay neutral” in a U.S.-China conflict over Taiwan, said Santo Darmosumarto, director of East Asia and Pacific Affairs at Indonesia’s Ministry of Foreign Affairs.

Indonesia’s neutrality complicates Washington’s expanding efforts in Asia to counter China, said Hugh White, an Australian defense strategist.

“Militarily, access to bases in Indonesia would be a big asset to U.S. forces in a war over Taiwan, but that’s not going to happen,” Mr. White said.

Last August, Indonesia’s military participated with U.S. forces in a multinational air, land and sea exercise. But while its weapons, many of them from Russia, age, buying replacements from the United States appears unlikely. Meanwhile, last February, Indonesia bought 42 Rafale fighter jets from France.

Weeks after Mr. Austin, the defense secretary, left in November, Indonesia decided not to buy F-15 fighter jets, citing budgetary reasons, according to two Biden administration officials with knowledge of the discussions. The officials said they were told the cost was too steep given Indonesia’s focus on its domestic agenda.

Mr. Austin came away from Indonesia with slim gains: some additional training programs in the United States for Indonesian military students. They also train in Russia and China.

Dera Menra Sijabat contributed reporting from Indonesia.