@@ -12,7 +12,9 @@ import { generateComponentManifest } from '../../src/pages/manifest.ts';
1212import { scanPages } from '../../src/pages/scan.ts' ;
1313import { deserializePageData } from '../../src/pages/serialize.ts' ;
1414import { defineHandler } from '../../src/runtime/handler.ts' ;
15- import { createPageGetHandler } from '../../src/runtime/routing-pages.ts' ;
15+ import { createErrorRenderer } from '../../src/runtime/routing-errors.ts' ;
16+ import { createPageGetHandler , createPagesFallback } from '../../src/runtime/routing-pages.ts' ;
17+ import { createRoutingRuntime } from '../../src/runtime/routing.ts' ;
1618
1719const roots : Array < string > = [ ] ;
1820function fixture ( files : Record < string , string > ) {
@@ -277,6 +279,104 @@ describe('Pages loader boundary protocol', () => {
277279 } ) ;
278280} ) ;
279281
282+ describe ( 'loader not-found dispatch' , ( ) => {
283+ it ( 'preserves a custom app not-found redirect' , async ( ) => {
284+ const app = new Hono ( ) ;
285+ app . notFound ( ( c ) => c . redirect ( '/login' ) ) ;
286+ app . get (
287+ '/' ,
288+ createPageGetHandler ( {
289+ componentId : 'index' ,
290+ load : async ( ) => ( { loader : ( c ) => c . notFound ( ) } ) ,
291+ render : async ( ) => '<main>Missing page</main>' ,
292+ assets : async ( ) => ( { css : '' , body : '' , preloads : '' } ) ,
293+ headConfig : null ,
294+ renderOptions : { boundaries : { notFound : 'missing' , errors : [ ] } } ,
295+ } ) ,
296+ ) ;
297+ const response = await app . request ( 'https://app.test/' ) ;
298+ expect ( response . status ) . toBe ( 302 ) ;
299+ expect ( response . headers . get ( 'Location' ) ) . toBe ( '/login' ) ;
300+ } ) ;
301+
302+ it . each ( [
303+ { pages : false , boundary : true } ,
304+ { pages : true , boundary : true } ,
305+ { pages : false , boundary : false } ,
306+ ] ) (
307+ 'renders a loader not-found once (Pages: $pages, boundary: $boundary)' ,
308+ async ( { pages, boundary } ) => {
309+ let rootCalls = 0 ;
310+ let loaderCalls = 0 ;
311+ const rendered : Array < string > = [ ] ;
312+ const app = new Hono ( ) ;
313+ app . notFound (
314+ createPagesFallback ( {
315+ routing : createRoutingRuntime ( app , { nodeTarget : true } ) ,
316+ errors : createErrorRenderer ( { projectRoot : '/project' , homeDir : '/home' } ) ,
317+ pageNotFound : createPageGetHandler ( {
318+ componentId : 'root' ,
319+ load : async ( ) => ( { loader : ( ) => new Response ( null , { status : 404 } ) } ) ,
320+ render : async ( ) => {
321+ rootCalls ++ ;
322+ return '<main>Root not found</main>' ;
323+ } ,
324+ assets : async ( ) => ( { css : '' , body : '' , preloads : '' } ) ,
325+ headConfig : null ,
326+ renderOptions : { boundaries : { notFound : 'root/not-found' , errors : [ ] } } ,
327+ } ) ,
328+ } ) ,
329+ ) ;
330+ app . get (
331+ '/items/:id' ,
332+ createPageGetHandler ( {
333+ componentId : 'items/item' ,
334+ load : async ( ) => ( {
335+ loader : defineHandler (
336+ async ( c , next ) => {
337+ const notFound = c . notFound ;
338+ await next ( ) ;
339+ expect ( c . notFound ) . toBe ( notFound ) ;
340+ } ,
341+ async ( c ) => {
342+ loaderCalls ++ ;
343+ await Promise . resolve ( ) ;
344+ return c . notFound ( ) ;
345+ } ,
346+ ) ,
347+ } ) ,
348+ render : async ( page ) => {
349+ rendered . push ( page . component ) ;
350+ return '<main>' + page . component + '</main>' ;
351+ } ,
352+ assets : async ( ) => ( { css : '' , body : '' , preloads : '' } ) ,
353+ headConfig : null ,
354+ renderOptions : boundary
355+ ? { boundaries : { notFound : 'items/not-found' , errors : [ ] } }
356+ : undefined ,
357+ } ) ,
358+ ) ;
359+ const response = await app . request ( 'https://app.test/items/1' , {
360+ headers : pages
361+ ? { 'X-VoidPages' : 'true' , 'X-VoidPages-Version' : PAGES_PROTOCOL_VERSION }
362+ : { } ,
363+ } ) ;
364+ expect ( response . status ) . toBe ( 404 ) ;
365+ expect ( rootCalls ) . toBe ( boundary ? 0 : 1 ) ;
366+ expect ( loaderCalls ) . toBe ( 1 ) ;
367+ expect ( rendered ) . toEqual ( pages || ! boundary ? [ ] : [ 'items/not-found' ] ) ;
368+ const body = await response . text ( ) ;
369+ if ( pages ) {
370+ expect ( deserializePageData ( body ) ) . toMatchObject ( { component : 'items/not-found' } ) ;
371+ } else {
372+ expect ( body ) . toBe (
373+ boundary ? '<main>items/not-found</main>' : '<main>Root not found</main>' ,
374+ ) ;
375+ }
376+ } ,
377+ ) ;
378+ } ) ;
379+
280380describe ( 'boundary recovery failures' , ( ) => {
281381 it . each ( [ 'malformed JSON' , 'body rejection' , 'exhausted fallback' ] ) (
282382 'handles %s without replaying the loader' ,
0 commit comments