I am an Afghan citizen and I have a flight from Sao pula to Dominica via Puerto Rico.
I have a 23h stay.
Do I need a transit visa?
Puerto Rico is a United States territory and the same rules for immigration purposes apply as in any US state (this is not true of every US territory, but it is the case for Puerto Rico). Just as transits at mainland US airports require you to go through immigration, all transits at San Juan require you to go through US immigration. You will need a US visa.
Furthermore, according to sleepininairports.net, you cannot sleep overnight airside at San Juan Airport and there are mixed reports as to whether sleeping overnight in the airport is allowed at all.