Skip to content

Commit 515f68b

Browse files
committed
refactor: create middleware before adding to handlers
1 parent f02dbdc commit 515f68b

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/module.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fileURLToPath } from 'url'
22
import { addServerHandler, addTemplate, defineNuxtModule, useLogger } from '@nuxt/kit'
33
import type { Options } from 'http-proxy-middleware'
4-
import { join } from 'pathe'
4+
import { resolve } from 'pathe'
55
import { defu } from 'defu'
66
// @ts-expect-error: No types
77
import dedent from 'dedent'
@@ -21,19 +21,12 @@ export default defineNuxtModule<Options>({
2121
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
2222
nuxt.options.build.transpile.push(runtimeDir, '#build/proxy-handler')
2323

24-
const handlerPath = join(nuxt.options.buildDir, 'proxy-handler.ts')
25-
2624
// Final resolved configuration
2725
const finalConfig = nuxt.options.runtimeConfig.proxy = defu(nuxt.options.runtimeConfig.proxy, {
2826
logger: options.logger,
2927
changeOrigin: options.changeOrigin,
3028
})
3129

32-
addServerHandler({
33-
handler: handlerPath,
34-
middleware: true,
35-
})
36-
3730
addTemplate({
3831
src: 'proxy-handler.ts',
3932
write: true,
@@ -43,6 +36,11 @@ export default defineNuxtModule<Options>({
4336
export default createProxyMiddleware(${finalConfig})
4437
`,
4538
})
39+
40+
addServerHandler({
41+
handler: resolve(nuxt.options.buildDir, 'proxy-handler.ts'),
42+
middleware: true,
43+
})
4644
},
4745
})
4846

0 commit comments

Comments
 (0)