feat(nuxt): throw error if setInterval is used on server (#25259)

This commit is contained in:
Sébastien Chopin 2024-01-19 23:38:15 +01:00 committed by GitHub
parent c93c526f50
commit 90d8518c84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 1 deletions

View File

@ -0,0 +1,14 @@
import { createError } from '../composables/error'
const intervalError = '[nuxt] `setInterval` should not be used on the server. Consider wrapping it with an `onNuxtReady`, `onBeforeMount` or `onMounted` lifecycle hook, or ensure you only call it in the browser by checking `import.meta.client`.'
export const setInterval = import.meta.client ? window.setInterval : () => {
if (import.meta.dev) {
throw createError({
statusCode: 500,
message: intervalError
})
}
console.error(intervalError)
}

View File

@ -29,7 +29,7 @@ export function useRequestHeaders (include?: any[]) {
} }
/** @since 3.9.0 */ /** @since 3.9.0 */
export function useRequestHeader(header: string) { export function useRequestHeader (header: string) {
if (import.meta.client) { return undefined } if (import.meta.client) { return undefined }
const event = useRequestEvent() const event = useRequestEvent()
return event ? getRequestHeader(event, header) : undefined return event ? getRequestHeader(event, header) : undefined

View File

@ -25,6 +25,10 @@ const granularAppPresets: InlinePreset[] = [
imports: ['requestIdleCallback', 'cancelIdleCallback'], imports: ['requestIdleCallback', 'cancelIdleCallback'],
from: '#app/compat/idle-callback' from: '#app/compat/idle-callback'
}, },
{
imports: ['setInterval'],
from: '#app/compat/interval'
},
{ {
imports: ['useAppConfig', 'updateAppConfig'], imports: ['useAppConfig', 'updateAppConfig'],
from: '#app/config' from: '#app/config'