Open FlatMapIO opened 4 months ago
Hmm weird, i think some of our docs sites are deployed on CF worker and we don't see this issue right @ryansolid
Came here to report this, to shed more light:
The error occurs in CF environment if the event.nativeEvent
is not passed to vinxi/http functions
export default createMiddleware({
onRequest: [
(event) => {
const headers = getRequestHeaders()
console.log(headers)
}
]
})
It works if passed in:
export default createMiddleware({
onRequest: [
(event) => {
const headers = getRequestHeaders(event.nativeEvent)
console.log(headers)
}
]
})
The same issue occurs with <HttpStatusCode />
: if you go to a 404 page that renders this you will get a 500 error of Context is not available
Not passing event.nativeEvent
will work in dev and also a local build, but not when ran in CF (or wrangler)
Works for me @sabercoy, thanks!
Simply appending event as the first parameter to each "vinxi/http" function seems to work. Here are some examples:
export default eventHandler(async (event) => {
const url = getRequestURL(event);
const storedState = getCookie(event, "state");
return sendRedirect(event, url.toString());
})
I don't have access to event.nativeEvent though.
I only use getRequestEvent now, and I have not configured any options related to als. The cf worker works fine
defineConfig({
middleware: './src/middleware.ts',
// @ts-ignore
vite(option) {
return {
plugins: [tsconfigPaths()],
}
},
})
middleware.ts
export default createMiddleware({
onRequest: [
async (event) => {
if (import.meta.env.DEV) {
// @ts-ignore
if (!globalThis.__cf) {
console.log('# attach cloudflare development environment')
const cf = await import('wrangler')
const proxy = await cf.getPlatformProxy<Env>({ persist: true })
// @ts-ignore
globalThis.__cf = proxy
}
if (!event.locals.cloudflare) {
event.locals.cloudflare = {
env: {
// ...process.env
// @ts-ignore
...globalThis.__cf.env,
},
} as any
}
} else {
event.locals.cloudflare = event.nativeEvent.context.cloudflare
}
},
],
onBeforeResponse: [(event, { body }) => {}],
})
server-context.ts
export type Env = {
GOOGLE_CLIENT_ID: string
GOOGLE_CLIENT_SECRET: string
GOOGLE_REDIRECT_URL: string
// =================================================
KV: KVNamespace
DB: D1Database
AI: any
}
declare module '@solidjs/start/server' {
interface RequestEventLocals {
env: Env
cloudflare: {
request: Request
env: Env
context: ExecutionContext
}
}
}
export function getServerContext() {
if (!isServer) {
throw new Error('You must call this function only in server environment')
}
const event = getRequestEvent()!
const cf = event.locals.cloudflare
return {
env: cf.env,
cloudflare: cf,
event,
}
}
In solid-start 0.6.0, getEvent() works fine in the development environment to get the request event context, but when deployed to a cloudflare worker, the following occurs.
app.config.ts