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

Commit 816b330

Browse files
authored
Merge pull request #5 from WittBulter/improve-page-loader
feat(pages): improve page loader
2 parents a3bf553 + 5ab4591 commit 816b330

File tree

6 files changed

+25
-32
lines changed

6 files changed

+25
-32
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
1010
>use **back-loader** in your project, you can quickly preload more pictures, styles(.css) or scripts, it will make your website more fluent.
1111
12+
According to my data,use `back-loader` preload all page in production project, DomReadyTime < 200 ms !!!
13+
the user experience of each page can be greatly improved.
1214
<br>
1315

1416
#### Usage
@@ -39,15 +41,14 @@ type loadEvent = {
3941
insertScripts?: Function,
4042
}
4143
```
42-
<br>
43-
44-
#### More
4544
46-
1. the resources of different domain names can be loaded normally.
47-
2. if you use `page`, `back-loader` automatically analyzes every resources included in the page. but domain name needs to be the same as the current page. (of course)
45+
For example, i want preload `google.com/new-product.html` page, then append the above code on the home page (or any other pages),
46+
like `setTimeout(() => backHandler.start(), 1000)`, browser will load all resources and caching to memory or disk,
47+
waiting for the user to browse `google.com/new-product.html`,just need few millisecond!
4848
4949
<br>
5050
51+
5152
#### LICENSE
5253
5354
**MIT**

examples/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ document.title = 'hello'
44
const handler = async() => {
55
const backLoader = new BackLoader({
66
// scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
7-
// pages: ['https://google.com'],
8-
images: ['https://www.bing.com/az/hprichbg/rb/TadamiTrain_ROW14602114613_1920x1080.jpg'],
7+
pages: ['https://www.baidu.com'],
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.3",
3+
"version": "1.0.0",
44
"description": "a easy and steady preload lib",
55
"main": "release/index",
66
"unpkg": "release/index.js",

src/core/loader.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventHub } from './event'
22
import { LoaderEvent } from '../types'
3-
import { $fetch, filterResources, listenImageLoad } from '../utils/tools'
3+
import { $fetch, hiddenIframe, listenImageLoad } from '../utils/tools'
44

55

66
export class Loader {
@@ -22,16 +22,9 @@ export class Loader {
2222

2323
pages(urls: string[]): void {
2424
urls.forEach(url => {
25-
$fetch(url, { mode: 'no-cors' })
26-
.then(html => {
27-
this.scripts(filterResources(html, 'script'))
28-
this.styles(filterResources(html, 'style'))
29-
})
30-
.catch((e) => {
31-
this.emit(Object.assign({}, this.baseEvent, {
32-
type: 'page', source: url, success: false,
33-
}))
34-
})
25+
const iframe: HTMLIFrameElement = document.createElement('iframe')
26+
iframe.src = url
27+
document.body.appendChild(hiddenIframe(iframe))
3528
})
3629
}
3730

src/utils/tools.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ export const filterResources = (source: string, type: string): string[] => {
3333
export const listenImageLoad = (images: HTMLImageElement[], done: (url: string) => void)
3434
: void => {
3535
images.forEach(img => img.onload = () => done(img.src))
36-
// const isCompleted = (imgs: HTMLImageElement[]) => !imgs.length
37-
// const timer: number = window.setInterval(() => {
38-
//
39-
// // images = images.map(img => {
40-
// // console.log(img)
41-
// // if (!img.complete) return img
42-
// // done(img.src)
43-
// // return null
44-
// // })
45-
// // .filter(v => !!v)
46-
// isCompleted(images) && clearInterval(timer)
47-
// }, 300)
36+
}
37+
38+
export const hiddenIframe = (iframe: HTMLIFrameElement): HTMLIFrameElement => {
39+
iframe.style.opacity = '0'
40+
iframe.style.position = 'fixed'
41+
iframe.style.top = '-20000px'
42+
iframe.style.left = '-20000px'
43+
iframe.style.zIndex = '-100'
44+
iframe.style.width = '1px'
45+
iframe.style.height = '1px'
46+
return iframe
4847
}

0 commit comments

Comments
 (0)