@@ -40,8 +40,16 @@ declare global {
4040const APP_ID = import . meta. prerender ? "prerender" : "default" ;
4141
4242export function useNitroApp ( _id = APP_ID ) : NitroApp {
43+ let instance = globalThis . __nitro__ ?. [ _id ] ;
44+ if ( instance ) {
45+ return instance ;
46+ }
4347 globalThis . __nitro__ ??= { } ;
44- return ( globalThis . __nitro__ [ _id ] ??= initNitroApp ( ) ) ;
48+ instance = globalThis . __nitro__ [ _id ] = createNitroApp ( ) ;
49+ if ( hasPlugins ) {
50+ initNitroPlugins ( instance ) ;
51+ }
52+ return instance ;
4553}
4654
4755export function useNitroHooks ( _id = APP_ID ) : HookableCore < NitroRuntimeHooks > {
@@ -89,23 +97,6 @@ export function fetch(
8997 return fetch ( resource , init ) ;
9098}
9199
92- function initNitroApp ( ) : NitroApp {
93- const nitroApp = ( globalThis . __nitro__ = createNitroApp ( ) ) ;
94- if ( hasPlugins ) {
95- for ( const plugin of plugins ) {
96- try {
97- plugin (
98- nitroApp as NitroApp & { hooks : NonNullable < NitroApp [ "hooks" ] > }
99- ) ;
100- } catch ( error : any ) {
101- nitroApp . captureError ?.( error , { tags : [ "plugin" ] } ) ;
102- throw error ;
103- }
104- }
105- }
106- return nitroApp ;
107- }
108-
109100function createNitroApp ( ) : NitroApp {
110101 const hooks = hasHooks ? new HookableCore < NitroRuntimeHooks > ( ) : undefined ;
111102
@@ -171,6 +162,18 @@ function createNitroApp(): NitroApp {
171162 return app ;
172163}
173164
165+ function initNitroPlugins ( app : NitroApp ) {
166+ for ( const plugin of plugins ) {
167+ try {
168+ plugin ( app as NitroApp & { hooks : NonNullable < NitroApp [ "hooks" ] > } ) ;
169+ } catch ( error : any ) {
170+ app . captureError ?.( error , { tags : [ "plugin" ] } ) ;
171+ throw error ;
172+ }
173+ }
174+ return app ;
175+ }
176+
174177function createH3App ( config : H3Config ) {
175178 // Create H3 app
176179 const h3App = new H3Core ( config ) ;
0 commit comments