Skip to content

Commit

Permalink
chore: use middleware instead of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Oct 2, 2024
1 parent 6c73e6d commit f921892
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.nitro.commands.preview = 'npx nuxthub preview'
nuxt.options.nitro.commands.deploy = 'npx nuxthub deploy'

// Add the env plugin
addServerPlugin(resolve('./runtime/env'))
// Add the env middleware
nuxt.options.nitro.handlers ||= []
nuxt.options.nitro.handlers.unshift({
middleware: true,
handler: resolve('./runtime/env')
})
}

// Local development without remote connection
Expand Down
17 changes: 7 additions & 10 deletions src/runtime/env.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { NitroAppPlugin } from 'nitropack'
import type { H3Event } from 'h3'
import { eventHandler } from 'h3'

// Hack around Cloudflare regression since nodejs_compat_v2 for process.env
export default <NitroAppPlugin> function (nitroApp) {
nitroApp.hooks.hook('request', async (event: H3Event) => {
const env = event.context.cloudflare?.env || {}
for (const key in env) {
process.env[key] = env[key]
}
})
}
export default eventHandler((event) => {
const env = event.context.cloudflare?.env || {}
for (const key in env) {
process.env[key] = env[key]
}
})

0 comments on commit f921892

Please sign in to comment.