Skip to content

Commit ccee3a7

Browse files
committed
Use target server
1 parent 575cc29 commit ccee3a7

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed

packages/runtime/src/index.ts

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
EnvConfig,
3939
excludeFiles,
4040
ExperimentalTraceVersion,
41+
NextServerTargetVersion,
4142
getDynamicRoutes,
4243
getExportIntent,
4344
getExportStatus,
@@ -89,7 +90,7 @@ export const version = 2;
8990
const htmlContentType = 'text/html; charset=utf-8';
9091
const nowDevChildProcesses = new Set<ChildProcess>();
9192

92-
['SIGINT', 'SIGTERM'].forEach(signal => {
93+
['SIGINT', 'SIGTERM'].forEach((signal) => {
9394
process.once(signal as NodeJS.Signals, () => {
9495
for (const child of nowDevChildProcesses) {
9596
debug(
@@ -193,6 +194,17 @@ function isLegacyNext(nextVersion: string) {
193194
return true;
194195
}
195196

197+
/**
198+
* Check if the target: `server` can be used to build the app
199+
*/
200+
function isServerTargetNext(nextVersion: string): boolean {
201+
if (nextVersion === 'canary' || nextVersion === 'latest') {
202+
return true;
203+
}
204+
205+
return semver.gte(NextServerTargetVersion, nextVersion);
206+
}
207+
196208
const name = '[@vercel/next]';
197209
const urls: stringMap = {};
198210

@@ -256,12 +268,6 @@ export async function build({
256268
...spawnOpts.env,
257269
NOW_BUILDER: '1',
258270
VERCEL: '1',
259-
// Next.js changed the default output folder beginning with
260-
// 10.0.8-canary.15 from `.next/serverless` to `.next/server`.
261-
// This is an opt-out of this behavior until we support it.
262-
// https://github.com/dealmore/terraform-aws-next-js/issues/86
263-
// https://github.com/vercel/next.js/pull/22731
264-
NEXT_PRIVATE_TARGET: 'experimental-serverless-trace',
265271
// We override init CWD here with the entrypoint to ensure that applications
266272
// can get the CWD from the download directory root
267273
INIT_CWD: entryPath,
@@ -345,8 +351,15 @@ export async function build({
345351
console.warn('WARNING: You should not upload the `.next` directory.');
346352
}
347353

348-
const isLegacy = nextVersionRange && isLegacyNext(nextVersionRange);
349-
debug(`MODE: ${isLegacy ? 'legacy' : 'serverless'}`);
354+
const isServerTarget =
355+
nextVersionRange && isServerTargetNext(nextVersionRange);
356+
const isLegacy =
357+
!isServerTarget && nextVersionRange && isLegacyNext(nextVersionRange);
358+
debug(
359+
`MODE: ${
360+
isServerTarget ? 'server(less)' : isLegacy ? 'legacy' : 'serverless'
361+
}`
362+
);
350363

351364
if (isLegacy) {
352365
console.warn(
@@ -430,7 +443,11 @@ export async function build({
430443
});
431444
}
432445

433-
if (!isLegacy) {
446+
if (isServerTarget) {
447+
debug(
448+
`Application is being built in server mode since 10.0.9 meets minimum version of v${NextServerTargetVersion}`
449+
);
450+
} else if (!isLegacy) {
434451
await createServerlessConfig(workPath, entryPath, nextVersion);
435452
}
436453

@@ -571,7 +588,7 @@ export async function build({
571588
`${(ssgDataRoute && ssgDataRoute.dataRoute) || dataRoute.page}${
572589
dataRoute.routeKeys
573590
? `?${Object.keys(dataRoute.routeKeys)
574-
.map(key => `${dataRoute.routeKeys![key]}=$${key}`)
591+
.map((key) => `${dataRoute.routeKeys![key]}=$${key}`)
575592
.join('&')}`
576593
: ''
577594
}`
@@ -590,7 +607,7 @@ export async function build({
590607
`/${escapedBuildId}/(?${
591608
ssgDataRoute ? '<nextLocale>' : ':'
592609
}${i18n.locales
593-
.map(locale => escapeStringRegexp(locale))
610+
.map((locale) => escapeStringRegexp(locale))
594611
.join('|')})/`
595612
);
596613

@@ -604,7 +621,7 @@ export async function build({
604621
`/${escapedBuildId}/(?${
605622
ssgDataRoute ? '<nextLocale>' : ':'
606623
}${i18n.locales
607-
.map(locale => escapeStringRegexp(locale))
624+
.map((locale) => escapeStringRegexp(locale))
608625
.join('|')})[/]?`
609626
);
610627
}
@@ -847,7 +864,7 @@ export async function build({
847864
);
848865
const nodeModules = excludeFiles(
849866
await glob('node_modules/**', entryPath),
850-
file => file.startsWith('node_modules/.cache')
867+
(file) => file.startsWith('node_modules/.cache')
851868
);
852869
const launcherFiles = {
853870
'now__bridge.js': new FileFsRef({
@@ -876,7 +893,7 @@ export async function build({
876893
const launcherData = await readFile(launcherPath, 'utf8');
877894

878895
await Promise.all(
879-
Object.keys(pages).map(async page => {
896+
Object.keys(pages).map(async (page) => {
880897
// These default pages don't have to be handled as they'd always 404
881898
if (['_app.js', '_error.js', '_document.js'].includes(page)) {
882899
return;
@@ -933,7 +950,7 @@ export async function build({
933950
const pagesDir = path.join(
934951
entryPath,
935952
outputDirectory,
936-
'serverless',
953+
isServerTarget ? 'server' : 'serverless',
937954
'pages'
938955
);
939956

@@ -1034,9 +1051,9 @@ export async function build({
10341051
};
10351052

10361053
const isApiPage = (page: string) =>
1037-
page.replace(/\\/g, '/').match(/serverless\/pages\/api(\/|\.js$)/);
1054+
page.replace(/\\/g, '/').match(/server(less)?\/pages\/api(\/|\.js$)/);
10381055

1039-
const canUsePreviewMode = Object.keys(pages).some(page =>
1056+
const canUsePreviewMode = Object.keys(pages).some((page) =>
10401057
isApiPage(pages[page].fsPath)
10411058
);
10421059

@@ -1068,7 +1085,7 @@ export async function build({
10681085
}
10691086
};
10701087

1071-
Object.keys(prerenderManifest.staticRoutes).forEach(route =>
1088+
Object.keys(prerenderManifest.staticRoutes).forEach((route) =>
10721089
onPrerenderRouteInitial(route)
10731090
);
10741091

@@ -1208,7 +1225,7 @@ export async function build({
12081225
debug(
12091226
'detected (legacy) assets to be bundled with serverless function:'
12101227
);
1211-
assetKeys.forEach(assetFile => debug(`\t${assetFile}`));
1228+
assetKeys.forEach((assetFile) => debug(`\t${assetFile}`));
12121229
debug(
12131230
'\nPlease upgrade to Next.js 9.1 to leverage modern asset handling.'
12141231
);
@@ -1334,7 +1351,7 @@ export async function build({
13341351
if (i18n) {
13351352
addPageLambdaRoute(
13361353
`[/]?(?:${i18n.locales
1337-
.map(locale => escapeStringRegexp(locale))
1354+
.map((locale) => escapeStringRegexp(locale))
13381355
.join('|')})?${escapeStringRegexp(outputName)}`
13391356
);
13401357
} else {
@@ -1368,7 +1385,7 @@ export async function build({
13681385
}
13691386
} else {
13701387
await Promise.all(
1371-
pageKeys.map(async page => {
1388+
pageKeys.map(async (page) => {
13721389
// These default pages don't have to be handled as they'd always 404
13731390
if (['_app.js', '_document.js'].includes(page)) {
13741391
return;
@@ -1465,8 +1482,8 @@ export async function build({
14651482
false,
14661483
routesManifest,
14671484
new Set(prerenderManifest.omittedRoutes)
1468-
).then(arr =>
1469-
arr.map(route => {
1485+
).then((arr) =>
1486+
arr.map((route) => {
14701487
const { i18n } = routesManifest || {};
14711488

14721489
if (i18n) {
@@ -1486,7 +1503,7 @@ export async function build({
14861503
`^${dynamicPrefix ? `${dynamicPrefix}[/]?` : '[/]?'}(?${
14871504
isLocalePrefixed ? '<nextLocale>' : ':'
14881505
}${i18n.locales
1489-
.map(locale => escapeStringRegexp(locale))
1506+
.map((locale) => escapeStringRegexp(locale))
14901507
.join('|')})?`
14911508
);
14921509

@@ -1517,8 +1534,8 @@ export async function build({
15171534
dynamicPages,
15181535
false,
15191536
routesManifest
1520-
).then(arr =>
1521-
arr.map(route => {
1537+
).then((arr) =>
1538+
arr.map((route) => {
15221539
route.src = route.src.replace('^', `^${dynamicPrefix}`);
15231540
return route;
15241541
})
@@ -1563,7 +1580,7 @@ export async function build({
15631580
const pages = {
15641581
${groupPageKeys
15651582
.map(
1566-
page =>
1583+
(page) =>
15671584
`'${page}': () => require('./${path.join(
15681585
'./',
15691586
group.pages[page].pageFileName
@@ -1609,7 +1626,7 @@ export async function build({
16091626
// for prerendered dynamic routes (/blog/post-1) we need to
16101627
// find the match since it won't match the page directly
16111628
const dynamicRoutes = ${JSON.stringify(
1612-
completeDynamicRoutes.map(route => ({
1629+
completeDynamicRoutes.map((route) => ({
16131630
src: route.src,
16141631
dest: route.dest,
16151632
}))
@@ -1936,13 +1953,13 @@ export async function build({
19361953
}
19371954
};
19381955

1939-
Object.keys(prerenderManifest.staticRoutes).forEach(route =>
1956+
Object.keys(prerenderManifest.staticRoutes).forEach((route) =>
19401957
onPrerenderRoute(route, { isBlocking: false, isFallback: false })
19411958
);
1942-
Object.keys(prerenderManifest.fallbackRoutes).forEach(route =>
1959+
Object.keys(prerenderManifest.fallbackRoutes).forEach((route) =>
19431960
onPrerenderRoute(route, { isBlocking: false, isFallback: true })
19441961
);
1945-
Object.keys(prerenderManifest.blockingFallbackRoutes).forEach(route =>
1962+
Object.keys(prerenderManifest.blockingFallbackRoutes).forEach((route) =>
19461963
onPrerenderRoute(route, { isBlocking: true, isFallback: false })
19471964
);
19481965

@@ -2021,7 +2038,7 @@ export async function build({
20212038
// We need to delete lambdas from output instead of omitting them from the
20222039
// start since we rely on them for powering Preview Mode (read above in
20232040
// onPrerenderRoute).
2024-
prerenderManifest.omittedRoutes.forEach(routeKey => {
2041+
prerenderManifest.omittedRoutes.forEach((routeKey) => {
20252042
// Get the route file as it'd be mounted in the builder output
20262043
const routeFileNoExt = path.posix.join(
20272044
entryDirectory,
@@ -2081,7 +2098,7 @@ export async function build({
20812098

20822099
const trailingSlashRedirects: Route[] = [];
20832100

2084-
redirects = redirects.filter(_redir => {
2101+
redirects = redirects.filter((_redir) => {
20852102
const redir = _redir as Source;
20862103
// detect the trailing slash redirect and make sure it's
20872104
// kept above the wildcard mapping to prevent erroneous redirects
@@ -2113,7 +2130,7 @@ export async function build({
21132130
...staticDirectoryFiles,
21142131
},
21152132
wildcard: i18n?.domains
2116-
? i18n?.domains.map(item => {
2133+
? i18n?.domains.map((item) => {
21172134
return {
21182135
domain: item.domain,
21192136
value:
@@ -2156,7 +2173,7 @@ export async function build({
21562173
entryDirectory,
21572174
'/'
21582175
)}(?!(?:_next/.*|${i18n.locales
2159-
.map(locale => escapeStringRegexp(locale))
2176+
.map((locale) => escapeStringRegexp(locale))
21602177
.join('|')})(?:/.*|$))(.*)$`,
21612178
// we aren't able to ensure trailing slash mode here
21622179
// so ensure this comes after the trailing slash redirect
@@ -2172,7 +2189,7 @@ export async function build({
21722189
'/',
21732190
entryDirectory
21742191
)}/?(?:${i18n.locales
2175-
.map(locale => escapeStringRegexp(locale))
2192+
.map((locale) => escapeStringRegexp(locale))
21762193
.join('|')})?/?$`,
21772194
locale: {
21782195
redirect: i18n.domains.reduce(
@@ -2182,7 +2199,7 @@ export async function build({
21822199
}://${item.domain}/`;
21832200

21842201
if (item.locales) {
2185-
item.locales.map(locale => {
2202+
item.locales.map((locale) => {
21862203
prev[locale] = `http${item.http ? '' : 's'}://${
21872204
item.domain
21882205
}/${locale}`;
@@ -2241,7 +2258,7 @@ export async function build({
22412258
entryDirectory,
22422259
'/'
22432260
)}(?!(?:_next/.*|${i18n.locales
2244-
.map(locale => escapeStringRegexp(locale))
2261+
.map((locale) => escapeStringRegexp(locale))
22452262
.join('|')})(?:/.*|$))(.*)$`,
22462263
dest: `/${i18n.defaultLocale}/$1`,
22472264
continue: true,
@@ -2264,7 +2281,7 @@ export async function build({
22642281
entryDirectory,
22652282
'/'
22662283
)}(?:${i18n.locales
2267-
.map(locale => escapeStringRegexp(locale))
2284+
.map((locale) => escapeStringRegexp(locale))
22682285
.join('|')})?[/]?404`,
22692286
status: 404,
22702287
continue: true,
@@ -2283,7 +2300,7 @@ export async function build({
22832300
{ handle: 'filesystem' },
22842301

22852302
// map pages to their lambda
2286-
...pageLambdaRoutes.filter(route => {
2303+
...pageLambdaRoutes.filter((route) => {
22872304
// filter out any SSG pages as they are already present in output
22882305
if ('headers' in route) {
22892306
let page = route.headers?.['x-nextjs-page']!;
@@ -2331,7 +2348,7 @@ export async function build({
23312348
'/',
23322349
entryDirectory
23332350
)}/?(?:${i18n.locales
2334-
.map(locale => escapeStringRegexp(locale))
2351+
.map((locale) => escapeStringRegexp(locale))
23352352
.join('|')})/(.*)`,
23362353
dest: `${path.join('/', entryDirectory, '/')}$1`,
23372354
check: true,
@@ -2350,7 +2367,7 @@ export async function build({
23502367
entryDirectory,
23512368
'/'
23522369
)}(?:${i18n?.locales
2353-
.map(locale => escapeStringRegexp(locale))
2370+
.map((locale) => escapeStringRegexp(locale))
23542371
.join('|')})/(.*)`,
23552372
dest: '/$1',
23562373
check: true,
@@ -2406,7 +2423,7 @@ export async function build({
24062423
entryDirectory,
24072424
'/'
24082425
)}(?<nextLocale>${i18n.locales
2409-
.map(locale => escapeStringRegexp(locale))
2426+
.map((locale) => escapeStringRegexp(locale))
24102427
.join('|')})(/.*|$)`,
24112428
dest: '/$nextLocale/404',
24122429
status: 404,

packages/runtime/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ function syncEnvVars(base: EnvConfig, removeEnv: EnvConfig, addEnv: EnvConfig) {
564564

565565
export const ExperimentalTraceVersion = `9.0.4-canary.1`;
566566

567+
/**
568+
* Next.js version that uses default target `server` for serverless builds
569+
*/
570+
export const NextServerTargetVersion = '10.0.9-canary.4';
571+
567572
export type PseudoLayer = {
568573
[fileName: string]: PseudoFile | PseudoSymbolicLink;
569574
};

0 commit comments

Comments
 (0)