一種選擇是使用onEnter
事件
const projectCanonnicalAddr = "http://localhost";
function cacheQueryParser(query) {
let out = '';
if (typeof query === 'string') {
out = query.split(':').pop().replace(/^[^/]*/, '');
}
return out;
}
function intercepPath(next, replace) {
if (next.location.pathname === '/search'
&& next.location.query.q
&& next.location.query.q.indexOf('cache') === 0
&& next.location.query.q.indexOf(projectCanonnicalAddr) > -1) {
replace(null, cacheQueryParser(next.location.query.q));
}
};
在此之後,對於包羅萬象的路線定義,你可以使用這樣的攔截路由邏輯:
<Route path="*" component={PageNotFoundApp.container} onEnter={intercepPath}/>
請注意,使用注入的replace
函數實際上會將瀏覽器導航到作爲第二個參數提供的路徑。在谷歌緩存的情況下,我沒有測試過它,這可能是一個錯誤的實現。 作爲一個選項,您可以傳遞一個有效的狀態作爲此函數的第一個參數。