I was trying to upload this gif and was constantly running into the 504 error. I couldn't get past it despite trying to upload the gif about twenty times.
I use FFmpeg to generate these gifs and a tool called gifsicle to optimize them. I noticed that gifsicle has an option called --careful, the function of which is described as "Write larger GIFs that avoid bugs in other programs." I used this option to optimize the gif and the upload instantly succeeded.
Searching for what the code does, it seems like it has something to do with "minumun code size". From a Google search it looks like this has something to do with LZW compression.