As bkolobara suggests it increasing the fee is the solution to a 504 Timeout error.
Re-submitting the same transaction (with the same sequence number) in a loop, with a delay, until it is accepted can work too, but I find this practice a bit too aggressive.
For my part
StellarSdk.BASE_FEE * 10 give good results on testnet... but not always, it is a sort of lottery 😆
But I'd be happier if I could predict it for each transaction with the fee stats https://developers.stellar.org/api/aggregations/fee-stats/
I'd be pleased if anyone could enlighten me about the fee stats by answering my post https://galactictalk.org/d/2597-predict-the-fees-in-practice