diff --git a/platform/src/components/aws/router.ts b/platform/src/components/aws/router.ts index fcf4dd89a..e911d525c 100644 --- a/platform/src/components/aws/router.ts +++ b/platform/src/components/aws/router.ts @@ -163,7 +163,7 @@ interface BaseRouteArgs { * header. The given code will be injected at the end of this function. * * ```js - * function handler(event) { + * async function handler(event) { * // Default behavior code * * // User injected code @@ -244,7 +244,7 @@ interface BaseRouteArgs { * the provided code. * * ```js - * function handler(event) { + * async function handler(event) { * // User injected code * * return event.response; @@ -611,7 +611,7 @@ export class Router extends Component implements Link.Linkable { { runtime: "cloudfront-js-2.0", code: [ - `function handler(event) {`, + `async function handler(event) {`, ` event.request.headers["x-forwarded-host"] = event.request.headers.host;`, ` return event.request;`, `}`, @@ -626,15 +626,15 @@ export class Router extends Component implements Link.Linkable { path: string, config: | { - injection: string; - kvStores?: string[]; - } + injection: string; + kvStores?: string[]; + } | undefined, rewrite: | { - regex: string; - to: string; - } + regex: string; + to: string; + } | undefined, injectHostHeader: boolean, ) { @@ -644,17 +644,19 @@ export class Router extends Component implements Link.Linkable { runtime: "cloudfront-js-2.0", keyValueStoreAssociations: config?.kvStores ?? [], code: ` -function handler(event) { - ${injectHostHeader - ? `event.request.headers["x-forwarded-host"] = event.request.headers.host;` - : "" - } - ${rewrite - ? ` +async function handler(event) { + ${ + injectHostHeader + ? `event.request.headers["x-forwarded-host"] = event.request.headers.host;` + : "" + } + ${ + rewrite + ? ` const re = new RegExp("${rewrite.regex}"); event.request.uri = event.request.uri.replace(re, "${rewrite.to}");` - : "" - } + : "" + } ${config?.injection ?? ""} return event.request; }`, @@ -676,7 +678,7 @@ event.request.uri = event.request.uri.replace(re, "${rewrite.to}");` runtime: "cloudfront-js-2.0", keyValueStoreAssociations: config!.kvStores ?? [], code: ` -function handler(event) { +async function handler(event) { ${config.injection ?? ""} return event.response; }`, @@ -833,30 +835,30 @@ function handler(event) { functionAssociations: [ ...("url" in route || route.edge?.viewerRequest || route.rewrite ? [ - { - eventType: "viewer-request", - functionArn: - route.edge?.viewerRequest || route.rewrite - ? createCfRequestFunction( - path, - route.edge?.viewerRequest, - route.rewrite, - "url" in route, - ).arn - : createCfRequestDefaultFunction().arn, - }, - ] + { + eventType: "viewer-request", + functionArn: + route.edge?.viewerRequest || route.rewrite + ? createCfRequestFunction( + path, + route.edge?.viewerRequest, + route.rewrite, + "url" in route, + ).arn + : createCfRequestDefaultFunction().arn, + }, + ] : []), ...(route.edge?.viewerResponse ? [ - { - eventType: "viewer-response", - functionArn: createCfResponseFunction( - path, - route.edge.viewerResponse, - ).arn, - }, - ] + { + eventType: "viewer-response", + functionArn: createCfResponseFunction( + path, + route.edge.viewerResponse, + ).arn, + }, + ] : []), ], ...("url" in route ? urlDefaultConfig : bucketDefaultConfig), diff --git a/platform/src/components/aws/ssr-site.ts b/platform/src/components/aws/ssr-site.ts index 8e3cc02a8..efe17fd9f 100644 --- a/platform/src/components/aws/ssr-site.ts +++ b/platform/src/components/aws/ssr-site.ts @@ -210,7 +210,7 @@ export interface SsrSiteArgs extends BaseSsrSiteArgs { * header. The given code will be injected at the end of this function. * * ```js - * function handler(event) { + * async function handler(event) { * // Default behavior code * * // User injected code @@ -272,7 +272,7 @@ export interface SsrSiteArgs extends BaseSsrSiteArgs { * the provided code. * * ```js - * function handler(event) { + * async function handler(event) { * // User injected code * * return event.response; @@ -915,7 +915,7 @@ export function createServersAndDistribution( runtime: "cloudfront-js-2.0", keyValueStoreAssociations: config.apply((v) => v?.kvStores ?? []), code: interpolate` -function handler(event) { +async function handler(event) { ${injections.join("\n")} ${config.apply((v) => v?.injection ?? "")} return event.request; diff --git a/platform/src/components/aws/static-site.ts b/platform/src/components/aws/static-site.ts index 09a94f74c..45e09635c 100644 --- a/platform/src/components/aws/static-site.ts +++ b/platform/src/components/aws/static-site.ts @@ -87,7 +87,7 @@ export interface StaticSiteArgs extends BaseStaticSiteArgs { * The given code will be injected at the end of the function. * * ```js - * function handler(event) { + * async function handler(event) { * // Default behavior code * * // User injected code @@ -147,7 +147,7 @@ export interface StaticSiteArgs extends BaseStaticSiteArgs { * the provided code. * * ```js - * function handler(event) { + * async function handler(event) { * // User injected code * * return event.response; @@ -876,7 +876,7 @@ export class StaticSite extends Component implements Link.Linkable { ), code: output(args.edge).apply( (edge) => ` -function handler(event) { +async function handler(event) { if (event.request.uri.endsWith('/')) { event.request.uri += 'index.html'; } else if (!event.request.uri.includes('.')) { @@ -898,7 +898,7 @@ function handler(event) { ), code: output(args.edge).apply( (edge) => ` -function handler(event) { +async function handler(event) { ${edge?.viewerResponse?.injection ?? ""} return event.response; } diff --git a/www/sst.config.ts b/www/sst.config.ts index 41b85c380..701074370 100644 --- a/www/sst.config.ts +++ b/www/sst.config.ts @@ -41,7 +41,7 @@ export default $config({ const cfFunction = new aws.cloudfront.Function("AstroRedirect", { runtime: "cloudfront-js-2.0", code: [ - `function handler(event) {`, + `async function handler(event) {`, ` const request = event.request;`, // ie. request.uri is /examples/foo ` return {`,