未加星标

Easy Offline First Apps With Webpack's Offline Plugin

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

TL;DR Here is a repository with a working example. :)

Many of your have probably noticed all the buzz about offline first Web-apps. The Web is full of articles about the AppCache about Service Workers and such and now you ask yourself, how do I can get my apps work offline, like the cool kids . Or you simply asked, how come that data I already have on my device isn't shown to me anymore?!

Well, there are a bunch of technologies to help you with this, but this seems a rather fast paced topic these days. So first things first.

If you write a single page application, you often have different types of request your app sends. Some get images, some get styles, some code and some actual user data .

Caching User Data

For the last point, the user data , you will basically store the whole stuff somewhere on the client. Since this depends mostly on your business logic, it is advisable to write at least part of that logic yourself.

You can use localStorage for this if a key-value-store is enough or, indexed DB if you need a more sophisticated datastore. There are even libraries that help with the usage of these low-level Web-APIs. Like PouchDB and LocalForage .

Caching Assets

But this post is about your assets. Your HTML, CSS, JS and Images.

If you bundle your assets with Webpack, there is a really easy way: The offline-plugin .

After building with this plugin, you can call a function, that will make all Webpack generated assets available offline.

A basic webpack.config.js could look like this:

const HtmlWebpackPlugin = require('html-webpack-plugin') const OfflinePlugin = require('offline-plugin') const html = new HtmlWebpackPlugin({template: './src/index.html'}) const offline = new OfflinePlugin module.exports = { entry: './src/index.js', output: { filename: '[name].[hash].js', path: './assets/', }, plugins: [html, offline], } Pitfall: Non-Webpack Assets

Now you probably ask, why does the basic version need another plugin? Well, the offline-plugin has a catch.

It will only cache the assets that are produced by Webpack if it isn't handed any configuration object.

So if you have an index.html , which you don't generate with Webpack, it won't be cached and your whole app won't show when there is no connection.

Using the html-plugin is a way to get around this problem. Then the index.html will be included.

Another way is using externals :

const offline = new OfflinePlugin({ externals: ['index.html'], })

This is also used for assets from another server, for example if you require Bootstrap or jQuery from a content delivery network (CDN.

I found it a bit confusing at first, because I thought of externals as API requests and CDN assets. But it basically means: files not built by Webpack

The basic config won't give you any warning, only if you try to manually configure the caches you will maybe see this:

WARNING in OfflinePlugin: Cache asset [index.html] is not found in the output assets,if it's an external asset, put it to the |externals| option to remove this warning Pitfall: Cached Forever

The basic approach is to require the offline-plugin runtime at the beginning at your code and install it. Which basically means enable caching .

const offlinePluginRuntime = require('offline-plugin/runtime') offlinePluginRuntime.install()

But you don't have to activate the caching automatically on application start. This can lead to unexpected behaviour if you're not used to offline modes.

For example, you could upload another version of a file and give it the same name, so it won't change on the client.

Or you have an API call in externals and now only the cached results are shown.

This can greatly help with debugging.

Conclusion

The offline-plugin is a really easy way to show already downloaded data to your users, even if they aren't online. These days, where more and more people are online via mobile devices, this offline scenario becomes less and less of an exeption.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: CDNBootstrapHTMLCSSjQuery
分页:12
转载请注明
本文标题:Easy Offline First Apps With Webpack's Offline Plugin
本站链接:http://www.codesec.net/view/532865.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(102)