@@ -81,9 +81,6 @@ import {
81
81
detectConflictingPaths ,
82
82
computeFromManifest ,
83
83
getJsPageSizeInKb ,
84
- getNamedExports ,
85
- hasCustomGetInitialProps ,
86
- isPageStatic ,
87
84
PageInfo ,
88
85
printCustomRoutes ,
89
86
printTreeView ,
@@ -266,7 +263,7 @@ export default async function build(
266
263
)
267
264
const pageKeys = Object . keys ( mappedPages )
268
265
const conflictingPublicFiles : string [ ] = [ ]
269
- const hasCustomErrorPage = mappedPages [ '/_error' ] . startsWith (
266
+ const hasCustomErrorPage : boolean = mappedPages [ '/_error' ] . startsWith (
270
267
'private-next-pages'
271
268
)
272
269
const hasPages404 = Boolean (
@@ -656,219 +653,228 @@ export default async function build(
656
653
await promises . readFile ( buildManifestPath , 'utf8' )
657
654
) as BuildManifest
658
655
659
- let customAppGetInitialProps : boolean | undefined
660
- let namedExports : Array < string > | undefined
661
- let isNextImageImported : boolean | undefined
662
656
const analysisBegin = process . hrtime ( )
663
- let hasSsrAmpPages = false
664
657
665
658
const staticCheckSpan = nextBuildSpan . traceChild ( 'static-check' )
666
- const { hasNonStaticErrorPage } = await staticCheckSpan . traceAsyncFn (
667
- async ( ) => {
668
- process . env . NEXT_PHASE = PHASE_PRODUCTION_BUILD
659
+ const {
660
+ customAppGetInitialProps,
661
+ namedExports,
662
+ isNextImageImported,
663
+ hasSsrAmpPages,
664
+ hasNonStaticErrorPage,
665
+ } = await staticCheckSpan . traceAsyncFn ( async ( ) => {
666
+ process . env . NEXT_PHASE = PHASE_PRODUCTION_BUILD
667
+
668
+ const staticCheckWorkers = new Worker ( staticCheckWorker , {
669
+ numWorkers : config . experimental . cpus ,
670
+ enableWorkerThreads : config . experimental . workerThreads ,
671
+ } ) as Worker & typeof import ( './utils' )
672
+
673
+ staticCheckWorkers . getStdout ( ) . pipe ( process . stdout )
674
+ staticCheckWorkers . getStderr ( ) . pipe ( process . stderr )
675
+
676
+ const runtimeEnvConfig = {
677
+ publicRuntimeConfig : config . publicRuntimeConfig ,
678
+ serverRuntimeConfig : config . serverRuntimeConfig ,
679
+ }
669
680
670
- const staticCheckWorkers = new Worker ( staticCheckWorker , {
671
- numWorkers : config . experimental . cpus ,
672
- enableWorkerThreads : config . experimental . workerThreads ,
673
- } ) as Worker & { isPageStatic : typeof isPageStatic }
681
+ const nonStaticErrorPageSpan = staticCheckSpan . traceChild (
682
+ 'check-static-error-page'
683
+ )
684
+ const nonStaticErrorPagePromise = nonStaticErrorPageSpan . traceAsyncFn (
685
+ async ( ) =>
686
+ hasCustomErrorPage &&
687
+ ( await staticCheckWorkers . hasCustomGetInitialProps (
688
+ '/_error' ,
689
+ distDir ,
690
+ isLikeServerless ,
691
+ runtimeEnvConfig ,
692
+ false
693
+ ) )
694
+ )
695
+ // we don't output _app in serverless mode so use _app export
696
+ // from _error instead
697
+ const appPageToCheck = isLikeServerless ? '/_error' : '/_app'
674
698
675
- staticCheckWorkers . getStdout ( ) . pipe ( process . stdout )
676
- staticCheckWorkers . getStderr ( ) . pipe ( process . stderr )
699
+ const customAppGetInitialPropsPromise = staticCheckWorkers . hasCustomGetInitialProps (
700
+ appPageToCheck ,
701
+ distDir ,
702
+ isLikeServerless ,
703
+ runtimeEnvConfig ,
704
+ true
705
+ )
677
706
678
- const runtimeEnvConfig = {
679
- publicRuntimeConfig : config . publicRuntimeConfig ,
680
- serverRuntimeConfig : config . serverRuntimeConfig ,
681
- }
707
+ const namedExportsPromise = staticCheckWorkers . getNamedExports (
708
+ appPageToCheck ,
709
+ distDir ,
710
+ isLikeServerless ,
711
+ runtimeEnvConfig
712
+ )
682
713
683
- const nonStaticErrorPageSpan = staticCheckSpan . traceChild (
684
- 'check-static-error-page'
685
- )
686
- const nonStaticErrorPage = await nonStaticErrorPageSpan . traceAsyncFn (
687
- async ( ) =>
688
- hasCustomErrorPage &&
689
- ( await hasCustomGetInitialProps (
690
- '/_error' ,
714
+ // eslint-disable-next-line no-shadow
715
+ let isNextImageImported : boolean | undefined
716
+ // eslint-disable-next-line no-shadow
717
+ let hasSsrAmpPages = false
718
+
719
+ const computedManifestData = await computeFromManifest (
720
+ buildManifest ,
721
+ distDir ,
722
+ config . experimental . gzipSize
723
+ )
724
+ await Promise . all (
725
+ pageKeys . map ( async ( page ) => {
726
+ const checkPageSpan = staticCheckSpan . traceChild ( 'check-page' , {
727
+ page,
728
+ } )
729
+ return checkPageSpan . traceAsyncFn ( async ( ) => {
730
+ const actualPage = normalizePagePath ( page )
731
+ const [ selfSize , allSize ] = await getJsPageSizeInKb (
732
+ actualPage ,
691
733
distDir ,
692
- isLikeServerless ,
693
- runtimeEnvConfig ,
694
- false
695
- ) )
696
- )
697
- // we don't output _app in serverless mode so use _app export
698
- // from _error instead
699
- const appPageToCheck = isLikeServerless ? '/_error' : '/_app'
734
+ buildManifest ,
735
+ config . experimental . gzipSize ,
736
+ computedManifestData
737
+ )
700
738
701
- customAppGetInitialProps = await hasCustomGetInitialProps (
702
- appPageToCheck ,
703
- distDir ,
704
- isLikeServerless ,
705
- runtimeEnvConfig ,
706
- true
707
- )
739
+ let isSsg = false
740
+ let isStatic = false
741
+ let isHybridAmp = false
742
+ let ssgPageRoutes : string [ ] | null = null
708
743
709
- namedExports = await getNamedExports (
710
- appPageToCheck ,
711
- distDir ,
712
- isLikeServerless ,
713
- runtimeEnvConfig
714
- )
744
+ const nonReservedPage = ! page . match (
745
+ / ^ \/ ( _ a p p | _ e r r o r | _ d o c u m e n t | a p i ( \/ | $ ) ) /
746
+ )
715
747
716
- if ( customAppGetInitialProps ) {
717
- console . warn (
718
- chalk . bold . yellow ( `Warning: ` ) +
719
- chalk . yellow (
720
- `You have opted-out of Automatic Static Optimization due to \`getInitialProps\` in \`pages/_app\`. This does not opt-out pages with \`getStaticProps\``
721
- )
722
- )
723
- console . warn (
724
- 'Read more: https://nextjs.org/docs/messages/opt-out-auto-static-optimization\n'
725
- )
726
- }
748
+ if ( nonReservedPage ) {
749
+ try {
750
+ let isPageStaticSpan = checkPageSpan . traceChild (
751
+ 'is-page-static'
752
+ )
753
+ let workerResult = await isPageStaticSpan . traceAsyncFn ( ( ) => {
754
+ return staticCheckWorkers . isPageStatic (
755
+ page ,
756
+ distDir ,
757
+ isLikeServerless ,
758
+ runtimeEnvConfig ,
759
+ config . i18n ?. locales ,
760
+ config . i18n ?. defaultLocale ,
761
+ isPageStaticSpan . id
762
+ )
763
+ } )
727
764
728
- const computedManifestData = await computeFromManifest (
729
- buildManifest ,
730
- distDir ,
731
- config . experimental . gzipSize
732
- )
733
- await Promise . all (
734
- pageKeys . map ( async ( page ) => {
735
- const checkPageSpan = staticCheckSpan . traceChild ( 'check-page' , {
736
- page,
737
- } )
738
- return checkPageSpan . traceAsyncFn ( async ( ) => {
739
- const actualPage = normalizePagePath ( page )
740
- const [ selfSize , allSize ] = await getJsPageSizeInKb (
741
- actualPage ,
742
- distDir ,
743
- buildManifest ,
744
- config . experimental . gzipSize ,
745
- computedManifestData
746
- )
765
+ if (
766
+ workerResult . isStatic === false &&
767
+ ( workerResult . isHybridAmp || workerResult . isAmpOnly )
768
+ ) {
769
+ hasSsrAmpPages = true
770
+ }
747
771
748
- let isSsg = false
749
- let isStatic = false
750
- let isHybridAmp = false
751
- let ssgPageRoutes : string [ ] | null = null
772
+ if ( workerResult . isHybridAmp ) {
773
+ isHybridAmp = true
774
+ hybridAmpPages . add ( page )
775
+ }
752
776
753
- const nonReservedPage = ! page . match (
754
- / ^ \/ ( _ a p p | _ e r r o r | _ d o c u m e n t | a p i ( \/ | $ ) ) /
755
- )
777
+ if ( workerResult . isNextImageImported ) {
778
+ isNextImageImported = true
779
+ }
756
780
757
- if ( nonReservedPage ) {
758
- try {
759
- let isPageStaticSpan = checkPageSpan . traceChild (
760
- 'is-page-static'
761
- )
762
- let workerResult = await isPageStaticSpan . traceAsyncFn ( ( ) => {
763
- return staticCheckWorkers . isPageStatic (
764
- page ,
765
- distDir ,
766
- isLikeServerless ,
767
- runtimeEnvConfig ,
768
- config . i18n ?. locales ,
769
- config . i18n ?. defaultLocale ,
770
- isPageStaticSpan . id
771
- )
772
- } )
781
+ if ( workerResult . hasStaticProps ) {
782
+ ssgPages . add ( page )
783
+ isSsg = true
773
784
774
785
if (
775
- workerResult . isStatic === false &&
776
- ( workerResult . isHybridAmp || workerResult . isAmpOnly )
786
+ workerResult . prerenderRoutes &&
787
+ workerResult . encodedPrerenderRoutes
777
788
) {
778
- hasSsrAmpPages = true
779
- }
780
-
781
- if ( workerResult . isHybridAmp ) {
782
- isHybridAmp = true
783
- hybridAmpPages . add ( page )
784
- }
785
-
786
- if ( workerResult . isNextImageImported ) {
787
- isNextImageImported = true
788
- }
789
-
790
- if ( workerResult . hasStaticProps ) {
791
- ssgPages . add ( page )
792
- isSsg = true
793
-
794
- if (
795
- workerResult . prerenderRoutes &&
789
+ additionalSsgPaths . set ( page , workerResult . prerenderRoutes )
790
+ additionalSsgPathsEncoded . set (
791
+ page ,
796
792
workerResult . encodedPrerenderRoutes
797
- ) {
798
- additionalSsgPaths . set ( page , workerResult . prerenderRoutes )
799
- additionalSsgPathsEncoded . set (
800
- page ,
801
- workerResult . encodedPrerenderRoutes
802
- )
803
- ssgPageRoutes = workerResult . prerenderRoutes
804
- }
805
-
806
- if ( workerResult . prerenderFallback === 'blocking' ) {
807
- ssgBlockingFallbackPages . add ( page )
808
- } else if ( workerResult . prerenderFallback === true ) {
809
- ssgStaticFallbackPages . add ( page )
810
- }
811
- } else if ( workerResult . hasServerProps ) {
812
- serverPropsPages . add ( page )
813
- } else if (
814
- workerResult . isStatic &&
815
- customAppGetInitialProps === false
816
- ) {
817
- staticPages . add ( page )
818
- isStatic = true
793
+ )
794
+ ssgPageRoutes = workerResult . prerenderRoutes
819
795
}
820
796
821
- if ( hasPages404 && page === '/404' ) {
822
- if (
823
- ! workerResult . isStatic &&
824
- ! workerResult . hasStaticProps
825
- ) {
826
- throw new Error (
827
- `\`pages/404\` ${ STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR } `
828
- )
829
- }
830
- // we need to ensure the 404 lambda is present since we use
831
- // it when _app has getInitialProps
832
- if (
833
- customAppGetInitialProps &&
834
- ! workerResult . hasStaticProps
835
- ) {
836
- staticPages . delete ( page )
837
- }
797
+ if ( workerResult . prerenderFallback === 'blocking' ) {
798
+ ssgBlockingFallbackPages . add ( page )
799
+ } else if ( workerResult . prerenderFallback === true ) {
800
+ ssgStaticFallbackPages . add ( page )
838
801
}
802
+ } else if ( workerResult . hasServerProps ) {
803
+ serverPropsPages . add ( page )
804
+ } else if (
805
+ workerResult . isStatic &&
806
+ ( await customAppGetInitialPropsPromise ) === false
807
+ ) {
808
+ staticPages . add ( page )
809
+ isStatic = true
810
+ }
839
811
812
+ if ( hasPages404 && page === '/404' ) {
813
+ if ( ! workerResult . isStatic && ! workerResult . hasStaticProps ) {
814
+ throw new Error (
815
+ `\`pages/404\` ${ STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR } `
816
+ )
817
+ }
818
+ // we need to ensure the 404 lambda is present since we use
819
+ // it when _app has getInitialProps
840
820
if (
841
- STATIC_STATUS_PAGES . includes ( page ) &&
842
- ! workerResult . isStatic &&
821
+ ( await customAppGetInitialPropsPromise ) &&
843
822
! workerResult . hasStaticProps
844
823
) {
845
- throw new Error (
846
- `\`pages${ page } \` ${ STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR } `
847
- )
824
+ staticPages . delete ( page )
848
825
}
849
- } catch ( err ) {
850
- if ( err . message !== 'INVALID_DEFAULT_EXPORT' ) throw err
851
- invalidPages . add ( page )
852
826
}
827
+
828
+ if (
829
+ STATIC_STATUS_PAGES . includes ( page ) &&
830
+ ! workerResult . isStatic &&
831
+ ! workerResult . hasStaticProps
832
+ ) {
833
+ throw new Error (
834
+ `\`pages${ page } \` ${ STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR } `
835
+ )
836
+ }
837
+ } catch ( err ) {
838
+ if ( err . message !== 'INVALID_DEFAULT_EXPORT' ) throw err
839
+ invalidPages . add ( page )
853
840
}
841
+ }
854
842
855
- pageInfos . set ( page , {
856
- size : selfSize ,
857
- totalSize : allSize ,
858
- static : isStatic ,
859
- isSsg,
860
- isHybridAmp,
861
- ssgPageRoutes,
862
- initialRevalidateSeconds : false ,
863
- } )
843
+ pageInfos . set ( page , {
844
+ size : selfSize ,
845
+ totalSize : allSize ,
846
+ static : isStatic ,
847
+ isSsg,
848
+ isHybridAmp,
849
+ ssgPageRoutes,
850
+ initialRevalidateSeconds : false ,
864
851
} )
865
852
} )
866
- )
867
- staticCheckWorkers . end ( )
868
-
869
- return { hasNonStaticErrorPage : nonStaticErrorPage }
853
+ } )
854
+ )
855
+ const returnValue = {
856
+ customAppGetInitialProps : await customAppGetInitialPropsPromise ,
857
+ namedExports : await namedExportsPromise ,
858
+ isNextImageImported,
859
+ hasSsrAmpPages,
860
+ hasNonStaticErrorPage : await nonStaticErrorPagePromise ,
870
861
}
871
- )
862
+
863
+ staticCheckWorkers . end ( )
864
+ return returnValue
865
+ } )
866
+
867
+ if ( customAppGetInitialProps ) {
868
+ console . warn (
869
+ chalk . bold . yellow ( `Warning: ` ) +
870
+ chalk . yellow (
871
+ `You have opted-out of Automatic Static Optimization due to \`getInitialProps\` in \`pages/_app\`. This does not opt-out pages with \`getStaticProps\``
872
+ )
873
+ )
874
+ console . warn (
875
+ 'Read more: https://nextjs.org/docs/messages/opt-out-auto-static-optimization\n'
876
+ )
877
+ }
872
878
873
879
if ( ! hasSsrAmpPages ) {
874
880
requiredServerFiles . ignore . push (
0 commit comments