Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: polyfill event for useRequestEvent when nitro is disabled #999

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/bridge/src/runtime/composables/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { H3Event } from 'h3'
import type { NuxtAppCompat } from '@nuxt/bridge-schema'
import { getRequestHeaders } from 'h3'
import { getRequestHeaders, createEvent } from 'h3'
import { useNuxtApp } from '../nuxt'

export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
Expand All @@ -14,5 +14,11 @@ export function useRequestHeaders (include?: any[]) {
}

export function useRequestEvent (nuxtApp: NuxtAppCompat = useNuxtApp()): H3Event {
return nuxtApp.ssrContext?.event as H3Event
// check if H3 event is available
if (nuxtApp.ssrContext?.event) { return nuxtApp.ssrContext.event }
// Check if we created H3 event manually before
if (nuxtApp.ssrContext?._event) { return nuxtApp.ssrContext._event }
// Create H3 event https://github.com/nuxt/bridge/pull/999#discussion_r1413049422
nuxtApp.ssrContext._event = createEvent(nuxtApp.ssrContext?.req, nuxtApp.ssrContext?.res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind you this is a very fragile low-level method and behavior can be broken in future releases but considering that it is acceptable for edge-case usage in bridge, looks good workaround...

return nuxtApp.ssrContext._event
}