During World War I, the town was captured by Italian forces in 1915 and became the rear line during the bloody Battles of the Isonzo, being briefly recaptured by Austria-Hungary after the 1917 Battle of Caporetto, but returning to Italy in 1918. The shipyards were severely damaged by bitter fighting and had to be rebuilt afterwards. During World War II the town was repeatedly bombed and heavily damaged, became a center of the Italian Resistance after the Armistice of Cassibile, and it was briefly occupied by Yugoslav troops at the end of the war.