refactor: warn when setting useResponseHeader on client

This commit is contained in:
Daniel Roe 2024-09-19 15:07:00 +01:00
parent f19f208ded
commit 81ef3c247d
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 20 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3' import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3'
import { computed, getCurrentInstance } from 'vue' import { computed, getCurrentInstance, ref } from 'vue'
import { useServerHead } from '@unhead/vue' import { useServerHead } from '@unhead/vue'
import type { NuxtApp } from '../nuxt' import type { NuxtApp } from '../nuxt'
@ -63,10 +63,26 @@ export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: nu
/** @since 3.14.0 */ /** @since 3.14.0 */
export function useResponseHeader (header: string) { export function useResponseHeader (header: string) {
if (import.meta.client) { return {} } if (import.meta.client) {
if (import.meta.dev) {
return computed({
get: () => undefined,
set: () => console.warn('[nuxt] Setting response headers is not supported in the browser.'),
})
}
return ref()
}
const event = useRequestEvent() const event = useRequestEvent()
if (!event) { return {} } if (!event) {
if (import.meta.dev) {
return computed({
get: () => undefined,
set: () => console.warn('[nuxt] Setting response headers is not supported in the browser.'),
})
}
return ref()
}
return computed({ return computed({
get () { get () {

View File

@ -395,7 +395,7 @@ describe('ssr composables', () => {
expect(useRequestFetch()).toEqual($fetch) expect(useRequestFetch()).toEqual($fetch)
expect(useRequestHeaders()).toEqual({}) expect(useRequestHeaders()).toEqual({})
expect(prerenderRoutes('/')).toBeUndefined() expect(prerenderRoutes('/')).toBeUndefined()
expect(useResponseHeader()).toEqual({}) expect(useResponseHeader('x-test').value).toBeUndefined()
}) })
}) })