@@ -4,9 +4,9 @@ import { includeBytes } from "fastly:experimental";
44import { ConfigStore } from "fastly:config-store" ;
55
66// Load static files as a Uint8Array at compile time.
7- // File path is relative to root of project, not to this file
8- const notfoundPage = includeBytes ( "./ src/not-found.html" ) ;
9- const robotsPage = includeBytes ( "./ src/robots.txt" ) ;
7+ // File path is relative to the root of the project, not this file
8+ const notfoundPage = includeBytes ( "src/not-found.html" ) || new Uint8Array ( ) ;
9+ const robotsPage = includeBytes ( "src/robots.txt" ) || new Uint8Array ( ) ;
1010
1111const handler = async ( event ) => {
1212 // get the request from the client
@@ -16,46 +16,58 @@ const handler = async (event) => {
1616
1717 // Prepare logs
1818 const logger = new Logger ( "tacolog" ) ;
19- logger . log ( ' Request: ' + reqURL ) ;
20- logger . log ( ' User-Agent: ' + req . headers . get ( ' User-Agent' ) ) ;
19+ logger . log ( ` Request: ${ reqURL } ` ) ;
20+ logger . log ( ` User-Agent: ${ req . headers . get ( " User-Agent" ) } ` ) ;
2121
2222 // Check if there is a redirect for the URL requested.
2323 // If there is, redirect the client.
24- const config = new ConfigStore ( ' redirects' ) ;
24+ const config = new ConfigStore ( " redirects" ) ;
2525 const dest = config . get ( reqPath ) ;
2626
2727 if ( dest ) {
2828 return new Response ( "" , {
2929 status : 301 ,
30- headers : { Location : dest } ,
30+ headers : { Location : String ( dest ) } ,
3131 } ) ;
3232 }
3333
34- const backendResponse = await fetch ( req , {
35- backend : "vcl-origin" ,
36- cacheOverride : new CacheOverride ( "pass" )
37- } ) ;
34+ // Fetch from backend
35+ let backendResponse ;
36+ try {
37+ backendResponse = await fetch ( req , {
38+ backend : "vcl-origin" ,
39+ cacheOverride : new CacheOverride ( "pass" ) ,
40+ } ) ;
41+ } catch ( error ) {
42+ logger . log ( `Backend fetch error: ${ error } ` ) ;
43+ return new Response ( "Internal Server Error" , { status : 500 } ) ;
44+ }
3845
3946 // Handle 404s with a custom response
40- if ( backendResponse . status == 404 ) {
41- return new Response ( notfoundPage , {
47+ if ( backendResponse . status === 404 ) {
48+ return new Response ( new Uint8Array ( notfoundPage ) , {
4249 status : 404 ,
50+ headers : { "Content-Type" : "text/html" } ,
4351 } ) ;
4452 }
4553
4654 // Return robots.txt with a custom response
4755 if ( reqURL . pathname . endsWith ( "/robots.txt" ) ) {
48- return new Response ( robotsPage , {
56+ return new Response ( new Uint8Array ( robotsPage ) , {
4957 status : 200 ,
58+ headers : { "Content-Type" : "text/plain" } ,
5059 } ) ;
5160 }
5261
53- // If status is not 404, send the backend response to the client
54- if ( backendResponse . status != 404 ) {
55- // Add headers to the response back to the client
56- backendResponse . headers . append ( "x-tacos" , "🌮🌮🌮" )
57- return backendResponse ;
58- }
59- }
62+ // Clone backend response and modify headers before returning
63+ const modifiedResponse = new Response ( backendResponse . body , {
64+ status : backendResponse . status ,
65+ statusText : backendResponse . statusText ,
66+ headers : backendResponse . headers ,
67+ } ) ;
68+
69+ modifiedResponse . headers . append ( "x-tacos" , "🌮🌮🌮" ) ;
70+ return modifiedResponse ;
71+ } ;
6072
61- addEventListener ( "fetch" , event => event . respondWith ( handler ( event ) ) ) ;
73+ addEventListener ( "fetch" , ( event ) => event . respondWith ( handler ( event ) ) ) ;
0 commit comments