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

Does it work with nuxt 2? #2

Open
TouchSek opened this issue Jul 26, 2023 · 3 comments
Open

Does it work with nuxt 2? #2

TouchSek opened this issue Jul 26, 2023 · 3 comments

Comments

@TouchSek
Copy link

I want to use this plugin with nuxt 2, but I do not really it's working or not, please let me know. Thanks.

@IlyaSemenov
Copy link
Owner

I suppose it won't work in plain Nuxt 2 as-is, as it uses nitro for server middleware which I believe only appeared in Nuxt Bridge?

The approach itself works in Nuxt 2 (I've been using it for years until I published it to npm). You can copy files from src into your project and adapt accordingly.

@TouchSek
Copy link
Author

@IlyaSemenov do you have any work around to play with nuxt 2?

@IlyaSemenov
Copy link
Owner

@TouchSek I extracted the following from git history of my private project, it was used with nuxt 2.15.8.

Module:

import { Module } from '@nuxt/types/config/module'
import path from 'path'

interface ModuleOptions {
	path: string
	check_interval: number
	version: string
}

const default_options: ModuleOptions = {
	path: '/version.json',
	check_interval: 1000 * 60 * 5, // 5 minutes
	version: null,
}

const nuxt_module: Module<Partial<ModuleOptions>> = function (module_options) {
	const options: ModuleOptions = { ...default_options, ...module_options }
	if (!options.version) {
		console.log('nuxt-detect-update: version not provided, disabled.')
		return
	}
	this.addServerMiddleware({
		path: options.path,
		handler(req, res, next) {
			res.statusCode = 200
			res.setHeader('Content-Type', 'application/json')
			res.setHeader('Cache-Control', 'no-cache')
			res.end(JSON.stringify({ version: options.version }))
		},
	})
	this.addPlugin({
		src: path.resolve(__dirname, 'plugin.js'),
		options,
		mode: 'client',
	})
}

export default nuxt_module

Module plugin (plugin.js):

const options = JSON.parse('<%= JSON.stringify(options) %>')

export default ({ app }) => {
	let last_check_time = new Date()
	app.router.afterEach(async () => {
		const last_check_interval = new Date() - last_check_time
		if (last_check_interval >= options.check_interval) {
			last_check_time = new Date()
			const version = await get_remote_version()
			if (version !== options.version) {
				options.version = version
				window.$nuxt.$emit('nuxt-detect-update', version)
			}
		}
	})
}

async function get_remote_version() {
	return new Promise((resolve, reject) => {
		const xhr = new XMLHttpRequest()
		xhr.open('GET', options.path)
		xhr.onload = function () {
			if (xhr.status === 200) {
				try {
					const { version } = JSON.parse(xhr.responseText)
					if (version) {
						resolve(version)
					} else {
						reject('Malformed version response.')
					}
				} catch (err) {
					reject(err)
				}
			} else {
				reject('Request failed.')
			}
		}
		xhr.send()
	})
}

User-land Nuxt plugin:

export default function () {
	window.onNuxtReady(() => {
		window.$nuxt.$on('nuxt-detect-update', () => {
			window.$nuxt.$buefy.snackbar.open({
				message: 'Click Update to load new version',
				type: 'is-warning',
				position: 'is-bottom',
				actionText: 'Upload',
				indefinite: true,
				onAction: () => {
					document.location.reload(true)
				},
			})
		})
	})
}

Hope this helps. Please note I unlikely will be able to assist with this legacy code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants