Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 18c457c

Browse files
committed
fix: use no-cors in page fetch
fix: use no-cors in page fetch build(pkg): v0.1.2 fix(event): clean unused record
1 parent 70185f8 commit 18c457c

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

examples/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const handler = async() => {
55
const backLoader = new BackLoader({
66
// scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
77
// pages: ['https://google.com'],
8-
images: ['https://cn.bing.com/az/hprichbg/rb/BarHarborCave_ZH-CN8055769470_1920x1080.jpg'],
8+
images: ['https://www.bing.com/az/hprichbg/rb/TadamiTrain_ROW14602114613_1920x1080.jpg'],
99
})
1010
backLoader.start().on(event => {
1111
console.log(event)

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "back-loader",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "",
55
"main": "release/index",
66
"unpkg": "release/index.js",

src/core/event.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ export class EventHub {
3939
// just remove one
4040
if (done) return this.removeOne(eventType, done)
4141
// remove this type
42-
this.eventRecords
43-
.filter(re => re.type === eventType)
44-
.forEach(re => this.source.removeEventListener(re.type, re.handle))
42+
this.eventRecords = this.eventRecords
43+
.map(re => {
44+
if (re.type !== eventType) return re
45+
this.source.removeEventListener(re.type, re.handle)
46+
return null
47+
})
48+
.filter(r => r)
4549
}
4650

4751
removeAll(): void {
4852
this.eventRecords
49-
.forEach(re => this.source.removeEventListener(re.type, re.handle))
53+
.forEach(re => this.source.removeEventListener(re.type, re.handle))
54+
this.eventRecords = []
5055
}
5156

5257
private removeOne(eventType: string, done?: EventHubListener): void {
53-
const record: EventRecord = this.eventRecords.find(record => {
58+
const index: number = this.eventRecords.findIndex(record => {
5459
return record.type === eventType && record.done === done
5560
})
56-
if (!record) return
57-
this.source.removeEventListener(eventType, record.handle)
61+
if (index < 0) return
62+
this.source.removeEventListener(eventType, this.eventRecords[index].handle)
63+
this.eventRecords.splice(index, 1)
5864
}
5965

6066
}

src/core/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export class Loader {
2222

2323
pages(urls: string[]): void {
2424
urls.forEach(url => {
25-
$fetch(url, { mode: 'cors' })
25+
$fetch(url, { mode: 'no-cors' })
2626
.then(html => {
2727
this.scripts(filterResources(html, 'script'))
2828
this.styles(filterResources(html, 'style'))
2929
})
30-
.catch(() => {
30+
.catch((e) => {
3131
this.emit(Object.assign({}, this.baseEvent, {
3232
type: 'page', source: url, success: false,
3333
}))

src/utils/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// resource load use no-cors mode default
22
// html load must use cors
33
export const $fetch = (url: string, init: RequestInit = {}) => {
4-
return fetch(url, Object.assign({ mode: 'no-cors' }, init))
4+
return fetch(url, Object.assign({ mode: 'cors', cache: 'reload' }, init))
55
.then(r => r.text())
66
}
77

0 commit comments

Comments
 (0)