Skip to content

Commit

Permalink
feat: 增加获取原mip链接方法-对应cacheUrl (#295)
Browse files Browse the repository at this point in the history
* feat: 增加获取原mip链接方法-对应cacheUrl
  • Loading branch information
jennyliang220 authored Apr 8, 2018
1 parent 46b1f90 commit 3ffff5e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mip",
"version": "1.0.79",
"version": "1.0.80",
"description": "mobile instant page",
"main": "dist/mip.js",
"dependencies": {},
Expand Down
12 changes: 11 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ define(function (require) {
'use strict';

var fn = require('./utils/fn');
var hash = require('./hash');

/**
* Exchange a url to cache url.
Expand Down Expand Up @@ -70,11 +71,20 @@ define(function (require) {

/**
* 获取页面原mip url,可以将页面mip-cache url处理为原页面
* 由于cache-url可能会被改写,需要还原
*
* @return {string} 原mip页URL
*/
function getOriginalUrl() {
return parseCacheUrl(window.location.href);
var parsedUrl = parseCacheUrl(window.location.href);
if(parsedUrl == window.location.href) {
// 直接打开MIP页
return parsedUrl;
}
// mip-cache页面
var urlWithoutHash = parsedUrl.split('#')[0];
var originHash = hash.get('mipanchor');
return urlWithoutHash + (originHash.length ? '#' : '') + originHash;
}

return {
Expand Down
15 changes: 2 additions & 13 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,8 @@ define(function (require) {
});

describe('.getOriginalUrl', function () {
var stub = sinon.stub(util, "getOriginalUrl", function (str) {
return util.parseCacheUrl(str);
});

it('mip url', function () {
expect(util.getOriginalUrl('https://www.mipengine.com')).to.equal('https://www.mipengine.com');
expect(util.getOriginalUrl('http://www.mipengine.com')).to.equal('http://www.mipengine.com');
expect(util.getOriginalUrl('http://www.mipengine.com/c/index.html')).to.equal('http://www.mipengine.com/c/index.html');
});
it('mip-Cache url', function () {
expect(util.getOriginalUrl('//mipcache.bdstatic.com/c/www.mipengine.org')).to.equal('http://www.mipengine.org');
expect(util.getOriginalUrl('//mipcache.bdstatic.com/c/s/www.mipengine.org')).to.equal('https://www.mipengine.org');
});
// 由于getOriginalUrl 直接获取window.location,不能 sinon.stub, 跳过
// 由于 hash.get 取的是 window.location 而不是传入的 url,mip-cache+hash情况单测跳过
})
});
});
Expand Down

0 comments on commit 3ffff5e

Please sign in to comment.