@@ -4,7 +4,7 @@ import { join, relative } from 'path'
44import type { NetlifyPlugin } from '@netlify/build'
55import { bold , redBright } from 'chalk'
66import destr from 'destr'
7- import { copy , ensureDir , existsSync , readFileSync } from 'fs-extra'
7+ import { existsSync , readFileSync } from 'fs-extra'
88import { outdent } from 'outdent'
99
1010import { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME } from './constants'
@@ -26,7 +26,7 @@ import {
2626 getExtendedApiRouteConfigs ,
2727 warnOnApiRoutes ,
2828} from './helpers/functions'
29- import { generateRedirects } from './helpers/redirects'
29+ import { generateRedirects , generateStaticRedirects } from './helpers/redirects'
3030import { shouldSkip , isNextAuthInstalled , getCustomImageResponseHeaders , getRemotePatterns } from './helpers/utils'
3131import {
3232 verifyNetlifyBuildVersion ,
@@ -80,18 +80,12 @@ const plugin: NetlifyPlugin = {
8080
8181 checkNextSiteHasBuilt ( { publish, failBuild } )
8282
83- const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental, distDir } =
84- await getNextConfig ( {
83+ const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental } = await getNextConfig (
84+ {
8585 publish,
8686 failBuild,
87- } )
88-
89- const dotNextDir = join ( appDir , distDir )
90-
91- // This is the *generated* publish dir. The user specifies .next, be we actually use this subdirectory
92- const publishDir = join ( dotNextDir , 'dist' )
93- await ensureDir ( publishDir )
94-
87+ } ,
88+ )
9589 await cleanupEdgeFunctions ( constants )
9690
9791 const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
@@ -123,7 +117,7 @@ const plugin: NetlifyPlugin = {
123117 }
124118
125119 if ( isNextAuthInstalled ( ) ) {
126- const config = await getRequiredServerFiles ( dotNextDir )
120+ const config = await getRequiredServerFiles ( publish )
127121
128122 const userDefinedNextAuthUrl = config . config . env . NEXTAUTH_URL
129123
@@ -140,7 +134,7 @@ const plugin: NetlifyPlugin = {
140134 )
141135 config . config . env . NEXTAUTH_URL = nextAuthUrl
142136
143- await updateRequiredServerFiles ( dotNextDir , config )
137+ await updateRequiredServerFiles ( publish , config )
144138 } else {
145139 // Using the deploy prime url in production leads to issues because the unique deploy ID is part of the generated URL
146140 // and will not match the expected URL in the callback URL of an OAuth application.
@@ -151,27 +145,30 @@ const plugin: NetlifyPlugin = {
151145 console . log ( `NextAuth package detected, setting NEXTAUTH_URL environment variable to ${ nextAuthUrl } ` )
152146 config . config . env . NEXTAUTH_URL = nextAuthUrl
153147
154- await updateRequiredServerFiles ( dotNextDir , config )
148+ await updateRequiredServerFiles ( publish , config )
155149 }
156150 }
157151
158- const buildId = readFileSync ( join ( dotNextDir , 'BUILD_ID' ) , 'utf8' ) . trim ( )
152+ const buildId = readFileSync ( join ( publish , 'BUILD_ID' ) , 'utf8' ) . trim ( )
159153
160- await configureHandlerFunctions ( { netlifyConfig, ignore, publish : relative ( process . cwd ( ) , dotNextDir ) } )
161- const apiRoutes = await getExtendedApiRouteConfigs ( dotNextDir , appDir )
154+ await configureHandlerFunctions ( { netlifyConfig, ignore, publish : relative ( process . cwd ( ) , publish ) } )
155+ const apiRoutes = await getExtendedApiRouteConfigs ( publish , appDir )
162156
163157 await generateFunctions ( constants , appDir , apiRoutes )
164158 await generatePagesResolver ( { target, constants } )
165159
166- await movePublicFiles ( { appDir, outdir, publishDir } )
160+ await movePublicFiles ( { appDir, outdir, publish } )
167161
168162 await patchNextFiles ( appDir )
169163
170164 if ( ! destr ( process . env . SERVE_STATIC_FILES_FROM_ORIGIN ) ) {
171- await moveStaticPages ( { distDir : dotNextDir , i18n, basePath, publishDir } )
165+ await moveStaticPages ( { target , netlifyConfig , i18n, basePath } )
172166 }
173167
174- await copy ( join ( dotNextDir , 'static' ) , join ( publishDir , '_next' , 'static' ) )
168+ await generateStaticRedirects ( {
169+ netlifyConfig,
170+ nextConfig : { basePath, i18n } ,
171+ } )
175172
176173 await setupImageFunction ( {
177174 constants,
@@ -193,16 +190,20 @@ const plugin: NetlifyPlugin = {
193190 } ,
194191
195192 async onPostBuild ( {
196- netlifyConfig,
193+ netlifyConfig : {
194+ build : { publish } ,
195+ redirects,
196+ headers,
197+ } ,
197198 utils : {
198199 status,
199200 cache,
200201 functions,
201202 build : { failBuild } ,
202203 } ,
203- constants : { FUNCTIONS_DIST , PUBLISH_DIR } ,
204+ constants : { FUNCTIONS_DIST } ,
204205 } ) {
205- await saveCache ( { cache, publish : netlifyConfig . build . publish } )
206+ await saveCache ( { cache, publish } )
206207
207208 if ( shouldSkip ( ) ) {
208209 status . show ( {
@@ -218,16 +219,15 @@ const plugin: NetlifyPlugin = {
218219
219220 await checkForOldFunctions ( { functions } )
220221 await checkZipSize ( join ( FUNCTIONS_DIST , `${ ODB_FUNCTION_NAME } .zip` ) )
221- const nextConfig = await getNextConfig ( { publish : netlifyConfig . build . publish , failBuild } )
222+ const nextConfig = await getNextConfig ( { publish, failBuild } )
222223
223224 const { basePath, appDir } = nextConfig
224225
225- generateCustomHeaders ( nextConfig , netlifyConfig . headers )
226+ generateCustomHeaders ( nextConfig , headers )
226227
227- warnForProblematicUserRewrites ( { basePath, redirects : netlifyConfig . redirects } )
228+ warnForProblematicUserRewrites ( { basePath, redirects } )
228229 warnForRootRedirects ( { appDir } )
229230 await warnOnApiRoutes ( { FUNCTIONS_DIST } )
230- netlifyConfig . build . publish = join ( PUBLISH_DIR , 'dist' )
231231 } ,
232232}
233233// The types haven't been updated yet
0 commit comments