Spain was caught up in European events of the Napoleonic era that led to its loss of empire in Spanish America. Spain was France's ally, but it had tried to avoid being drawn directly into the ongoing conflict between Napoleon's France and Britain. War broke out in 1804 after a British squadron captured a Spanish convoy off Cape Santa Maria, Portugal. The British navy defeated the Spanish navy in the Battle of Trafalgar in 1805. The following year, the British attempted to seize the Río de la Plata estuary. The viceroy retreated hastily to the hills when defeated by a small British force. However, the "Criollos"' militias and colonial army repulsed the now reinforced British force in 1807.