@@ -35,6 +35,8 @@ import {
35
35
JSON_CONTENT_TYPE_HEADER ,
36
36
NEXT_RESUME_HEADER ,
37
37
} from '../../lib/constants'
38
+ import { normalizeLocalePath } from '../../shared/lib/i18n/normalize-locale-path'
39
+ import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
38
40
39
41
interface SharedRouteFields {
40
42
/**
@@ -62,6 +64,12 @@ interface SharedRouteFields {
62
64
*/
63
65
assets : Record < string , string >
64
66
67
+ /**
68
+ * wasmAssets are bundled wasm files with mapping of name
69
+ * to filePath on disk
70
+ */
71
+ wasmAssets ?: Record < string , string >
72
+
65
73
/**
66
74
* config related to the route
67
75
*/
@@ -77,6 +85,12 @@ interface SharedRouteFields {
77
85
* region preferences to the provider being used
78
86
*/
79
87
preferredRegion ?: string | string [ ]
88
+
89
+ /**
90
+ * env is the environment variables to expose, this is only
91
+ * populated for edge runtime currently
92
+ */
93
+ env ?: Record < string , string >
80
94
}
81
95
}
82
96
@@ -297,9 +311,30 @@ export interface NextAdapter {
297
311
} ) => Promise < void > | void
298
312
}
299
313
314
+ function normalizePathnames (
315
+ config : NextConfigComplete ,
316
+ outputs : AdapterOutputs
317
+ ) {
318
+ // normalize pathname field with basePath
319
+ if ( config . basePath ) {
320
+ for ( const output of [
321
+ ...outputs . pages ,
322
+ ...outputs . pagesApi ,
323
+ ...outputs . appPages ,
324
+ ...outputs . appRoutes ,
325
+ ...outputs . prerenders ,
326
+ ...outputs . staticFiles ,
327
+ ...( outputs . middleware ? [ outputs . middleware ] : [ ] ) ,
328
+ ] ) {
329
+ output . pathname = addPathPrefix ( output . pathname , config . basePath )
330
+ }
331
+ }
332
+ }
333
+
300
334
export async function handleBuildComplete ( {
301
335
dir,
302
336
config,
337
+ configOutDir,
303
338
distDir,
304
339
pageKeys,
305
340
tracingRoot,
@@ -318,6 +353,7 @@ export async function handleBuildComplete({
318
353
} : {
319
354
dir : string
320
355
distDir : string
356
+ configOutDir : string
321
357
adapterPath : string
322
358
tracingRoot : string
323
359
nextVersion : string
@@ -341,16 +377,34 @@ export async function handleBuildComplete({
341
377
if ( typeof adapterMod . onBuildComplete === 'function' ) {
342
378
Log . info ( `Running onBuildComplete from ${ adapterMod . name } ` )
343
379
344
- try {
345
- const outputs : AdapterOutputs = {
346
- pages : [ ] ,
347
- pagesApi : [ ] ,
348
- appPages : [ ] ,
349
- appRoutes : [ ] ,
350
- prerenders : [ ] ,
351
- staticFiles : [ ] ,
352
- }
380
+ const outputs : AdapterOutputs = {
381
+ pages : [ ] ,
382
+ pagesApi : [ ] ,
383
+ appPages : [ ] ,
384
+ appRoutes : [ ] ,
385
+ prerenders : [ ] ,
386
+ staticFiles : [ ] ,
387
+ }
388
+
389
+ if ( config . output === 'export' ) {
390
+ // collect export assets and provide as static files
391
+ const exportFiles = await recursiveReadDir ( configOutDir )
392
+
393
+ for ( const file of exportFiles ) {
394
+ let pathname = (
395
+ file . endsWith ( '.html' ) ? file . replace ( / \. h t m l $ / , '' ) : file
396
+ ) . replace ( / \\ / g, '/' )
353
397
398
+ pathname = pathname . startsWith ( '/' ) ? pathname : `/${ pathname } `
399
+
400
+ outputs . staticFiles . push ( {
401
+ id : file ,
402
+ pathname,
403
+ filePath : path . join ( configOutDir , file ) ,
404
+ type : AdapterOutputType . STATIC_FILE ,
405
+ } satisfies AdapterOutput [ 'STATIC_FILE' ] )
406
+ }
407
+ } else {
354
408
const staticFiles = await recursiveReadDir ( path . join ( distDir , 'static' ) )
355
409
356
410
for ( const file of staticFiles ) {
@@ -460,12 +514,15 @@ export async function handleBuildComplete({
460
514
''
461
515
) ,
462
516
assets : { } ,
463
- config :
464
- type === AdapterOutputType . MIDDLEWARE
517
+ wasmAssets : { } ,
518
+ config : {
519
+ ...( type === AdapterOutputType . MIDDLEWARE
465
520
? {
466
521
matchers : page . matchers ,
467
522
}
468
- : { } ,
523
+ : { } ) ,
524
+ env : page . env ,
525
+ } ,
469
526
}
470
527
471
528
function handleFile ( file : string ) {
@@ -482,9 +539,15 @@ export async function handleBuildComplete({
482
539
for ( const file of page . files ) {
483
540
handleFile ( file )
484
541
}
485
- for ( const item of [ ...( page . wasm || [ ] ) , ... ( page . assets || [ ] ) ] ) {
542
+ for ( const item of [ ...( page . assets || [ ] ) ] ) {
486
543
handleFile ( item . filePath )
487
544
}
545
+ for ( const item of page . wasm || [ ] ) {
546
+ if ( ! output . wasmAssets ) {
547
+ output . wasmAssets = { }
548
+ }
549
+ output . wasmAssets [ item . name ] = item . filePath
550
+ }
488
551
489
552
if ( type === AdapterOutputType . MIDDLEWARE ) {
490
553
outputs . middleware = output
@@ -528,12 +591,28 @@ export async function handleBuildComplete({
528
591
// if it's an auto static optimized page it's just
529
592
// a static file
530
593
if ( staticPages . has ( page ) ) {
531
- outputs . staticFiles . push ( {
532
- id : page ,
533
- pathname : route ,
534
- type : AdapterOutputType . STATIC_FILE ,
535
- filePath : pageFile . replace ( / \. j s $ / , '.html' ) ,
536
- } satisfies AdapterOutput [ 'STATIC_FILE' ] )
594
+ if ( config . i18n ) {
595
+ for ( const locale of config . i18n . locales || [ ] ) {
596
+ const localePage =
597
+ page === '/' ? `/${ locale } ` : addPathPrefix ( page , `/${ locale } ` )
598
+ outputs . staticFiles . push ( {
599
+ id : localePage ,
600
+ pathname : localePage ,
601
+ type : AdapterOutputType . STATIC_FILE ,
602
+ filePath : path . join (
603
+ pagesDistDir ,
604
+ `${ normalizePagePath ( localePage ) } .html`
605
+ ) ,
606
+ } satisfies AdapterOutput [ 'STATIC_FILE' ] )
607
+ }
608
+ } else {
609
+ outputs . staticFiles . push ( {
610
+ id : page ,
611
+ pathname : route ,
612
+ type : AdapterOutputType . STATIC_FILE ,
613
+ filePath : pageFile . replace ( / \. j s $ / , '.html' ) ,
614
+ } satisfies AdapterOutput [ 'STATIC_FILE' ] )
615
+ }
537
616
continue
538
617
}
539
618
@@ -639,7 +718,12 @@ export async function handleBuildComplete({
639
718
childRoute : string ,
640
719
allowMissing ?: boolean
641
720
) => {
642
- const parentOutput = pageOutputMap [ srcRoute ] || appOutputMap [ srcRoute ]
721
+ const normalizedSrcRoute = normalizeLocalePath (
722
+ srcRoute ,
723
+ config . i18n ?. locales || [ ]
724
+ ) . pathname
725
+ const parentOutput =
726
+ pageOutputMap [ normalizedSrcRoute ] || appOutputMap [ normalizedSrcRoute ]
643
727
644
728
if ( ! parentOutput && ! allowMissing ) {
645
729
console . error ( {
@@ -966,7 +1050,11 @@ export async function handleBuildComplete({
966
1050
}
967
1051
prerenderGroupId += 1
968
1052
}
1053
+ }
1054
+
1055
+ normalizePathnames ( config , outputs )
969
1056
1057
+ try {
970
1058
await adapterMod . onBuildComplete ( {
971
1059
routes : {
972
1060
dynamicRoutes : routesManifest . dynamicRoutes ,
0 commit comments