use of com.vaadin.flow.server.PwaIcon in project flow by vaadin.
the class PwaHandler method init.
private void init(PwaRegistry pwaRegistry) {
// Icon handling
for (PwaIcon icon : pwaRegistry.getIcons()) {
requestHandlerMap.put(icon.getRelHref(), (session, request, response) -> {
response.setContentType(icon.getType());
// caching
if (icon.shouldBeCached()) {
response.setHeader("Cache-Control", "no-cache, must-revalidate");
}
try (OutputStream out = response.getOutputStream()) {
icon.write(out);
}
return true;
});
}
// Assume that offline page and offline stub (for display within app)
// are the same. This may change in the future.
List<String> offlinePaths = new ArrayList<>();
if (pwaRegistry.getPwaConfiguration().isOfflinePathEnabled()) {
offlinePaths.add(pwaRegistry.getPwaConfiguration().relOfflinePath());
}
offlinePaths.add("/" + DEFAULT_OFFLINE_STUB_PATH);
for (String offlinePath : offlinePaths) {
requestHandlerMap.put(offlinePath, (session, request, response) -> {
response.setContentType("text/html");
try (PrintWriter writer = response.getWriter()) {
writer.write(pwaRegistry.getOfflineHtml());
}
return true;
});
}
// manifest.webmanifest handling
requestHandlerMap.put(pwaRegistry.getPwaConfiguration().relManifestPath(), (session, request, response) -> {
response.setContentType("application/manifest+json");
try (PrintWriter writer = response.getWriter()) {
writer.write(pwaRegistry.getManifestJson());
}
return true;
});
// sw-runtime.js handling (service worker import for precaching runtime
// generated assets)
requestHandlerMap.put(SW_RUNTIME_PRECACHE_PATH, (session, request, response) -> {
response.setContentType("application/javascript");
try (PrintWriter writer = response.getWriter()) {
writer.write(pwaRegistry.getRuntimeServiceWorkerJs());
}
return true;
});
}
use of com.vaadin.flow.server.PwaIcon in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noPWA.
@Test
public void writeBootstrapPage_noPWA() throws IOException, ServiceException {
TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
PwaRegistry registry = Mockito.mock(PwaRegistry.class);
PwaConfiguration conf = Mockito.mock(PwaConfiguration.class);
Mockito.when(registry.getPwaConfiguration()).thenReturn(conf);
Mockito.when(conf.isEnabled()).thenReturn(true);
Mockito.when(conf.getManifestPath()).thenReturn("bar");
PwaIcon icon = Mockito.mock(PwaIcon.class);
Mockito.when(icon.asElement()).thenReturn(new Element("h1"));
Mockito.when(registry.getHeaderIcons()).thenReturn(Collections.singletonList(icon));
VaadinServletService service = new MockVaadinServletService() {
@Override
protected PwaRegistry getPwaRegistry() {
return registry;
}
};
initLookup(service);
VaadinSession session = new MockVaadinSession(service);
session.lock();
session.setConfiguration(service.getDeploymentConfiguration());
MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
config.setEnableDevServer(false);
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(request.getServletPath()).thenReturn("/");
VaadinResponse response = getMockResponse(null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Mockito.when(response.getOutputStream()).thenReturn(stream);
handler.synchronizedHandleRequest(session, request, response);
String result = stream.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("bar")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("h1")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("baz")));
}
Aggregations