Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listen on new port #60

Merged
merged 4 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/loading/app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default {
}

this.onData(window.$STATE)
this.sseConnect(`${this.baseURL}_loading/sse`)
this.sseConnect(`${this.baseURL}/sse`)
this.setTimeout()
},

Expand All @@ -135,7 +135,7 @@ export default {
this.clearTimeout()

try {
const data = await fetch(`${this.baseURL}_loading/json`).then(res => res.json())
const data = await fetch(`${this.baseURL}/json`).then(res => res.json())
this.onData(data)
} catch (e) {
this.logError(e)
Expand Down
42 changes: 40 additions & 2 deletions packages/loading/lib/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const { parseStack } = require('./utils/error')
const SSE = require('./sse')

class LoadingUI {
constructor ({ baseURL }) {
constructor () {
// Create a connect middleware stack
this.app = connect()

// Create an SSE handler instance
this.sse = new SSE()

this.baseURL = baseURL
this.baseURL = ''
this._lastBroadCast = 0

this.states = []
Expand All @@ -26,6 +26,12 @@ class LoadingUI {
}

async init () {
// Fix CORS
this.app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
next()
})

// Subscribe to SSR channel
this.app.use('/sse', (req, res) => this.sse.subscribe(req, res))

Expand All @@ -40,6 +46,38 @@ class LoadingUI {
const indexPath = resolve(distPath, 'index.html')
this.indexTemplate = await fs.readFile(indexPath, 'utf-8')
this.app.use('/', this.serveIndex)

// Start listening
await this._listen()
}

_listen () {
return new Promise((resolve, reject) => {
if (this._server) {
return resolve()
}
this._server = this.app.listen(0, (err) => {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (err) {
return reject(err)
}
const { port } = this._server.address()
this.baseURL = `http://localhost:${port}`
resolve()
})
})
}

close () {
if (this._server) {
return new Promise((resolve, reject) => {
this._server.close((err) => {
if (err) {
return reject(err)
}
resolve()
})
})
}
}

get state () {
Expand Down
11 changes: 5 additions & 6 deletions packages/loading/lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ module.exports = async function NuxtLoadingScreen () {
return
}
const LoadingUI = require('./loading')

const baseURL = this.options.router.base
const loading = new LoadingUI({ baseURL })
const loading = new LoadingUI()
await loading.init()

this.addServerMiddleware({
path: '/_loading', // baseURL will be prepended by nuxt for middleware
handler: loading.app
this.nuxt.options._loadingScreenBaseURL = loading.baseURL

this.nuxt.hook('close', async () => {
await loading.close()
})

this.nuxt.hook('bundler:progress', (states) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/loading/poi.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
entry: './app/index.js',
output: {
publicUrl: '{BASE_URL}_loading/',
publicUrl: '{BASE_URL}',
dir: 'app-dist',
html: {
template: 'app/index.html',
Expand Down