Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next 12.2 middleware support - remove target config (#2477) #2478

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ The fourth cache behaviour handles next API requests `api/*`.
| domainType |`string` |`"both"` | Can be one of:`"apex"`- apex domain only, don't create a www subdomain.`"www"` - www domain only, don't create an apex subdomain.`"both"`- create both www and apex domains when either one is provided. |
| domainMinimumProtocolVersion |`string` |`"TLSv1.2_2018"` | Can be one of: `"SSLv3", "TLSv1", "TLSv1.1_2016", "TLSv1.2_2018", "TLSv1.2_2019", "TLSv1.2_2021" or "TLSv1_2016"`. See [reference](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-viewercertificate.html). |
| publicDirectoryCache |`boolean\|object`|`true` | Customize the`public`/`static`folder asset caching policy. Assigning an object with`value`and/or`test`lets you customize the caching policy and the types of files being cached. Assigning false disables caching |
| useServerlessTraceTarget |`boolean` |`false` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. Note: while using this, you may need to set`NODE_ENV`variable to`production`. | | logLambdaExecutionTimes | `boolean` |`false` | Logs to CloudWatch the default handler performance metrics. |
| useServerlessTraceTarget |`boolean` |`true` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. Note: while using this, you may need to set`NODE_ENV`variable to`production`. | | logLambdaExecutionTimes | `boolean` |`false` | Logs to CloudWatch the default handler performance metrics. |
| minifyHandlers |`boolean` |`false` | Use pre-built minified handlers to reduce code size. Does not minify custom handlers. | | deploy |`boolean` |`true` | Whether to deploy resources to AWS (available in the latest alpha). Useful if you just need the build outputs (Lambdas and assets) but want to deploy them yourself. Build outputs will be created in the`.serverless_nextjs`directory. You are then responsible to configure AWS yourself: setting CloudFront behaviors with Lambda function associations, uploading assets to S3 with the proper`Cache-Control`headers, etc. |
| enableHTTPCompression |`boolean` |`false` | When set to`true`the Lambda@Edge functions for SSR and API requests will use Gzip to compress the response. Note that you shouldn't need to enable this because CloudFront will compress responses for you out of the box. |
| authentication |`object` |`undefined` | Authentication object for use with basic authentication (available from 1.19.0-alpha.3). It only supports a single username/password combination for now and is inlined in plaintext in the Lambda handler. You must also forward the`Authorization`header for CloudFront behaviors, e.g`defaults`, `api/*`, and `\_next/data/\_`. **Note: this is meant as a simple means of protecting an environment such as a development/test site, it is not recommended for production use.** |
Expand Down
2 changes: 1 addition & 1 deletion documentation/docs/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sidebar_label: Inputs
| cloudfront | `object` | `{}` | Inputs to be passed to [aws-cloudfront](https://github.com/serverless-components/aws-cloudfront) |
| domainType | `string` | `"both"` | Can be one of: `"apex"` - apex domain only, don't create a www subdomain. `"www"` - www domain only, don't create an apex subdomain.`"both"` - create both www and apex domains when either one is provided. |
| publicDirectoryCache | `boolean\|object` | `true` | Customize the `public`/`static` folder asset caching policy. Assigning an object with `value` and/or `test` lets you customize the caching policy and the types of files being cached. Assigning false disables caching |
| useServerlessTraceTarget | `boolean` | `false` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. |
| useServerlessTraceTarget | `boolean` | `true` | Use the experimental-serverless-trace target to build your next app. This is the same build target that Vercel Now uses. See this [RFC](https://github.com/vercel/next.js/pull/8246) for details. |
| verbose | `boolean` | `false` | Print verbose output to the console. |

Custom inputs can be configured like this:
Expand Down
49 changes: 24 additions & 25 deletions packages/libs/core/src/build/builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fse from "fs-extra";
import fse, { existsSync } from "fs-extra";
import { join } from "path";
import path from "path";
import {
Expand All @@ -12,7 +12,6 @@ import readDirectoryFiles from "./lib/readDirectoryFiles";
import filterOutDirectories from "./lib/filterOutDirectories";
import { NextConfig } from "./types";
import normalizePath from "normalize-path";
import createServerlessConfig from "./lib/createServerlessConfig";
import execa from "execa";
import { prepareBuildManifests } from "./";
import pathToPosix from "./lib/pathToPosix";
Expand All @@ -36,6 +35,16 @@ const defaultBuildOptions = {
assetIgnorePatterns: []
};

export function getNextConfigPath(nextConfigDir: string): string {
let nextConfigPath = path.join(nextConfigDir, "next.config.js");

// fails to import ESM config
// if (existsSync(nextConfigPath.replace(".js", ".mjs")))
// nextConfigPath = nextConfigPath.replace(".js", ".mjs");

return nextConfigPath;
}

/**
* Core builder class that has common build functions for all platforms.
*/
Expand All @@ -57,7 +66,7 @@ export default abstract class CoreBuilder {
this.buildOptions.nextStaticDir ?? this.buildOptions.nextConfigDir
);
this.dotNextDir = path.join(this.nextConfigDir, ".next");
this.serverlessDir = path.join(this.dotNextDir, "serverless");
this.serverlessDir = path.join(this.dotNextDir, "server");
this.outputDir = this.buildOptions.outputDir;
}

Expand Down Expand Up @@ -103,28 +112,18 @@ export default abstract class CoreBuilder {
this.buildOptions
);

const { restoreUserConfig } = await createServerlessConfig(
const subprocess = execa(cmd, args, {
cwd,
path.join(this.nextConfigDir),
false
);

try {
const subprocess = execa(cmd, args, {
cwd,
env
});

if (debugMode) {
// @ts-ignore
subprocess.stdout.pipe(process.stdout);
}
env
});

await subprocess;
} finally {
await restoreUserConfig();
if (debugMode) {
// @ts-ignore
subprocess.stdout.pipe(process.stdout);
}

await subprocess;

// eslint-disable-next-line @typescript-eslint/no-var-requires
const routesManifest = require(join(
this.dotNextDir,
Expand Down Expand Up @@ -200,7 +199,7 @@ export default abstract class CoreBuilder {
}

/**
* Check whether this .next/serverless/pages file is a JS file used for runtime rendering.
* Check whether this .next/server/pages file is a JS file used for runtime rendering.
* @param pageManifest
* @param relativePageFile
*/
Expand Down Expand Up @@ -311,7 +310,7 @@ export default abstract class CoreBuilder {
}

protected async readNextConfig(): Promise<NextConfig | undefined> {
const nextConfigPath = path.join(this.nextConfigDir, "next.config.js");
const nextConfigPath = getNextConfigPath(this.nextConfigDir);

if (await fse.pathExists(nextConfigPath)) {
const nextConfig = await require(nextConfigPath);
Expand Down Expand Up @@ -405,7 +404,7 @@ export default abstract class CoreBuilder {
});

const htmlAssets = [...htmlFiles, ...fallbackFiles].map((file) => {
const source = path.join(dotNextDirectory, `serverless/pages${file}`);
const source = path.join(dotNextDirectory, `server/pages${file}`);
const destination = path.join(
assetOutputDirectory,
withBasePath(`static-pages/${buildId}${file}`)
Expand All @@ -415,7 +414,7 @@ export default abstract class CoreBuilder {
});

const jsonAssets = jsonFiles.map((file) => {
const source = path.join(dotNextDirectory, `serverless/pages${file}`);
const source = path.join(dotNextDirectory, `server/pages${file}`);
const destination = path.join(
assetOutputDirectory,
withBasePath(`_next/data/${buildId}${file}`)
Expand Down
1 change: 1 addition & 0 deletions packages/libs/core/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { pathToRegexStr } from "./pathToRegexStr";
import { PrerenderManifest } from "next/dist/build";
import { getSortedRoutes } from "./sortedRoutes";
import { usedSSR } from "./ssr";
export { getNextConfigPath } from "./builder";

export const prepareBuildManifests = async (
buildOptions: BuildOptions,
Expand Down
91 changes: 0 additions & 91 deletions packages/libs/core/src/build/lib/createServerlessConfig.ts

This file was deleted.

45 changes: 16 additions & 29 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
} from "./types";
import pathToPosix from "@sls-next/core/dist/build/lib/pathToPosix";
import normalizeNodeModules from "@sls-next/core/dist/build/lib/normalizeNodeModules";
import createServerlessConfig from "@sls-next/core/dist/build/lib/createServerlessConfig";
import { isTrailingSlashRedirect } from "@sls-next/core/dist/build/lib/redirector";
import readDirectoryFiles from "@sls-next/core/dist/build/lib/readDirectoryFiles";
import filterOutDirectories from "@sls-next/core/dist/build/lib/filterOutDirectories";
import { Job } from "@vercel/nft/out/node-file-trace";
import { prepareBuildManifests } from "@sls-next/core";
import { getNextConfigPath, prepareBuildManifests } from "@sls-next/core";
import { NextConfig } from "@sls-next/core";
import { NextI18nextIntegration } from "@sls-next/core/dist/build/third-party/next-i18next";
import normalizePath from "normalize-path";
Expand Down Expand Up @@ -59,7 +58,7 @@ const defaultBuildOptions = {
cwd: process.cwd(),
env: {},
cmd: "./node_modules/.bin/next",
useServerlessTraceTarget: false,
useServerlessTraceTarget: true,
logLambdaExecutionTimes: false,
domainRedirects: {},
minifyHandlers: false,
Expand Down Expand Up @@ -91,7 +90,7 @@ class Builder {
this.nextConfigDir = path.resolve(nextConfigDir);
this.nextStaticDir = path.resolve(nextStaticDir ?? nextConfigDir);
this.dotNextDir = path.join(this.nextConfigDir, ".next");
this.serverlessDir = path.join(this.dotNextDir, "serverless");
this.serverlessDir = path.join(this.dotNextDir, "server");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this changed

this.outputDir = outputDir;
if (buildOptions) {
this.buildOptions = buildOptions;
Expand Down Expand Up @@ -120,9 +119,7 @@ class Builder {
const hasServerlessPageManifest = await fse.pathExists(path);

if (!hasServerlessPageManifest) {
return Promise.reject(
"pages-manifest not found. Check if `next.config.js` target is set to 'serverless'"
);
return Promise.reject("pages-manifest not found.");
}

return await fse.readJSON(path);
Expand Down Expand Up @@ -170,7 +167,7 @@ class Builder {
}

/**
* Check whether this .next/serverless/pages file is a JS file used for runtime rendering.
* Check whether this .next/server/pages file is a JS file used for runtime rendering.
* @param buildManifest
* @param relativePageFile
*/
Expand Down Expand Up @@ -543,7 +540,7 @@ class Builder {
}

async readNextConfig(): Promise<NextConfig | undefined> {
const nextConfigPath = path.join(this.nextConfigDir, "next.config.js");
const nextConfigPath = getNextConfigPath(this.nextConfigDir);

if (await fse.pathExists(nextConfigPath)) {
const nextConfig = await require(nextConfigPath);
Expand Down Expand Up @@ -637,7 +634,7 @@ class Builder {
});

const htmlAssets = [...htmlFiles, ...fallbackFiles].map((file) => {
const source = path.join(dotNextDirectory, `serverless/pages${file}`);
const source = path.join(dotNextDirectory, `server/pages${file}`);
const destination = path.join(
assetOutputDirectory,
withBasePath(`static-pages/${buildId}${file}`)
Expand All @@ -647,7 +644,7 @@ class Builder {
});

const jsonAssets = jsonFiles.map((file) => {
const source = path.join(dotNextDirectory, `serverless/pages${file}`);
const source = path.join(dotNextDirectory, `server/pages${file}`);
const destination = path.join(
assetOutputDirectory,
withBasePath(`_next/data/${buildId}${file}`)
Expand Down Expand Up @@ -743,28 +740,18 @@ class Builder {
fse.emptyDir(join(this.outputDir, ASSETS_DIR))
]);

const { restoreUserConfig } = await createServerlessConfig(
const subprocess = execa(cmd, args, {
cwd,
path.join(this.nextConfigDir),
useServerlessTraceTarget
);

try {
const subprocess = execa(cmd, args, {
cwd,
env
});

if (debugMode) {
// @ts-ignore
subprocess.stdout.pipe(process.stdout);
}
env
});

await subprocess;
} finally {
await restoreUserConfig();
if (debugMode) {
// @ts-ignore
subprocess.stdout.pipe(process.stdout);
}

await subprocess;

// eslint-disable-next-line @typescript-eslint/no-var-requires
const routesManifest = require(join(
this.dotNextDir,
Expand Down