Skip to content

Commit 47c7287

Browse files
vaadin-bottaefimcollovati
authored
fix: Fix ServiceWorker path requests in dev mode (#14151) (#14203)
Fixes: #13987 Co-authored-by: Soroosh Taefi <taefi.soroosh@gmail.com> Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent 8083333 commit 47c7287

4 files changed

Lines changed: 102 additions & 19 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/PwaRegistry.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.slf4j.LoggerFactory;
4242

43+
import com.vaadin.experimental.FeatureFlags;
4344
import com.vaadin.flow.server.communication.PwaHandler;
4445
import com.vaadin.flow.server.startup.ApplicationConfiguration;
4546
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
@@ -258,7 +259,11 @@ private String initializeRuntimeServiceWorker(
258259
if (pwaConfiguration.isOfflinePathEnabled()) {
259260
filesToCache
260261
.add(offlinePageCache(pwaConfiguration.getOfflinePath()));
262+
} else if (shouldCacheRoot()) {
263+
// No offlinePath configured, cache the root (#13987):
264+
filesToCache.add(offlinePageCache("."));
261265
}
266+
262267
// Offline stub to be shown within an <iframe> in the app shell
263268
filesToCache
264269
.add(offlinePageCache(PwaHandler.DEFAULT_OFFLINE_STUB_PATH));
@@ -610,4 +615,11 @@ static List<PwaIcon> getIconTemplates(String baseName) {
610615
return icons;
611616
}
612617

618+
private boolean shouldCacheRoot() {
619+
VaadinContext context = VaadinService.getCurrent().getContext();
620+
ApplicationConfiguration configuration = ApplicationConfiguration
621+
.get(context);
622+
return configuration != null && !configuration.isProductionMode()
623+
&& FeatureFlags.get(context).isEnabled(FeatureFlags.VITE);
624+
}
613625
}

‎flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,6 @@ if (process.env.NODE_ENV === 'development' && VITE_ENABLED) {
8888
({ url }) => url.pathname.startsWith(`${scopePath}VAADIN/`),
8989
networkFirst
9090
);
91-
92-
if (offlinePath === '.') {
93-
registerRoute(
94-
({ request, url }) => request.mode === 'navigate' && !isManifestEntryURL(url),
95-
async ({ event }) => {
96-
return networkFirst
97-
.handle({ request: new Request(offlinePath), event })
98-
.then(rewriteBaseHref);
99-
}
100-
)
101-
}
10291
}
10392

