@@ -278,9 +278,18 @@ async function createForwardedActionResponse(
278278 }
279279
280280 return new FlightRenderResult ( response . body ! )
281- } else {
282- // Since we aren't consuming the response body, we cancel it to avoid memory leaks
283- response . body ?. cancel ( )
281+ }
282+
283+ // Since we aren't consuming the response body, we cancel it to avoid memory leaks
284+ response . body ?. cancel ( )
285+
286+ // Pass the action-not-found marker through so the client throws
287+ // UnrecognizedActionError instead of a generic "unexpected response".
288+ if ( response . headers . get ( NEXT_ACTION_NOT_FOUND_HEADER ) === '1' ) {
289+ res . setHeader ( NEXT_ACTION_NOT_FOUND_HEADER , '1' )
290+ res . setHeader ( 'content-type' , 'text/plain' )
291+ res . statusCode = 404
292+ return RenderResult . fromStatic ( 'Server action not found.' , 'text/plain' )
284293 }
285294 } catch ( err ) {
286295 // we couldn't stream the forwarded response, so we'll just return an empty response
@@ -707,11 +716,15 @@ export async function handleAction({
707716
708717 const actionWasForwarded = Boolean ( req . headers [ 'x-action-forwarded' ] )
709718
710- if ( actionId ) {
719+ // Only attempt to forward if this request has not already been forwarded.
720+ // Otherwise middleware that rewrites the action POST can cause the receiving
721+ // worker to forward again, looping indefinitely.
722+ if ( actionId && ! actionWasForwarded ) {
711723 const forwardedWorker = selectWorkerForForwarding ( actionId , page )
712724
713- // If forwardedWorker is truthy, it means there isn't a worker for the action
714- // in the current handler, so we forward the request to a worker that has the action.
725+ // If forwardedWorker is truthy, it means there isn't a worker for the
726+ // action in the current handler, so we forward the request to a worker that
727+ // has the action.
715728 if ( forwardedWorker ) {
716729 return {
717730 type : 'done' ,
0 commit comments