We report the first systematic analysis of single exposures of all optical and ultraviolet (UV) observations performed by the UltraViolet and Optical Telescope (UVOT) on board the {em Neil Gehrels Swift Observatory} satellite available up to 2019 April of six $gamma$-ray-emitting narrow-line Seyfert 1 galaxies (NLSy1). Rapid variability has been significantly detected on hours time-scale for 1H 0323+342, SBS 0846+513, PMN J0948+0022, and PKS 2004-447 in 18 observations for a total of 34 events. In particular, we report the first detection of significant variability on short time-scale (3-6 ks) in optical for PKS 2004-447, and UV for 1H 0323+342 and PMN J0948+0022. The shortest variability time-scale observed for 1H 0323+342, SBS 0846+513, PMN J0948+0022, and PKS 2004-447 (assuming a Doppler factor delta = 10) gives a lower limit on the size of emission region between 9.7 $times$ 10$^{14}$ (for SBS 0846+513) and 1.6 $times$ 10$^{15}$ cm (for 1H 0323+342), suggesting that the optical and UV emission during these events is produced in compact regions within the jet. These observations provide unambiguous evidence about the relativistically beamed synchrotron emission in these sources, similar to blazars. A remarkable variability has been observed for PMN J0948+0022 on 2009 June 23 with an increase from $sim$1.1 to 0.4 mag going from v to w2 filter in $sim$1.6 h and a decrease at the initial level in a comparable time. The higher fractional flux change observed for this and other events at lower frequencies suggests that the synchrotron emission is more contaminated by thermal emission from accretion disc at higher frequencies.