From 5bff73061454dbd3984cf813418da8f15af17328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Ortu=C3=B1o?= Date: Fri, 7 Jun 2024 17:42:32 +0200 Subject: [PATCH] feat(nuxt): allow configuring interval for checking app update (#27324) --- .../nuxt/src/app/plugins/check-outdated-build.client.ts | 6 ++++-- packages/nuxt/src/core/nuxt.ts | 4 +++- packages/nuxt/src/core/templates.ts | 1 + packages/schema/src/config/experimental.ts | 8 ++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/app/plugins/check-outdated-build.client.ts b/packages/nuxt/src/app/plugins/check-outdated-build.client.ts index d8187d04d8..874ea667bd 100644 --- a/packages/nuxt/src/app/plugins/check-outdated-build.client.ts +++ b/packages/nuxt/src/app/plugins/check-outdated-build.client.ts @@ -4,6 +4,8 @@ import type { NuxtAppManifestMeta } from '../composables/manifest' import { onNuxtReady } from '../composables/ready' // @ts-expect-error virtual file import { buildAssetsURL } from '#internal/nuxt/paths' +// @ts-expect-error virtual file +import { outdatedBuildInterval } from '#build/nuxt.config.mjs' export default defineNuxtPlugin((nuxtApp) => { if (import.meta.test) { return } @@ -13,7 +15,7 @@ export default defineNuxtPlugin((nuxtApp) => { async function getLatestManifest () { const currentManifest = await getAppManifest() if (timeout) { clearTimeout(timeout) } - timeout = setTimeout(getLatestManifest, 1000 * 60 * 60) + timeout = setTimeout(getLatestManifest, outdatedBuildInterval) try { const meta = await $fetch(buildAssetsURL('builds/latest.json') + `?${Date.now()}`) 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) }) }) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 8b5d389967..e319d53d7c 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -508,7 +508,9 @@ async function initNuxt (nuxt: Nuxt) { 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) => { diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index de33767cd4..49a89f8943 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -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 viewTransition = ${ctx.nuxt.options.experimental.viewTransition}`, `export const appId = ${JSON.stringify(ctx.nuxt.options.appId)}`, + `export const outdatedBuildInterval = ${ctx.nuxt.options.experimental.checkOutdatedBuildInterval}`, ].join('\n\n') }, } diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index f429a873e8..bf984023fc 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -319,6 +319,14 @@ export default defineUntypedSchema({ */ 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. *