Let's Build an Offline-First Vue App
Depending on your application, you might find yourself in a position to offer your users the ability to use your program even if they have spotty or missing internet connectivity. By thinking about your app in terms of being offline-first, you can increase the overall response time of your application while providing a safety net for users to fall back on that allows them to continue working even in the absense of an internet connection. All of this is made possible with modern browsers, specialized files called Service Workers, and of course, Vue.js.
Progress
Series Info
- Episodes
- 10
- Run Time
- 1h 52m
- Difficulty
- Advanced
- Last Updated
- Feb 2, 2022
- Version
- Latest
Series Episodes
- Episodes (10)
An Offline First App?
In this series, I'll be showing you how to build an application from the ground up that's centered around being offline-first. We'll see how the end product looks and functions, and get started creating a new Vue app using the CLI tool.The Editor Frontend
The first thing we need for a notes app is a place to write out some notes! In order to do that, we'll have to install and configure a WYSIWYG editor. There's a lot of options to choose from, but the one I've found that works seamlessly with Vue projects is tiptap.Introducing IndexedDB
All modern web browsers come with a fantastic local storage API called IndexedDB. It has practically unlimited storage and is great for holding large amounts of data for websites, especially because it works offline. Let's see what it takes to add one to our Vue application.Saving and Displaying Notes
Alright so now that we have an IndexedDB database set up in our application, it's time that we put it to use. After a user types up a note, I want to save that raw HTML as a new row in the notes table (or Object Store) that we created. Also, the sidebar is looking a little bare, so let's fill it up with the notes in our database whenever the app loads up.Let's Talk PWA
Progressive Web App technology helps power the backbone of offline-first features. Using a specialized script called a service worker, we're able to cache resources, pre-cache offline pages, and allow our app to be installable to a home screen or desktop. All of this is easily added in with a single line in the terminal.Running Database Updates
Before we start diving into more offline-first features, let's finish up the main functionality of our app. We need to give our users the ability to update notes in the database, improve the UI to create a new note, and display a whole list of notes and their content from the database.Caching Assets
Now that we've added a service worker and some PWA functionality, we can expand on it a bit. First, we need to make sure to bust any cached content that might have been updated from the server. Then, let's take care of those favicons that aren't showing up in the cache and so aren't displaying when we're offline.Oops, I'm Offline
Just because our application is offline-first and able to run without internet access, doesn't mean there wouldn't be parts in it that need to connect to an external server. The best example for this is syncing some kind of changes or user data with an API, especially when a user regains connectivity.Time to Refactor
Right now our application is a single monolith contained in one Vue component. While it's functional, it doesn't really look great and could get even worse if we try to expand on it in the future. Let's take some time to properly refactor this, by splitting our main codebase into child components and introducing a central store for the application state.Wrapping Up and Next Steps
We're finished! Over the last nine episodes, you've learned how to create a fully-fledged offline-first Vue app from scratch. So, where do we go from here? There's a few different paths you can take to expand on this application and provide more features and functionality to your users.
