简体   繁体   English

使用Cordova服务于本地静态网站?

[英]Serving a local static website using Cordova?

We are currently starting work on a project which consists of a website, plus Apps for Android, iOS and (yes) Windows phone. 我们目前正在开始一个项目,该项目包括一个网站,以及适用于Android,iOS和(是)Windows Phone的Apps。

The apps are identical on all three platforms, and are relatively simple, at least in theory. 这些应用在所有三个平台上都是相同的,并且至少在理论上相对简单。

  1. They need to provide offline access to the content on the site. 他们需要提供对网站内容的脱机访问。 This is static content, so is a set of .html pages plus assets (images, css, javascript, etc.). 这是静态内容,一组.html页面和资产(图像,css,javascript等)也是如此。
  2. They need to be able to periodically update the content automatically, when an internet connection is available. 当互联网连接可用时,他们需要能够定期自动更新内容。
  3. Any external links should open in the user's internet browser rather than within the app. 任何外部链接都应在用户的互联网浏览器中而不是在应用程序中打开。
  4. The app should not display any browser 'chrome' (eg address bar, etc.). 该应用程序不应显示任何浏览器“ chrome”(例如地址栏等)。

It seems like Cordova is a good way to go with this (though I'm open to other suggestions). 似乎科尔多瓦是解决这个问题的好方法(尽管我愿意接受其他建议)。 However, I'm having difficulty finding any information about how to proceed. 但是,我很难找到有关如何进行的任何信息。

For point 1, the way I'd imagine it working is that the app loads some form of web view pointing to file://path/to/site/index.html. 对于第1点,我以为它起作用的方式是该应用程序加载了某种形式的Web视图,指向文件://path/to/site/index.html。 Assuming the site uses relative URLs for all internal links and assets this should be sufficient to provide a fully browsable offline version of the site. 假设该站点对所有内部链接和资产都使用相对URL,则这应该足以提供该站点的完全可浏览的脱机版本。 The app will ship with a static export of the current site. 该应用程序将附带当前站点的静态导出。

For point 2, I imagined that the Cordova app would occasionally run a synchornisation process, which would essentially be a number of web requests to the website's API to find out if there is new content. 对于第2点,我认为Cordova应用程序偶尔会运行同步化过程,这实际上是向网站API发出大量Web请求,以查找是否有新内容。 It would then download any new files, placing them in the file structure referred to in point 1, overwriting existing files. 然后它将下载所有新文件,并将它们放置在第1点所指的文件结构中,覆盖现有文件。 (For the purposes of this question, I don't care if that temporarily puts the site in an inconsistent state.) (出于这个问题的目的,我不在乎这是否会使网站暂时处于不一致状态。)

Point 3 sounds trivial, but I'm not sure if it is. 第三点听起来微不足道,但我不确定是否。 Would target="_blank" be enough? target =“ _ blank”就足够了吗? Or a bit of JavaScript to trigger some Cordova action when the link is clicked? 还是单击链接时会触发一些JavaScript触发一些Cordova操作? We control the way the HTML is generated, so we can insert additional markup for offline use, if necesssary. 我们控制HTML的生成方式,因此,如有必要,我们可以插入其他标记以供离线使用。

Can anyone offer any advice about how to handle an automatically-updating local static site in this manner? 谁能提供有关如何以这种方式处理自动更新的本地静态站点的任何建议? Is it even possible? 可能吗

Cordova mostly does store your html files in an app and the shows the index.html (Or an other page if you define it) in an WebView. Cordova的确会将您的html文件存储在应用程序中,并在WebView中显示index.html(或其他页面,如果您定义了它)。 The cool thing here is that Cordova will also provide an API to call Device APIs from JS. 这里很酷的一点是,Cordova还将提供一个API来从JS调用设备API。

Cordova does not automatically reload you html files and assets into the app. Cordova不会自动将您的html文件和资产重新加载到应用程序中。 For that you would need to release a new App or you simply reload just the Data in your App and you control all of that in JavaScript. 为此,您将需要发布一个新的应用程序,或者仅重新加载应用程序中的数据,然后使用JavaScript控制所有这些数据。

If you do not need any Device APIs I would recommend you take a look at ServiceWorkers for caching and reloading files. 如果不需要任何设备API,我建议您看一下ServiceWorkers来缓存和重新加载文件。 With some HTML Tags you can also create a really App like behavior after pressing "Add to homescreen". 使用某些HTML标签,您还可以在按“添加到主屏幕”后创建一个类似于App的行为。

Best Regards Marc 最好的问候马克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM