Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.
Closed
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
26 changes: 18 additions & 8 deletions packages/serverless-nextjs-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,19 @@ AWS_ACCESS_KEY_ID=accesskey
AWS_SECRET_ACCESS_KEY=sshhh
```

Set next.js build target to `serverless`:
```js
// next.config.js

module.exports = {
target: 'serverless'
}
```

And simply deploy:

```bash
$ serverless
$ next-build && serverless
```

### Custom domain name
Expand Down Expand Up @@ -170,13 +179,14 @@ The fourth cache behaviour handles next API requests `api/*`.

### Inputs

| Name | Type | Default Value | Description |
| ------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| domain | `Array` | `null` | For example `['admin', 'portal.com']`. |
| bucketName | `string` | `null` | Custom bucket name where static assets are stored. By default is autogenerated. |
| nextConfigDir | `string` | `.` | Directory where your application `next.config.js` file is. This input is useful when the `serverless.yml` is not in the same directory as the next app. |
| build | `boolean` | `true` | When true builds and deploys app, when false assume the app has been built and uses the .next .serverless_nextjs directories in nextConfigDir to deploy |
| memory | `number` or `object` | `512` | When assigned a number, both the default and api lambdas will assigned memory of that value. When assigned to an object, values for the default and api lambdas can be separately defined |
| Name | Type | Default Value | Description |
| -------------- | -------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| domain | `Array` | `null` | For example `['admin', 'portal.com']`. |
| bucketName | `string` | `null` | Custom bucket name where static assets are stored. By default is autogenerated. |
| nextConfigDir | `string` | `./` | Directory where your application `next.config.js` file is. This input is useful when the `serverless.yml` is not in the same directory as the next app. <br>**Note:** `nextConfigDir` should be set if `next.config.js` `distDir` is used. |
| build | `boolean` | `true` | When true builds and deploys app, when false assume the app has been built and uses the .next .serverless_nextjs directories in nextConfigDir to deploy | |
| staticDir | `string` | `./` | If your `static` or `public` directory is not a direct child of `nextConfigDir` this is needed. |
| memory | `number` or `object` | `512` | When assigned a number, both the default and api lambdas will assigned memory of that value. When assigned to an object, values for the default and api lambdas can be separately defined |

Custom inputs can be configured like this:

Expand Down
14 changes: 6 additions & 8 deletions packages/serverless-nextjs-component/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ class NextjsComponent extends Component {
? path.resolve(inputs.nextConfigDir)
: process.cwd();

await execa("node_modules/.bin/next", ["build"], {
cwd: nextConfigPath
});

await this.emptyBuildDirectory(nextConfigPath);

const {
Expand All @@ -263,6 +259,8 @@ class NextjsComponent extends Component {
const nextConfigPath = inputs.nextConfigDir
? path.resolve(inputs.nextConfigDir)
: process.cwd();
const nextStaticPath = inputs.nextStaticDir || "";
const staticPath = nextStaticPath || nextConfigPath;

const [defaultBuildManifest, apiBuildManifest] = await Promise.all([
this.readDefaultBuildManifest(nextConfigPath),
Expand Down Expand Up @@ -312,14 +310,14 @@ class NextjsComponent extends Component {
];

const [publicDirExists, staticDirExists] = await Promise.all([
fse.exists(join(nextConfigPath, "public")),
fse.exists(join(nextConfigPath, "static"))
fse.exists(join(staticPath, "public")),
fse.exists(join(staticPath, "static"))
]);

if (publicDirExists) {
assetsUpload.push(
bucket.upload({
dir: join(nextConfigPath, "public"),
dir: join(staticPath, "public"),
keyPrefix: "public"
})
);
Expand All @@ -328,7 +326,7 @@ class NextjsComponent extends Component {
if (staticDirExists) {
assetsUpload.push(
bucket.upload({
dir: join(nextConfigPath, "static"),
dir: join(staticPath, "static"),
keyPrefix: "static"
})
);
Expand Down