1
1
import { splitCookiesString } from "set-cookie-parser" ;
2
2
3
- import type { ServerBuild } from "./build " ;
3
+ import type { DataRouteMatch } from "../context " ;
4
4
import type { StaticHandlerContext } from "../router/router" ;
5
+ import type { ServerRouteModule } from "../dom/ssr/routeModules" ;
6
+ import type { ServerBuild } from "./build" ;
5
7
import invariant from "./invariant" ;
6
8
9
+ // Version used by v7 framework mode
7
10
export function getDocumentHeaders (
8
11
build : ServerBuild ,
9
12
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" ]
10
24
) : Headers {
11
25
let boundaryIdx = context . errors
12
26
? context . matches . findIndex ( ( m ) => context . errors ! [ m . route . id ] )
@@ -38,9 +52,6 @@ export function getDocumentHeaders(
38
52
39
53
return matches . reduce ( ( parentHeaders , match , idx ) => {
40
54
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 ;
44
55
let loaderHeaders = context . loaderHeaders [ id ] || new Headers ( ) ;
45
56
let actionHeaders = context . actionHeaders [ id ] || new Headers ( ) ;
46
57
@@ -56,8 +67,10 @@ export function getDocumentHeaders(
56
67
errorHeaders !== loaderHeaders &&
57
68
errorHeaders !== actionHeaders ;
58
69
70
+ let headersFn = getRouteHeadersFn ( match ) ;
71
+
59
72
// Use the parent headers for any route without a `headers` export
60
- if ( routeModule . headers == null ) {
73
+ if ( headersFn == null ) {
61
74
let headers = new Headers ( parentHeaders ) ;
62
75
if ( includeErrorCookies ) {
63
76
prependCookies ( errorHeaders ! , headers ) ;
@@ -68,16 +81,14 @@ export function getDocumentHeaders(
68
81
}
69
82
70
83
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
81
92
) ;
82
93
83
94
// Automatically preserve Set-Cookie headers from bubbled responses,
0 commit comments