mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(nuxt): throw error if setInterval
is used on server (#25259)
This commit is contained in:
parent
c93c526f50
commit
90d8518c84
14
packages/nuxt/src/app/compat/interval.ts
Normal file
14
packages/nuxt/src/app/compat/interval.ts
Normal 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)
|
||||
}
|
@ -29,7 +29,7 @@ export function useRequestHeaders (include?: any[]) {
|
||||
}
|
||||
|
||||
/** @since 3.9.0 */
|
||||
export function useRequestHeader(header: string) {
|
||||
export function useRequestHeader (header: string) {
|
||||
if (import.meta.client) { return undefined }
|
||||
const event = useRequestEvent()
|
||||
return event ? getRequestHeader(event, header) : undefined
|
||||
|
@ -25,6 +25,10 @@ const granularAppPresets: InlinePreset[] = [
|
||||
imports: ['requestIdleCallback', 'cancelIdleCallback'],
|
||||
from: '#app/compat/idle-callback'
|
||||
},
|
||||
{
|
||||
imports: ['setInterval'],
|
||||
from: '#app/compat/interval'
|
||||
},
|
||||
{
|
||||
imports: ['useAppConfig', 'updateAppConfig'],
|
||||
from: '#app/config'
|
||||
|
Loading…
Reference in New Issue
Block a user