Skip to content

Commit

Permalink
fix: 244 pass globalName to plugin client to make sure it works (#251)
Browse files Browse the repository at this point in the history
* fix: 244 pass globalName to plugin client to make sure it works

* fix: use globals.readyCallback instead

Co-authored-by: Sébastien Chopin <[email protected]>
  • Loading branch information
flozero and atinux authored Jul 20, 2020
1 parent 244c8f6 commit 3a6a022
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 62 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ module.exports = async function (moduleOptions) {
if (routerBasePath[routerBasePath.length - 1] === '/') {
routerBasePath = routerBasePath.slice(0, -1)
}

this.addPlugin({
fileName: 'content/plugin.client.js',
src: join(__dirname, 'templates/plugin.static.js'),
Expand All @@ -176,7 +177,8 @@ module.exports = async function (moduleOptions) {
src: join(__dirname, 'templates/plugin.client.js'),
options: {
apiPrefix: options.apiPrefixWithBase,
watch: options.watch
watch: options.watch,
readyCallbackName: this.options.globals.readyCallback(this.options.globalName)
}
})
}
Expand Down
122 changes: 61 additions & 61 deletions lib/templates/plugin.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,68 @@ export default (ctx, inject) => {
inject('content', $content)
ctx.$content = $content

<% if (options.watch) { %>
const logger = require('consola').withScope('@nuxt/content')
if (!window.WebSocket) {
logger.warn('[@nuxt/content] Could not activate hot reload, your browser does not support WebSocket.')
<% if (options.watch) { %>
const logger = require('consola').withScope('@nuxt/content')
if (!window.WebSocket) {
logger.warn('[@nuxt/content] Could not activate hot reload, your browser does not support WebSocket.')
return
}
let ws = null
let $nuxt = null
window['<%= options.readyCallbackName %>']((_nuxt) => {
$nuxt = _nuxt
})
const wsMessage = async function (message) {
try {
const data = JSON.parse(message.data)
if (!data) {
return
}
let ws = null
let $nuxt = null
window.onNuxtReady((_nuxt) => {
$nuxt = _nuxt
})
const wsMessage = async function (message) {
try {
const data = JSON.parse(message.data)
if (!data) {
return
}
$nuxt.$emit('content:update', data)
// Nuxt3: await $nuxt.callHook('content:update')
if ($nuxt.$store && $nuxt.$store._actions.nuxtServerInit) {
await $nuxt.$store.dispatch('nuxtServerInit', $nuxt.$options.context)
}
$nuxt.refresh()
} catch (err) { }
}
const wsOpen = () => logger.log('WS connected')
const wsError = function (e) {
switch (e.code) {
case 'ECONNREFUSED':
wsConnect(true)
break
default:
logger.warn('WS Error:', e)
break
}
}
const wsClose = function (e) {
// https://tools.ietf.org/html/rfc6455#section-11.7
if (e.code === 1000 || e.code === 1005) {
// Normal close
logger.log('WS closed!')
} else {
// Unkown error
wsConnect(true)
}
$nuxt.$emit('content:update', data)
// Nuxt3: await $nuxt.callHook('content:update')
if ($nuxt.$store && $nuxt.$store._actions.nuxtServerInit) {
await $nuxt.$store.dispatch('nuxtServerInit', $nuxt.$options.context)
}
const wsConnect = function (retry) {
if (retry) {
logger.log('WS reconnecting...')
setTimeout(wsConnect, 1000)
return
}
const protocol = location.protocol === 'https:' ? 'wss' : 'ws'
const wsPath = '/<%= options.apiPrefix %>/ws'
const wsURL = `${protocol}://${location.hostname}:${location.port}${wsPath}`
logger.log(`WS connect to ${wsURL}`)
ws = new WebSocket(wsURL)
ws.onopen = wsOpen
ws.onmessage = wsMessage
ws.onerror = wsError
ws.onclose = wsClose
}
wsConnect()
<% } %>
$nuxt.refresh()
} catch (err) { }
}
const wsOpen = () => logger.log('WS connected')
const wsError = function (e) {
switch (e.code) {
case 'ECONNREFUSED':
wsConnect(true)
break
default:
logger.warn('WS Error:', e)
break
}
}
const wsClose = function (e) {
// https://tools.ietf.org/html/rfc6455#section-11.7
if (e.code === 1000 || e.code === 1005) {
// Normal close
logger.log('WS closed!')
} else {
// Unkown error
wsConnect(true)
}
}
const wsConnect = function (retry) {
if (retry) {
logger.log('WS reconnecting...')
setTimeout(wsConnect, 1000)
return
}
const protocol = location.protocol === 'https:' ? 'wss' : 'ws'
const wsPath = '/<%= options.apiPrefix %>/ws'
const wsURL = `${protocol}://${location.hostname}:${location.port}${wsPath}`
logger.log(`WS connect to ${wsURL}`)
ws = new WebSocket(wsURL)
ws.onopen = wsOpen
ws.onmessage = wsMessage
ws.onerror = wsError
ws.onclose = wsClose
}
wsConnect()
<% } %>
}

0 comments on commit 3a6a022

Please sign in to comment.