-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.js
48 lines (41 loc) · 1.65 KB
/
headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @remix-run/server-runtime v1.5.1
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var setCookieParser = require('set-cookie-parser');
function getDocumentHeaders(build, matches, routeLoaderResponses, actionResponse) {
return matches.reduce((parentHeaders, match, index) => {
let routeModule = build.routes[match.route.id].module;
let routeLoaderResponse = routeLoaderResponses[match.route.id];
let loaderHeaders = routeLoaderResponse ? routeLoaderResponse.headers : new Headers();
let actionHeaders = actionResponse ? actionResponse.headers : new Headers();
let headers = new Headers(routeModule.headers ? typeof routeModule.headers === "function" ? routeModule.headers({
loaderHeaders,
parentHeaders,
actionHeaders
}) : routeModule.headers : undefined); // Automatically preserve Set-Cookie headers that were set either by the
// loader or by a parent route.
prependCookies(actionHeaders, headers);
prependCookies(loaderHeaders, headers);
prependCookies(parentHeaders, headers);
return headers;
}, new Headers());
}
function prependCookies(parentHeaders, childHeaders) {
let parentSetCookieString = parentHeaders.get("Set-Cookie");
if (parentSetCookieString) {
let cookies = setCookieParser.splitCookiesString(parentSetCookieString);
cookies.forEach(cookie => {
childHeaders.append("Set-Cookie", cookie);
});
}
}
exports.getDocumentHeaders = getDocumentHeaders;