Ansu Fati scored an extra-time winner as Barcelona scraped past third-tier Intercity 4-3 in the Copa del Rey after Oriol Soldevila's hat-trick.
A topsy-turvy tie saw the Blaugrana repeatedly frustrated by the minnows after Ronald Araujo's early effort had suggested a comfortable night for the 31-time winners.
That was certainly not the case, with Soldevila's hat-trick of equalisers sending the tie into extra time after Ousmane Dembele and Raphinha had each restored Barcelona's lead.
But Soldevila and Intercity could not repeat the feat a fourth time after Fati struck decisively in the 103rd minute.
A swift start for Xavi's side had seen Araujo head in Pablo Torre's corner with just four minutes on the clock, but the defender was soon required to clear off the line at the other end, and Intercity improved further after half-time.
A deserved equaliser followed just before the hour mark, with Vadik Murria nodding a corner down for Soldevila to fire past the helpless Inaki Pena.
The scores were only level for seven minutes as Dembele latched onto Gavi's pass to chip an effort over Gaizka Campos, but Intercity again fought back and Soldevila faced no challenge from Jordi Alba as he headed his second.
Alba redeemed himself with an assist for Raphinha at the other end, only for the winger to be brilliantly denied another by Campos, with Soldevila breaking from the subsequent corner to sensationally force extra time.
A fortuitous Barcelona fourth would prove enough, however, as Fati's deflected effort deceived Campos to send the visitors into the last 16.
What does it mean? Blaugrana avoid upset
Barcelona boast a stellar record against lower-tier sides in the Copa del Rey, but this was far from the comfortable game that many had expected.
The win extends the Blaugrana's run in the Copa del Rey to 22 matches against lower-league opposition without elimination, a sequence that extends back to October 2004 when they suffered a 1-0 defeat to Gramanet.
Intercity can certainly hold their heads high, though, having become the first side from the third tier to score three goals in a single Copa game against Barcelona since Novelda won 3-2 in September 2002.
Dembele's rejuvenation under the guidance of Xavi has been well documented and, having scored Barcelona's second of the game, he now stands as the player who has been directly involved in the most goals under the Camp Nou coach.
A tally of 28 (eight goals, 20 assists) puts the France international ahead of the rest in that regard.
Soldevila's hat-trick put his name up in lights despite the defeat, as he became the first Spanish player since Aritz Aduriz in 2015 to net a hat-trick against Barcelona.
His three goals came from four shots on the target, the most of any player on the field, and just five touches inside the opposition box.
After the last-16 draw, Barcelona travel to face Atletico Madrid in LaLiga on Saturday.