feat(nuxt): allow configuring interval for checking app update (#27324)

This commit is contained in:
Israel Ortuño 2024-06-07 17:42:32 +02:00 committed by GitHub
parent 34ea5ae2f2
commit 5bff730614
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 3 deletions

View File

@ -4,6 +4,8 @@ import type { NuxtAppManifestMeta } from '../composables/manifest'
import { onNuxtReady } from '../composables/ready' import { onNuxtReady } from '../composables/ready'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { buildAssetsURL } from '#internal/nuxt/paths' import { buildAssetsURL } from '#internal/nuxt/paths'
// @ts-expect-error virtual file
import { outdatedBuildInterval } from '#build/nuxt.config.mjs'
export default defineNuxtPlugin((nuxtApp) => { export default defineNuxtPlugin((nuxtApp) => {
if (import.meta.test) { return } if (import.meta.test) { return }
@ -13,7 +15,7 @@ export default defineNuxtPlugin((nuxtApp) => {
async function getLatestManifest () { async function getLatestManifest () {
const currentManifest = await getAppManifest() const currentManifest = await getAppManifest()
if (timeout) { clearTimeout(timeout) } if (timeout) { clearTimeout(timeout) }
timeout = setTimeout(getLatestManifest, 1000 * 60 * 60) timeout = setTimeout(getLatestManifest, outdatedBuildInterval)
try { try {
const meta = await $fetch<NuxtAppManifestMeta>(buildAssetsURL('builds/latest.json') + `?${Date.now()}`) const meta = await $fetch<NuxtAppManifestMeta>(buildAssetsURL('builds/latest.json') + `?${Date.now()}`)
if (meta.id !== currentManifest.id) { if (meta.id !== currentManifest.id) {
@ -25,5 +27,5 @@ export default defineNuxtPlugin((nuxtApp) => {
} }
} }
onNuxtReady(() => { timeout = setTimeout(getLatestManifest, 1000 * 60 * 60) }) onNuxtReady(() => { timeout = setTimeout(getLatestManifest, outdatedBuildInterval) })
}) })

View File

@ -508,7 +508,9 @@ async function initNuxt (nuxt: Nuxt) {
global: true, global: true,
}) })
addPlugin(resolve(nuxt.options.appDir, 'plugins/check-outdated-build.client')) if (nuxt.options.experimental.checkOutdatedBuildInterval !== false) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/check-outdated-build.client'))
}
} }
nuxt.hooks.hook('builder:watch', (event, relativePath) => { nuxt.hooks.hook('builder:watch', (event, relativePath) => {

View File

@ -413,6 +413,7 @@ export const nuxtConfigTemplate: NuxtTemplate = {
`export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}`, `export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}`,
`export const viewTransition = ${ctx.nuxt.options.experimental.viewTransition}`, `export const viewTransition = ${ctx.nuxt.options.experimental.viewTransition}`,
`export const appId = ${JSON.stringify(ctx.nuxt.options.appId)}`, `export const appId = ${JSON.stringify(ctx.nuxt.options.appId)}`,
`export const outdatedBuildInterval = ${ctx.nuxt.options.experimental.checkOutdatedBuildInterval}`,
].join('\n\n') ].join('\n\n')
}, },
} }

View File

@ -319,6 +319,14 @@ export default defineUntypedSchema({
*/ */
appManifest: true, appManifest: true,
/**
* Set the time interval (in ms) to check for new builds. Disabled when `experimental.appManifest` is `false`.
*
* Set to `false` to disable.
* @type {number | false}
*/
checkOutdatedBuildInterval: 1000 * 60 * 60,
/** /**
* Set an alternative watcher that will be used as the watching service for Nuxt. * Set an alternative watcher that will be used as the watching service for Nuxt.
* *