10493
registerRoute(

‎flow-server/src/test/java/com/vaadin/flow/server/PwaRegistryTest.java‎

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.vaadin.flow.server;
1717

1818
import javax.servlet.ServletContext;
19-
2019
import java.io.ByteArrayOutputStream;
2120
import java.io.IOException;
2221
import java.util.List;
@@ -26,22 +25,29 @@
2625
import org.junit.Assert;
2726
import org.junit.BeforeClass;
2827
import org.junit.Test;
28+
import org.mockito.MockedStatic;
2929
import org.mockito.Mockito;
3030

31+
import com.vaadin.experimental.FeatureFlags;
32+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
33+
3134
@PWA(name = "foo", shortName = "bar")
3235
public class PwaRegistryTest {
3336

3437
@PWA(name = "Custom Icon Path", shortName = "CIP", iconPath = "icons/splash/foo.png")
3538
private static class PwaWithCustomIconPath {
3639
}
3740

41+
@PWA(name = "Custom Icon Path", shortName = "COP", offlinePath = "some/path.html")
42+
private static class PwaWithCustomOfflinePath {
43+
}
44+
3845
private static List<PwaIcon> splashIconsForAppleDevices;
3946

4047
@BeforeClass
4148
public static void initPwaWithCustomIconPath() throws IOException {
42-
ServletContext context = Mockito.mock(ServletContext.class);
43-
PwaRegistry registry = new PwaRegistry(
44-
PwaWithCustomIconPath.class.getAnnotation(PWA.class), context);
49+
PwaRegistry registry = preparePwaRegistry(
50+
PwaWithCustomIconPath.class.getAnnotation(PWA.class), true);
4551
splashIconsForAppleDevices = registry.getIcons().stream().filter(
4652
icon -> "apple-touch-startup-image".equals(icon.getRel()))
4753
.collect(Collectors.toList());
@@ -50,15 +56,14 @@ public static void initPwaWithCustomIconPath() throws IOException {
5056
@Test
5157
public void pwaIconIsGeneratedBasedOnClasspathIcon_servletContextHasNoResources()
5258
throws IOException {
53-
ServletContext context = Mockito.mock(ServletContext.class);
5459
// PWA annotation has default value for "iconPath" but servlet context
5560
// has no resource for that path, in that case the ClassPath URL will be
5661
// checked which is "META-INF/resources/icons/icon.png" (this path
5762
// available is in the test resources folder). The icon in this path
5863
// differs from the default icon and set of icons will be generated
5964
// based on it
60-
PwaRegistry registry = new PwaRegistry(
61-
PwaRegistryTest.class.getAnnotation(PWA.class), context);
65+
PwaRegistry registry = preparePwaRegistry(
66+
PwaRegistryTest.class.getAnnotation(PWA.class), true);
6267
List<PwaIcon> icons = registry.getIcons();
6368
// This icon has width 32 and it's generated based on a custom icon (see
6469
// above)
@@ -70,6 +75,40 @@ public void pwaIconIsGeneratedBasedOnClasspathIcon_servletContextHasNoResources(
7075
Assert.assertEquals(26, stream.toByteArray()[36]);
7176
}
7277

78+
private static PwaRegistry preparePwaRegistry(PWA pwa, boolean viteEnabled)
79+
throws IOException {
80+
try (MockedStatic<VaadinService> vaadinService = Mockito
81+
.mockStatic(VaadinService.class);
82+
MockedStatic<ApplicationConfiguration> configuration = Mockito
83+
.mockStatic(ApplicationConfiguration.class);
84+
MockedStatic<FeatureFlags> featureFlags = Mockito
85+
.mockStatic(FeatureFlags.class)) {
86+
87+
VaadinService vaadinServiceMocked = Mockito
88+
.mock(VaadinService.class);
89+
VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
90+
ApplicationConfiguration applicationConfiguration = Mockito
91+
.mock(ApplicationConfiguration.class);
92+
93+
vaadinService.when(VaadinService::getCurrent)
94+
.thenReturn(vaadinServiceMocked);
95+
Mockito.when(vaadinServiceMocked.getContext())
96+
.thenReturn(vaadinContext);
97+
configuration
98+
.when(() -> ApplicationConfiguration.get(Mockito.any()))
99+
.thenReturn(applicationConfiguration);
100+
101+
FeatureFlags flags = Mockito.mock(FeatureFlags.class);
102+
Mockito.when(flags.isEnabled(FeatureFlags.VITE))
103+
.thenReturn(viteEnabled);
104+
featureFlags.when(() -> FeatureFlags.get(Mockito.any()))
105+
.thenReturn(flags);
106+
107+
ServletContext context = Mockito.mock(ServletContext.class);
108+
return new PwaRegistry(pwa, context);
109+
}
110+
}
111+
73112
@Test
74113
public void pwaWithCustomBaseIconPath_splashScreenIconForAllSupportedAppleDevicesAndOrientationsAreGenerated() {
75114
Assert.assertEquals(26, splashIconsForAppleDevices.size());
@@ -262,4 +301,43 @@ public void pwaWithCustomBaseIconPath_splashScreenIconForIPhoneDevices_includeBo
262301
.stream().filter(media -> media.contains("landscape")).count());
263302
}
264303

304+
@Test
305+
public void pwaWithCustomOfflinePath_viteIsEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath()
306+
throws IOException {
307+
PwaRegistry registry = preparePwaRegistry(
308+
PwaWithCustomOfflinePath.class.getAnnotation(PWA.class), true);
309+
Assert.assertTrue(registry.getRuntimeServiceWorkerJs()
310+
.contains("some/path.html"));
311+
Assert.assertFalse(registry.getRuntimeServiceWorkerJs()
312+
.contains("{ url: '.', revision:"));
313+
}
314+
315+
@Test
316+
public void pwaWithCustomOfflinePath_viteIsNotEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath()
317+
throws IOException {
318+
PwaRegistry registry = preparePwaRegistry(
319+
PwaWithCustomOfflinePath.class.getAnnotation(PWA.class), false);
320+
Assert.assertTrue(registry.getRuntimeServiceWorkerJs()
321+
.contains("some/path.html"));
322+
Assert.assertFalse(registry.getRuntimeServiceWorkerJs()
323+
.contains("{ url: '.', revision:"));
324+
}
325+
326+
@Test
327+
public void pwaWithoutCustomOfflinePath_viteIsEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath()
328+
throws IOException {
329+
PwaRegistry registry = preparePwaRegistry(
330+
PwaRegistryTest.class.getAnnotation(PWA.class), true);
331+
Assert.assertTrue(registry.getRuntimeServiceWorkerJs()
332+
.contains("{ url: '.', revision:"));
333+
}
334+
335+
@Test
336+
public void pwaWithoutCustomOfflinePath_viteIsNotEnabled_getRuntimeServiceWorkerJsDoesNotContainsRoot()
337+
throws IOException {
338+
PwaRegistry registry = preparePwaRegistry(
339+
PwaRegistryTest.class.getAnnotation(PWA.class), false);
340+
Assert.assertFalse(registry.getRuntimeServiceWorkerJs()
341+
.contains("{ url: '.', revision:"));
342+
}
265343
}

‎flow-tests/test-frontend/vite-pwa-production/src/test/java/com/vaadin/viteapp/MainIT.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ private void setOfflineAndReload() {
9595

9696
private void openPage(String url) {
9797
getDriver().get(getRootURL() + url);
98-
waitForServiceWorkerReady();
98+
try {
99+
waitForServiceWorkerReady();
100+
} finally {
101+
checkLogsForErrors();
102+
}
99103
}
100104
}

0 commit comments

Comments
 (0)