If you aren't traveling to the US or from the US, but just have a stopover in Puerto Rico, do you have to go through US customs?
Yes. Puerto Rico is part of the US, including for immigration purposes, and the same rules apply there as in airports in US states. The US does not have sterile transit for international-to-international connections; all passengers must go through US immigration, customs, and TSA security screening.
See our closely related questions Do I need an ESTA for changing airplanes in Puerto Rico? and Do I need a transit visa for changing airplane in San Juan airport?