Rexxar 是一個針對移動端的混合開發(fā)框架√贫希現(xiàn)在支持 Android 和 iOS 平臺盟榴。
詳情可參考:https://lincode.github.io/Rexxar-OpenSource
1 緩存策略
2 Rexxar Container?
Rexxar Container 主要的工作是截獲 Rexxar Web 的數(shù)據(jù)請求和原生功能請求拔第。通過從 Rexxar Web 發(fā)出 HTTP 請求的方式史辙,由 Rexxar Container 截獲的方式進行通信份氧。Native 和 Web 之間協(xié)議是由 URL 定義的亮航。Rexxar Web 訪問某個特定的 URL, Rexxar Container 截獲這些 URL 請求同辣,調(diào)用 Native 代碼完成相應(yīng)的功能拷姿。
2.1 RexxarWidget
作用:調(diào)用某些 Native UI 組件,例如調(diào)起一個 Toast旱函。
實現(xiàn)方式:通過 WebView 回調(diào) shouldOverrideUrlLoading 方法進行攔截响巢。
Android官網(wǎng)解釋:
public boolean shouldOverrideUrlLoading (WebView?view,?WebResourceRequest?request)
Give the host application a chance to take control when a URL is about to be loaded in the current WebView. If a WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the URL. If a WebViewClient is provided, returning?true?causes the current WebView to abort loading the URL, while returning?false?causes the WebView to continue loading the URL as usual.
shouldOverrideUrlLoading?主要是給WebView提供時機,讓其選擇是否對UrlLoading進行攔截棒妨。返回true表示進行攔截踪古,實現(xiàn)自己的邏輯;返回false則表示不攔截,webview繼續(xù)加載該url灾炭。
另外茎芋,如果H5需要callback返回數(shù)據(jù),Rexxar提供了原生回調(diào)H5的方法蜈出。
2.2 RexxarContainerAPI
作用:給 Web 一個 Native 的計算結(jié)果田弥。例如,給出當(dāng)前位置信息铡原。
實現(xiàn)方式:通過 WebView 回調(diào) shouldInterceptRequest方法進行攔截偷厦。
Android官網(wǎng)解釋:
public?WebResourceResponse?shouldInterceptRequest (WebView?view,?WebResourceRequest?request)
Notify the host application of a resource request and allow the application to return the data. If the return value is?null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used.
This callback is invoked for a variety of URL schemes (e.g.,?http(s):,?data:,?file:, etc.), not only those schemes which send requests over the network. This is not called for?javascript:?URLs,?blob:?URLs, or for assets accessed via?file:///android_asset/?or?file:///android_res/?URLs.
In the case of redirects, this is only called for the initial resource URL, not any subsequent redirect URLs.
如果該方法返回null,WebView將會按照平常一樣繼續(xù)加載燕刻;否則只泼,將會使用返回的WebResourceResponse內(nèi)容,也就節(jié)省了 WebView 訪問網(wǎng)絡(luò)資源卵洗,這個方法在后面緩存會有很大作用请唱。
Rexxar 攔截 html,js資源直接渲染進程返回过蹂,圖片等其他資源先返回空的數(shù)據(jù)流再異步向流中寫數(shù)據(jù)十绑。具體邏輯可以參考RexxarWebViewClient的handleResourceRequest方法。
3 可能存在的問題
H5頁面有更新酷勺,服務(wù)端更新router.json本橙,用戶可以在客戶端查看到最新的界面。如果用戶清除數(shù)據(jù)脆诉,并斷開網(wǎng)絡(luò)甚亭,這時再進入則只能加載預(yù)置在asset的H5頁面,從而出現(xiàn)前后不一致的情況击胜,當(dāng)然這種情況很極端亏狰。
4 項目實現(xiàn)
默認(rèn)從/data/data下取緩存,沒有則從asset取潜的,同時從服務(wù)器下載資源包(做版本對比)并保存到/data/data骚揍。具體流程圖待補充。