@@ -1392,38 +1392,43 @@ async function handler(event) {
13921392 ] ) . apply ( ( [ servers , imageOptimizerUrl ] ) => {
13931393 const kvEntries : Record < string , string > = { } ;
13941394 const dirs : string [ ] = [ ] ;
1395+ // Router append .html and index.html suffixes to requests to s3 routes:
1396+ // - `.well-known` contain files without suffix, hence will be appended .html
1397+ // - in the future, it might make sense for each dir to have props that controls
1398+ // the suffixes ie. "handleTrailingSlashse"
1399+ const expandDirs = [ ".well-known" ] ;
13951400
13961401 plan . assets . forEach ( ( copy ) => {
1397- fs . readdirSync ( path . join ( outputPath , copy . from ) , {
1398- withFileTypes : true ,
1399- } ) . forEach ( ( item ) => {
1400- if ( item . isFile ( ) ) {
1401- kvEntries [ toPosix ( path . join ( "/" , item . name ) ) ] = "s3" ;
1402- return ;
1403- }
1404-
1405- // Handle deep routes
1406- // In Next.js, asset requests are prefixed with is /_next/static, and
1407- // image optimization requests are prefixed with /_next/image. We cannot
1408- // route by 1 level of subdirs (ie. /_next/`), so we need to route by 2
1409- // levels of subdirs .
1410- if ( item . name !== copy . deepRoute ) {
1411- dirs . push ( toPosix ( path . join ( "/" , item . name ) ) ) ;
1412- return ;
1413- }
1414-
1415- fs . readdirSync ( path . join ( outputPath , copy . from , item . name ) , {
1416- withFileTypes : true ,
1417- } ) . forEach ( ( subItem ) => {
1418- if ( subItem . isFile ( ) ) {
1419- kvEntries [
1420- toPosix ( path . join ( "/" , item . name , subItem . name ) )
1421- ] = "s3" ;
1422- return ;
1423- }
1424- dirs . push ( toPosix ( path . join ( "/" , item . name , subItem . name ) ) ) ;
1425- } ) ;
1426- } ) ;
1402+ const processDir = ( childPath = "" , level = 0 ) => {
1403+ const currentPath = path . join ( outputPath , copy . from , childPath ) ;
1404+ fs . readdirSync ( currentPath , { withFileTypes : true } ) . forEach (
1405+ ( item ) => {
1406+ // File: add to kvEntries
1407+ if ( item . isFile ( ) ) {
1408+ kvEntries [ toPosix ( path . join ( "/" , childPath , item . name ) ) ] =
1409+ "s3" ;
1410+ return ;
1411+ }
1412+ // Directory + deep routes: recursively process it
1413+ // In Next.js, asset requests are prefixed with is /_next/static,
1414+ // and image optimization requests are prefixed with /_next/image .
1415+ // We cannot route by 1 level of subdirs (ie. /_next/`), so we need
1416+ // to route by 2 levels of subdirs.
1417+ // Directory + expand: recursively process it
1418+ if (
1419+ level === 0 &&
1420+ ( expandDirs . includes ( item . name ) ||
1421+ item . name === copy . deepRoute )
1422+ ) {
1423+ processDir ( path . join ( childPath , item . name ) , level + 1 ) ;
1424+ return ;
1425+ }
1426+ // Directory + NOT expand: add to route
1427+ dirs . push ( toPosix ( path . join ( "/" , childPath , item . name ) ) ) ;
1428+ } ,
1429+ ) ;
1430+ } ;
1431+ processDir ( ) ;
14271432 } ) ;
14281433
14291434 kvEntries [ "metadata" ] = JSON . stringify ( {
0 commit comments