@@ -17,7 +17,7 @@ import process from 'node:process'
1717import { Server as TlsServer } from 'node:tls'
1818import { validateOriginCandidate } from 'devframe/utils/origin'
1919import { joinURL , withLeadingSlash , withoutLeadingSlash , withoutTrailingSlash } from 'devframe/utils/url'
20- import { defineHandler , H3 as H3App , toNodeHandler } from 'h3'
20+ import { defineHandler , fromNodeHandler , H3 as H3App , toNodeHandler } from 'h3'
2121import { DEVFRAME_SSE_ROUTE , DEVFRAME_WS_ROUTE } from '../constants'
2222import { createInteractiveAuth } from '../recipes/interactive-auth'
2323import { diagnostics } from './diagnostics'
@@ -663,6 +663,35 @@ export function createInstanceShell<TContext extends DevframeNodeContext>(
663663 maybeRegister ( )
664664 }
665665
666+ /**
667+ * The host stack's `next` for each in-flight `nodeMiddleware` request at
668+ * base `/`, read by the fall-through middleware mounted at the end of
669+ * `init()`.
670+ */
671+ const hostFallthrough = new WeakMap < object , ( ) => void > ( )
672+
673+ /**
674+ * A devframe at `/` shares the origin root with its host, so it can't claim
675+ * every path the way it owns a `/__<id>/` base. A `nodeMiddleware` request
676+ * that no route answers goes to the host's `next` instead of h3's 404, so
677+ * the host app (e.g. Vite with the bridge) keeps serving its own pages.
678+ */
679+ function mountHostFallthrough ( ) : void {
680+ if ( base !== '/' )
681+ return
682+ app . use ( ( event , next ) => {
683+ if ( event . context . matchedRoute )
684+ return next ( )
685+ const nodeReq = event . runtime ?. node ?. req
686+ const hostNext = nodeReq && hostFallthrough . get ( nodeReq )
687+ if ( ! hostNext )
688+ return next ( )
689+ hostFallthrough . delete ( nodeReq )
690+ // Resolves once the host stack has sent its own response.
691+ return fromNodeHandler ( ( _req , _res , _next ) => hostNext ( ) ) ( event )
692+ } )
693+ }
694+
666695 let started : StartedServer | undefined
667696 let transport : WsRpcTransport | undefined
668697 let dispose : ( ( ) => Promise < void > ) | undefined
@@ -894,6 +923,9 @@ export function createInstanceShell<TContext extends DevframeNodeContext>(
894923
895924 await options . mount ?.( ctx , meta , api )
896925
926+ // Last in the chain, after every route the instance mounts.
927+ mountHostFallthrough ( )
928+
897929 // A pinned origin means the registry record needn't wait for a first
898930 // request.
899931 maybeRegister ( )
@@ -970,6 +1002,8 @@ export function createInstanceShell<TContext extends DevframeNodeContext>(
9701002 const { toNodeHandler } = await import ( 'h3/node' )
9711003 nodeHandler = toNodeHandler ( app )
9721004 }
1005+ if ( next && base === '/' )
1006+ hostFallthrough . set ( req , ( ) => next ( ) )
9731007 return nodeHandler ( req , res )
9741008 } )
9751009 . catch ( ( err : unknown ) => {
0 commit comments