DEV Community

Nathan Lively
Nathan Lively

Posted on

How to Build a PWA with Java and Spring Boot: Best Practices for Offline Functionality?

I'm currently working on a labor management project where one of the key requirements is to ensure that certain features (like clocking out) are accessible even when the user's device is temporarily offline. While this is a rare case, I want to future-proof the application by enabling additional offline functionality as needed.

Use Case:

  • Backend: Java with Spring Boot
  • Frontend: Thymeleaf with HTMX (or potentially Vaadin Flow)
  • Key Requirement: Ability to cache POST requests and navigate to cached pages when offline.

Technologies I'm Considering:

  1. Spring Boot with Thymeleaf and HTMX: My preferred stack. I like to build using TDD, and this combination aligns well with my experience. I already built a little demo project using a service-worker.js to cache and resent POST requests. I'm not sure if this is the best choice to continue adding offline functionality in the future.

  2. Vaadin Flow: I'm intrigued by Vaadin Flow for its strong focus on building business applications entirely in Java. However, I'm concerned about its ability to support offline functionality since it dynamically updates views. From what I understand, this would make it impossible to just cache an entire page for offline viewing. I'm aware of Hilla, but then I would need create all of the views in Typescript and that sort of defeats the purpose of using Vaadin, in my opinion.

  3. PWA Starter: I looked into PWA Starter, but it seems to focus on creating single-page apps (SPAs) in languages like Angular or React, which doesn't fit my preference for staying within the Spring Boot ecosystem.

  4. Quarkus or JHipster: I also considered Quarkus and JHipster, but JHipster typically uses Angular or Vue for the frontend, it seems, which I'm not very interested in. I'm looking for something that stays closer to a Java full-stack approach. Quarkus seems like it focuses on micro-services?

Questions:

  1. What are the best practices or frameworks/tools for integrating PWA capabilities, particularly offline functionality, within a Java and Spring Boot application? Maybe there's a tool that helps with the service worker, for example?
  2. Is there a way to effectively use Vaadin Flow with offline caching, or should I stick with Thymeleaf and HTMX for better control over service worker?
  3. Are there any specific resources, libraries, or patterns you recommend for implementing service workers in a Spring Boot and Thymeleaf setup?

I'm looking for advice on how to approach this, considering my preference to stay within the Java ecosystem and avoid adding significant new front-end technologies. I'm sure there are many questions I have not considered. Any insights or recommendations would be greatly appreciated!

Review of things I've tried

  • I built a demo by writing my own service worker. It was fine, but I'm not sure how far I can go with it.
  • I tried Vaadin Flow, but then realized that any offline functionality would require Typescript and Hilla.
  • I started looking at FlutterFlow, but it seems like a big design decision to make for this edge case of a user's device being offline.

Top comments (1)

Collapse
 
livenathan profile image
Nathan Lively

For example, the Vite PWA plugin looks like it handles the service worker automatically.