@@ -4,7 +4,7 @@ import { join, relative } from 'path'
4
4
import type { NetlifyPlugin } from '@netlify/build'
5
5
import { bold , redBright } from 'chalk'
6
6
import destr from 'destr'
7
- import { copy , ensureDir , existsSync , readFileSync } from 'fs-extra'
7
+ import { existsSync , readFileSync } from 'fs-extra'
8
8
import { outdent } from 'outdent'
9
9
10
10
import { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME } from './constants'
@@ -26,7 +26,7 @@ import {
26
26
getExtendedApiRouteConfigs ,
27
27
warnOnApiRoutes ,
28
28
} from './helpers/functions'
29
- import { generateRedirects } from './helpers/redirects'
29
+ import { generateRedirects , generateStaticRedirects } from './helpers/redirects'
30
30
import { shouldSkip , isNextAuthInstalled , getCustomImageResponseHeaders , getRemotePatterns } from './helpers/utils'
31
31
import {
32
32
verifyNetlifyBuildVersion ,
@@ -80,18 +80,12 @@ const plugin: NetlifyPlugin = {
80
80
81
81
checkNextSiteHasBuilt ( { publish, failBuild } )
82
82
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
+ {
85
85
publish,
86
86
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
+ )
95
89
await cleanupEdgeFunctions ( constants )
96
90
97
91
const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
@@ -123,7 +117,7 @@ const plugin: NetlifyPlugin = {
123
117
}
124
118
125
119
if ( isNextAuthInstalled ( ) ) {
126
- const config = await getRequiredServerFiles ( dotNextDir )
120
+ const config = await getRequiredServerFiles ( publish )
127
121
128
122
const userDefinedNextAuthUrl = config . config . env . NEXTAUTH_URL
129
123
@@ -140,7 +134,7 @@ const plugin: NetlifyPlugin = {
140
134
)
141
135
config . config . env . NEXTAUTH_URL = nextAuthUrl
142
136
143
- await updateRequiredServerFiles ( dotNextDir , config )
137
+ await updateRequiredServerFiles ( publish , config )
144
138
} else {
145
139
// Using the deploy prime url in production leads to issues because the unique deploy ID is part of the generated URL
146
140
// and will not match the expected URL in the callback URL of an OAuth application.
@@ -151,27 +145,30 @@ const plugin: NetlifyPlugin = {
151
145
console . log ( `NextAuth package detected, setting NEXTAUTH_URL environment variable to ${ nextAuthUrl } ` )
152
146
config . config . env . NEXTAUTH_URL = nextAuthUrl
153
147
154
- await updateRequiredServerFiles ( dotNextDir , config )
148
+ await updateRequiredServerFiles ( publish , config )
155
149
}
156
150
}
157
151
158
- const buildId = readFileSync ( join ( dotNextDir , 'BUILD_ID' ) , 'utf8' ) . trim ( )
152
+ const buildId = readFileSync ( join ( publish , 'BUILD_ID' ) , 'utf8' ) . trim ( )
159
153
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 )
162
156
163
157
await generateFunctions ( constants , appDir , apiRoutes )
164
158
await generatePagesResolver ( { target, constants } )
165
159
166
- await movePublicFiles ( { appDir, outdir, publishDir } )
160
+ await movePublicFiles ( { appDir, outdir, publish } )
167
161
168
162
await patchNextFiles ( appDir )
169
163
170
164
if ( ! destr ( process . env . SERVE_STATIC_FILES_FROM_ORIGIN ) ) {
171
- await moveStaticPages ( { distDir : dotNextDir , i18n, basePath, publishDir } )
165
+ await moveStaticPages ( { target , netlifyConfig , i18n, basePath } )
172
166
}
173
167
174
- await copy ( join ( dotNextDir , 'static' ) , join ( publishDir , '_next' , 'static' ) )
168
+ await generateStaticRedirects ( {
169
+ netlifyConfig,
170
+ nextConfig : { basePath, i18n } ,
171
+ } )
175
172
176
173
await setupImageFunction ( {
177
174
constants,
@@ -193,16 +190,20 @@ const plugin: NetlifyPlugin = {
193
190
} ,
194
191
195
192
async onPostBuild ( {
196
- netlifyConfig,
193
+ netlifyConfig : {
194
+ build : { publish } ,
195
+ redirects,
196
+ headers,
197
+ } ,
197
198
utils : {
198
199
status,
199
200
cache,
200
201
functions,
201
202
build : { failBuild } ,
202
203
} ,
203
- constants : { FUNCTIONS_DIST , PUBLISH_DIR } ,
204
+ constants : { FUNCTIONS_DIST } ,
204
205
} ) {
205
- await saveCache ( { cache, publish : netlifyConfig . build . publish } )
206
+ await saveCache ( { cache, publish } )
206
207
207
208
if ( shouldSkip ( ) ) {
208
209
status . show ( {
@@ -218,16 +219,15 @@ const plugin: NetlifyPlugin = {
218
219
219
220
await checkForOldFunctions ( { functions } )
220
221
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 } )
222
223
223
224
const { basePath, appDir } = nextConfig
224
225
225
- generateCustomHeaders ( nextConfig , netlifyConfig . headers )
226
+ generateCustomHeaders ( nextConfig , headers )
226
227
227
- warnForProblematicUserRewrites ( { basePath, redirects : netlifyConfig . redirects } )
228
+ warnForProblematicUserRewrites ( { basePath, redirects } )
228
229
warnForRootRedirects ( { appDir } )
229
230
await warnOnApiRoutes ( { FUNCTIONS_DIST } )
230
- netlifyConfig . build . publish = join ( PUBLISH_DIR , 'dist' )
231
231
} ,
232
232
}
233
233
// The types haven't been updated yet
0 commit comments