11import { splitCookiesString } from "set-cookie-parser" ;
22
3- import type { ServerBuild } from "./build " ;
3+ import type { DataRouteMatch } from "../context " ;
44import type { StaticHandlerContext } from "../router/router" ;
5+ import type { ServerRouteModule } from "../dom/ssr/routeModules" ;
6+ import type { ServerBuild } from "./build" ;
57import invariant from "./invariant" ;
68
9+ // Version used by v7 framework mode
710export function getDocumentHeaders (
811 build : ServerBuild ,
912 context : StaticHandlerContext
13+ ) : Headers {
14+ return getDocumentHeadersImpl ( context , ( m ) => {
15+ let route = build . routes [ m . route . id ] ;
16+ invariant ( route , `Route with id "${ m . route . id } " not found in build` ) ;
17+ return route . module . headers ;
18+ } ) ;
19+ }
20+
21+ function getDocumentHeadersImpl (
22+ context : StaticHandlerContext ,
23+ getRouteHeadersFn : ( match : DataRouteMatch ) => ServerRouteModule [ "headers" ]
1024) : Headers {
1125 let boundaryIdx = context . errors
1226 ? context . matches . findIndex ( ( m ) => context . errors ! [ m . route . id ] )
@@ -38,9 +52,6 @@ export function getDocumentHeaders(
3852
3953 return matches . reduce ( ( parentHeaders , match , idx ) => {
4054 let { id } = match . route ;
41- let route = build . routes [ id ] ;
42- invariant ( route , `Route with id "${ id } " not found in build` ) ;
43- let routeModule = route . module ;
4455 let loaderHeaders = context . loaderHeaders [ id ] || new Headers ( ) ;
4556 let actionHeaders = context . actionHeaders [ id ] || new Headers ( ) ;
4657
@@ -56,8 +67,10 @@ export function getDocumentHeaders(
5667 errorHeaders !== loaderHeaders &&
5768 errorHeaders !== actionHeaders ;
5869
70+ let headersFn = getRouteHeadersFn ( match ) ;
71+
5972 // Use the parent headers for any route without a `headers` export
60- if ( routeModule . headers == null ) {
73+ if ( headersFn == null ) {
6174 let headers = new Headers ( parentHeaders ) ;
6275 if ( includeErrorCookies ) {
6376 prependCookies ( errorHeaders ! , headers ) ;
@@ -68,16 +81,14 @@ export function getDocumentHeaders(
6881 }
6982
7083 let headers = new Headers (
71- routeModule . headers
72- ? typeof routeModule . headers === "function"
73- ? routeModule . headers ( {
74- loaderHeaders,
75- parentHeaders,
76- actionHeaders,
77- errorHeaders : includeErrorHeaders ? errorHeaders : undefined ,
78- } )
79- : routeModule . headers
80- : undefined
84+ typeof headersFn === "function"
85+ ? headersFn ( {
86+ loaderHeaders,
87+ parentHeaders,
88+ actionHeaders,
89+ errorHeaders : includeErrorHeaders ? errorHeaders : undefined ,
90+ } )
91+ : headersFn
8192 ) ;
8293
8394 // Automatically preserve Set-Cookie headers from bubbled responses,
0 commit comments