1
1
import { STATUS_CODES } from 'http' ;
2
+
2
3
import {
3
4
CloudFrontHeaders ,
4
5
CloudFrontResultResponse ,
5
6
CloudFrontRequestEvent ,
6
7
CloudFrontRequest ,
7
8
} from 'aws-lambda' ;
8
9
10
+ import { appendQuerystring } from './util/append-querystring' ;
9
11
import { fetchProxyConfig } from './util/fetch-proxy-config' ;
10
12
import { generateCloudFrontHeaders } from './util/generate-cloudfront-headers' ;
11
- import { ProxyConfig , RouteResult } from './types' ;
12
- import { Proxy } from './proxy' ;
13
13
import {
14
14
createCustomOriginFromApiGateway ,
15
15
createCustomOriginFromUrl ,
16
16
serveRequestFromCustomOrigin ,
17
17
serveRequestFromS3Origin ,
18
18
} from './util/custom-origin' ;
19
+ import { Proxy } from './proxy' ;
20
+ import { ProxyConfig , RouteResult } from './types' ;
19
21
20
22
let proxyConfig : ProxyConfig ;
21
23
let proxy : Proxy ;
@@ -31,23 +33,38 @@ function isRedirect(
31
33
routeResult . status >= 300 &&
32
34
routeResult . status <= 309
33
35
) {
34
- if ( 'Location' in routeResult . headers ) {
35
- let headers : CloudFrontHeaders = { } ;
36
-
37
- // If the redirect is permanent, cache the result
38
- if ( routeResult . status === 301 || routeResult . status === 308 ) {
39
- headers [ 'cache-control' ] = [
36
+ const redirectTarget = routeResult . headers [ 'Location' ] ;
37
+ if ( redirectTarget ) {
38
+ // Append the original querystring to the redirect
39
+ const redirectTargetWithQuerystring = routeResult . uri_args
40
+ ? appendQuerystring ( redirectTarget , routeResult . uri_args )
41
+ : redirectTarget ;
42
+
43
+ // Override the Location header value with the appended querystring
44
+ routeResult . headers [ 'Location' ] = redirectTargetWithQuerystring ;
45
+
46
+ // Redirects are not cached, see discussion for details:
47
+ // https://github.com/milliHQ/terraform-aws-next-js/issues/296
48
+ const initialHeaders : CloudFrontHeaders = {
49
+ 'cache-control' : [
40
50
{
41
51
key : 'Cache-Control' ,
42
- value : 'public,max-age=31536000,immutable' ,
52
+ value : 'public, max-age=0, must-revalidate' ,
53
+ } ,
54
+ ] ,
55
+ 'content-type' : [
56
+ {
57
+ key : 'Content-Type' ,
58
+ value : 'text/plain' ,
43
59
} ,
44
- ] ;
45
- }
60
+ ] ,
61
+ } ;
46
62
47
63
return {
48
64
status : routeResult . status . toString ( ) ,
49
65
statusDescription : STATUS_CODES [ routeResult . status ] ,
50
- headers : generateCloudFrontHeaders ( headers , routeResult . headers ) ,
66
+ headers : generateCloudFrontHeaders ( initialHeaders , routeResult . headers ) ,
67
+ body : `Redirecting to ${ redirectTargetWithQuerystring } (${ routeResult . status } )` ,
51
68
} ;
52
69
}
53
70
}
@@ -77,7 +94,6 @@ async function main(
77
94
] [ 0 ] . value ;
78
95
const apiEndpoint = request . origin ! . s3 ! . customHeaders [ 'x-env-api-endpoint' ] [ 0 ]
79
96
. value ;
80
- let headers : Record < string , string > = { } ;
81
97
82
98
try {
83
99
if ( ! proxyConfig ) {
@@ -107,9 +123,10 @@ async function main(
107
123
108
124
// Append query string if we have one
109
125
// @see : https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html
110
- const requestPath = `${ request . uri } ${
111
- request . querystring !== '' ? `?${ request . querystring } ` : ''
112
- } `;
126
+ const requestPath =
127
+ request . querystring !== ''
128
+ ? `${ request . uri } ?${ request . querystring } `
129
+ : request . uri ;
113
130
const proxyResult = proxy . route ( requestPath ) ;
114
131
115
132
// Check for redirect
0 commit comments