Skip to content

Commit e5f55a6

Browse files
committed
feat: support app router
1 parent 3fcb24a commit e5f55a6

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ <h2><a href="https://leancloud.cn">为开发加速</a></h2>
1111
<p>欢迎调试 JavaScript SDK,请打开浏览器控制台</p>
1212
</div>
1313
<script src="../dist/av.js"></script>
14-
<script src="../test-es5.js"></script>
14+
<script src="./test-es5.js"></script>
1515
</body>
1616
</html>

src/browserify-wrapper/ajax.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,5 @@ module.exports = function _ajax(method, resourceUrl, data, headers = {}) {
6565
});
6666

6767
req.end(JSON.stringify(data));
68-
debug(req);
6968
return promise;
7069
};

src/utils.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -130,39 +130,31 @@ const init = (AV) => {
130130
};
131131

132132
const setRegionServer = (region = 'cn') => {
133-
// AVConfig.region = region;
134-
// AVConfig.APIServerURL = API_HOST[region];
135-
// if (region === 'cn') {
136-
// Cache.get('APIServerURL').then(cachedServerURL => {
137-
// if (cachedServerURL) {
138-
// return cachedServerURL;
139-
// } else {
140-
// return ajax('get', `http://app-router.leancloud.cn/route?appId=${AV.applicationId}`)
141-
// .then(servers => {
142-
// if (servers.api_server) {
143-
// Cache.set('APIServerURL', servers.api_server, 30 * 24 * 3600000);
144-
// return servers.api_server;
145-
// }
146-
// });
147-
// }
148-
// }).then(serverURL => {
149-
// if (AVConfig.APIServerURL === API_HOST[region]) {
150-
// AVConfig.APIServerURL = serverURL;
151-
// }
152-
// })
153-
// }
154-
155-
// 服务器地区选项,默认为中国大陆
156-
switch (region) {
157-
case 'us':
158-
AVConfig.region = 'us';
159-
break;
160-
default:
161-
AVConfig.region = 'cn';
162-
break;
163-
}
164-
if (!AVConfig.APIServerURL) {
165-
AVConfig.APIServerURL = API_HOST[AVConfig.region];
133+
AVConfig.region = region;
134+
// 如果用户在 init 之前设置了 APIServerURL,则跳请求 router
135+
if (AVConfig.APIServerURL) {
136+
return;
137+
}
138+
AVConfig.APIServerURL = API_HOST[region];
139+
if (region === 'cn') {
140+
Cache.get('APIServerURL').then(cachedServerURL => {
141+
if (cachedServerURL) {
142+
return cachedServerURL;
143+
} else {
144+
return ajax('get', `https://app-router.leancloud.cn/1/route?appId=${AV.applicationId}`)
145+
.then(servers => {
146+
if (servers.api_server) {
147+
Cache.set('APIServerURL', servers.api_server, (servers.ttl || 3600) * 1000);
148+
return servers.api_server;
149+
}
150+
});
151+
}
152+
}).then(serverURL => {
153+
// 如果用户在 init 之后设置了 APIServerURL,保持用户设置
154+
if (AVConfig.APIServerURL === API_HOST[region]) {
155+
AVConfig.APIServerURL = `https://${serverURL}`;
156+
}
157+
})
166158
}
167159
};
168160

0 commit comments

Comments
 (0)