admin管理员组文章数量:1432632
I have an electron app that retrieves the app files (.html & .js) from a remote server using the function mainWindow.loadURL('.html')
The problem arises if the users network connection to the internet is offline or disconnected.
Is there a way in electron to cache the html and js files so that if the user is offline, electron will automatically load from the cache.
I have tried to use the HTML5 Application Cache and a plugin for webpack but these do not seem to work.
I have an electron app that retrieves the app files (.html & .js) from a remote server using the function mainWindow.loadURL('http://www.example./index.html')
The problem arises if the users network connection to the internet is offline or disconnected.
Is there a way in electron to cache the html and js files so that if the user is offline, electron will automatically load from the cache.
I have tried to use the HTML5 Application Cache and a plugin for webpack https://github./NekR/offline-plugin but these do not seem to work.
Share Improve this question asked Dec 27, 2016 at 13:39 samb90samb90 1,0734 gold badges17 silver badges35 bronze badges 2- Why not bundle the static content inside the app. – Sayam Qazi Commented Jun 23, 2018 at 17:21
- 1 because that what made the app more portable and only if the server change the data we gonna reload it – l1nuxuser Commented Feb 11, 2019 at 10:05
2 Answers
Reset to default 5I see this is an old question but I stumbled across this when doing a semi-related search and there is no answer at all right now, so I'll provide one:
Ignoring the Electron-specific nature of this question, the web-standard way to do this is using Service Workers. Here are some docs on that:
- "Using Service Workers" from MDN - https://developer.mozilla/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers - this is a reference source.
- "Adding a Service Worker and Offline into your Web App" - https://developers.google./web/fundamentals/codelabs/offline/ - this is a tutorial.
- "Creating Offline-First Web Apps with Service Workers" - https://auth0./blog/creating-offline-first-web-apps-with-service-workers/ - this is also a tutorial.
I think this would be the most direct way to solve this, even within Electron. (An advantage of Electron here is that you have a single, known browser to make this work for, but I think what you are trying to do fits perfectly within the problem-space that Service Workers are designed to address.)
That said, I think Sayam's ment/question is valid -- if this html/js is the actual content of your electron app, and assuming it doesn't change too often you could (and maybe should) distribute it with the app itself. Then you don't need to do anything special for offline support (as long as that html/js doesn't need network-based resources), and changes to that code are deployed as application updates.
Personally I think that once-per-week is about the maximum frequency of updates for which this approach is suitable. It would not bother me if an app auto-updated 2 or 3 times per month, but I think I'd uninstall an app that updates itself 2 or 3 times per week if I had that option.
There may also be some electron and/or node modules that address this problem-space, but I've never bothered to look because one of the two options above has always seemed appropriate to me.
Old question but still valid usecase (offline cache for dynamic assets). here is article that describes one solution for that (own ExpressJS caching middleware). Author made npm library to address that.
本文标签: javascriptElectronJSCache HTML and JS files from remote serverStack Overflow
版权声明:本文标题:javascript - ElectronJS - Cache HTML and JS files from remote server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581553a2664650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论