@@ -6,13 +6,16 @@ import type { OutputAsset, OutputChunk } from 'rollup';
66import { fileURLToPath } from 'url' ;
77import type {
88 AstroConfig ,
9+ AstroMiddlewareInstance ,
910 AstroSettings ,
1011 ComponentInstance ,
1112 EndpointHandler ,
1213 ImageTransform ,
14+ MiddlewareHandler ,
1315 RouteType ,
1416 SSRError ,
1517 SSRLoadedRenderer ,
18+ EndpointOutput ,
1619} from '../../@types/astro' ;
1720import {
1821 generateImage as generateImageInternal ,
@@ -27,10 +30,20 @@ import {
2730} from '../../core/path.js' ;
2831import { runHookBuildGenerated } from '../../integrations/index.js' ;
2932import { BEFORE_HYDRATION_SCRIPT_ID , PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js' ;
30- import { call as callEndpoint , throwIfRedirectNotAllowed } from '../endpoint/index.js' ;
31- import { AstroError } from '../errors/index.js' ;
33+ import {
34+ call as callEndpoint ,
35+ createAPIContext ,
36+ throwIfRedirectNotAllowed ,
37+ } from '../endpoint/index.js' ;
38+ import { AstroError , AstroErrorData } from '../errors/index.js' ;
3239import { debug , info } from '../logger/core.js' ;
33- import { createEnvironment , createRenderContext , renderPage } from '../render/index.js' ;
40+ import {
41+ createEnvironment ,
42+ createRenderContext ,
43+ getParamsAndProps ,
44+ GetParamsAndPropsError ,
45+ renderPage ,
46+ } from '../render/index.js' ;
3447import { callGetStaticPaths } from '../render/route-cache.js' ;
3548import { createLinkStylesheetElementSet , createModuleScriptsSet } from '../render/ssr-element.js' ;
3649import { createRequest } from '../request.js' ;
@@ -45,6 +58,7 @@ import {
4558} from './internal.js' ;
4659import type { PageBuildData , SingleFileBuiltModule , StaticBuildOptions } from './types' ;
4760import { getTimeStat } from './util.js' ;
61+ import { callMiddleware } from '../middleware/index.js' ;
4862
4963function shouldSkipDraft ( pageModule : ComponentInstance , settings : AstroSettings ) : boolean {
5064 return (
@@ -168,6 +182,7 @@ async function generatePage(
168182 const scripts = pageInfo ?. hoistedScript ?? null ;
169183
170184 const pageModule = ssrEntry . pageMap ?. get ( pageData . component ) ;
185+ const middleware = ssrEntry . middleware ;
171186
172187 if ( ! pageModule ) {
173188 throw new Error (
@@ -197,7 +212,7 @@ async function generatePage(
197212
198213 for ( let i = 0 ; i < paths . length ; i ++ ) {
199214 const path = paths [ i ] ;
200- await generatePath ( path , opts , generationOptions ) ;
215+ await generatePath ( path , opts , generationOptions , middleware ) ;
201216 const timeEnd = performance . now ( ) ;
202217 const timeChange = getTimeStat ( timeStart , timeEnd ) ;
203218 const timeIncrease = `(+${ timeChange } )` ;
@@ -339,7 +354,8 @@ function getUrlForPath(
339354async function generatePath (
340355 pathname : string ,
341356 opts : StaticBuildOptions ,
342- gopts : GeneratePathOptions
357+ gopts : GeneratePathOptions ,
358+ middleware : AstroMiddlewareInstance < unknown >
343359) {
344360 const { settings, logging, origin, routeCache } = opts ;
345361 const { mod, internals, linkIds, scripts : hoistedScripts , pageData, renderers } = gopts ;
@@ -412,6 +428,7 @@ async function generatePath(
412428 ssr,
413429 streaming : true ,
414430 } ) ;
431+
415432 const ctx = createRenderContext ( {
416433 origin,
417434 pathname,
@@ -426,7 +443,14 @@ async function generatePath(
426443 let encoding : BufferEncoding | undefined ;
427444 if ( pageData . route . type === 'endpoint' ) {
428445 const endpointHandler = mod as unknown as EndpointHandler ;
429- const result = await callEndpoint ( endpointHandler , env , ctx , logging ) ;
446+
447+ const result = await callEndpoint (
448+ endpointHandler ,
449+ env ,
450+ ctx ,
451+ logging ,
452+ middleware as AstroMiddlewareInstance < Response | EndpointOutput >
453+ ) ;
430454
431455 if ( result . type === 'response' ) {
432456 throwIfRedirectNotAllowed ( result . response , opts . settings . config ) ;
@@ -441,7 +465,39 @@ async function generatePath(
441465 } else {
442466 let response : Response ;
443467 try {
444- response = await renderPage ( mod , ctx , env ) ;
468+ const paramsAndPropsResp = await getParamsAndProps ( {
469+ mod : mod as any ,
470+ route : ctx . route ,
471+ routeCache : env . routeCache ,
472+ pathname : ctx . pathname ,
473+ logging : env . logging ,
474+ ssr : env . ssr ,
475+ } ) ;
476+
477+ if ( paramsAndPropsResp === GetParamsAndPropsError . NoMatchingStaticPath ) {
478+ throw new AstroError ( {
479+ ...AstroErrorData . NoMatchingStaticPathFound ,
480+ message : AstroErrorData . NoMatchingStaticPathFound . message ( ctx . pathname ) ,
481+ hint : ctx . route ?. component
482+ ? AstroErrorData . NoMatchingStaticPathFound . hint ( [ ctx . route ?. component ] )
483+ : '' ,
484+ } ) ;
485+ }
486+ const [ params , props ] = paramsAndPropsResp ;
487+
488+ const context = createAPIContext ( {
489+ request : ctx . request ,
490+ params,
491+ props,
492+ site : env . site ,
493+ adapterName : env . adapterName ,
494+ } ) ;
495+ // If the user doesn't configure a middleware, the rollup plugin emits a no-op function,
496+ // so it's safe to use `callMiddleware` regardless
497+ let onRequest = middleware . onRequest as MiddlewareHandler < Response > ;
498+ response = await callMiddleware < Response > ( onRequest , context , ( ) => {
499+ return renderPage ( mod , ctx , env , context ) ;
500+ } ) ;
445501 } catch ( err ) {
446502 if ( ! AstroError . is ( err ) && ! ( err as SSRError ) . id && typeof err === 'object' ) {
447503 ( err as SSRError ) . id = pageData . component ;
0 commit comments