Skip to content

Commit 46bd50a

Browse files
authored
feat: fallback to fetch if ws timesout (#6)
1 parent d2474e7 commit 46bd50a

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

app/app.vue

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<style src="./css/fonts.css"></style>
4040

4141
<script>
42+
import fetch from 'unfetch'
4243
import capitalizeMixin from './mixins/capitalize'
4344
import logMixin from './mixins/log'
4445
import wsMixin from './mixins/ws'
@@ -74,19 +75,54 @@ export default {
7475
},
7576
7677
mounted() {
77-
this.onWSData(window.__STATE__)
78+
this.onData(window.__STATE__)
7879
this.wsConnect('/_loading/ws')
80+
this.setTimeout()
7981
},
8082
8183
methods: {
8284
onWSData(data) {
85+
// We have data from ws. Delay timeout!
86+
this.setTimeout()
87+
88+
this.onData(data)
89+
},
90+
91+
async fetchData() {
92+
// Prevent any fetch happening during fetch
93+
this.clearTimeout()
94+
95+
try {
96+
const data = await fetch('/_loading/json').then(res => res.json())
97+
this.onData(data)
98+
} catch (e) {
99+
this.logError(e)
100+
}
101+
102+
// Start next timeout
103+
this.setTimeout()
104+
},
105+
106+
clearTimeout() {
107+
if (this._fetchTimeout) {
108+
clearTimeout(this._fetchTimeout)
109+
}
110+
},
111+
112+
setTimeout() {
113+
this.clearTimeout()
114+
this._fetchTimeout = setTimeout(() => this.fetchData(), 1000)
115+
},
116+
117+
onData(data) {
83118
if (!data || !data.states) {
84119
return
85120
}
86121
87122
let isFinished = true
88123
89124
this.bundles = data.states.map(state => state.name.toLowerCase())
125+
90126
// Ignore if not bundle is given
91127
if (!this.bundles.length) {
92128
return
@@ -128,12 +164,14 @@ export default {
128164
return
129165
}
130166
131-
// Clear console
132-
this.clearConsole()
133-
167+
// Replace document with new page
134168
document.open()
135169
document.write(html)
136170
document.close()
171+
172+
// Clear console and timeout
173+
this.clearConsole()
174+
this.clearTimeout()
137175
}
138176
}
139177
}

lib/loading.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class LoadingUI {
1010

1111
this.serveIndex = this.serveIndex.bind(this)
1212
this.serveWS = this.serveWS.bind(this)
13+
this.serveJSON = this.serveJSON.bind(this)
1314

1415
this._lastBroadCast = 0
1516
this.allDone = true
@@ -23,6 +24,9 @@ class LoadingUI {
2324
this.wss = new WebSocket.Server({ noServer: true })
2425
this.app.use('/ws', this.serveWS)
2526

27+
// Serve dist
28+
this.app.use('/json', this.serveJSON)
29+
2630
// Serve dist
2731
const distPath = path.resolve(__dirname, '..', 'app-dist')
2832
this.app.use('/', serveStatic(distPath))
@@ -74,6 +78,10 @@ class LoadingUI {
7478
))
7579
}
7680

81+
serveJSON(req, res) {
82+
res.end(JSON.stringify(this.state))
83+
}
84+
7785
serveWS(req) {
7886
this.handleUpgrade(req, req.socket, undefined)
7987
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"eslint-plugin-vue": "^5.2.2",
3434
"poi": "^12.5.7",
3535
"standard-version": "^5.0.2",
36+
"unfetch": "^4.1.0",
3637
"vue": "^2.6.10",
3738
"vue-template-compiler": "^2.6.10"
3839
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7338,6 +7338,11 @@ uglify-js@3.4.x, uglify-js@^3.1.4:
73387338
commander "~2.19.0"
73397339
source-map "~0.6.1"
73407340

7341+
unfetch@^4.1.0:
7342+
version "4.1.0"
7343+
resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.1.0.tgz#6ec2dd0de887e58a4dee83a050ded80ffc4137db"
7344+
integrity sha512-crP/n3eAPUJxZXM9T80/yv0YhkTEx2K1D3h7D1AJM6fzsWZrxdyRuLN0JH/dkZh1LNH8LxCnBzoPFCPbb2iGpg==
7345+
73417346
unicode-canonical-property-names-ecmascript@^1.0.4:
73427347
version "1.0.4"
73437348
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)