# @sls-next/lambda-at-edge

> Provides handlers that can be used in CloudFront Lambda@Edge to deploy next.js applications to the edge

Latest version **3.7.0** (published 2022-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sls-next/lambda-at-edge
pnpm add @sls-next/lambda-at-edge
yarn add @sls-next/lambda-at-edge
bun add @sls-next/lambda-at-edge
```

Provides the command `build-lambda-at-edge`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; large bundle.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.7.0 |
| Published | 2022-03-31 |
| First published | 2020-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 11.4 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4459 |
| Author | Daniel Conde Marin |
| Maintainers | danielconde, danielphang |
| Keywords | AWS, Lambda@Edge, Next.js |

## Links

- npm: https://www.npmjs.com/package/@sls-next/lambda-at-edge
- Repository: https://github.com/serverless-nextjs/serverless-next.js
- Homepage: https://github.com/serverless-nextjs/serverless-next.js#readme
- Issues: https://github.com/serverless-nextjs/serverless-next.js/issues
- npm.io page: https://npm.io/package/@sls-next/lambda-at-edge

## Dependencies (10)

- [execa](https://npm.io/package/execa.md) 5.1.1
- [fs-extra](https://npm.io/package/fs-extra.md) 9.1.0
- [get-stream](https://npm.io/package/get-stream.md) 6.0.1
- [node-fetch](https://npm.io/package/node-fetch.md) 2.6.5
- [@vercel/nft](https://npm.io/package/@vercel/nft.md) 0.17.5
- [@sls-next/core](https://npm.io/package/@sls-next/core.md) 3.7.0
- [normalize-path](https://npm.io/package/normalize-path.md) 3.0.0
- [@aws-sdk/client-s3](https://npm.io/package/@aws-sdk/client-s3.md) 3.54.0
- [@aws-sdk/client-sqs](https://npm.io/package/@aws-sdk/client-sqs.md) 3.54.0
- [@sls-next/aws-common](https://npm.io/package/@sls-next/aws-common.md) 3.7.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.7.0 (latest) — 2022-03-31
- 3.8.0-alpha.0 (alpha) — 2022-04-20
- 3.7.0-alpha.12 — 2022-03-15
- 3.7.0-alpha.11 — 2022-03-10
- 3.7.0-alpha.10 — 2022-02-23
- 3.7.0-alpha.9 — 2022-02-18
- 3.7.0-alpha.8 — 2022-02-02
- 3.7.0-alpha.7 — 2022-01-27
- 3.7.0-alpha.6 — 2022-01-25
- 3.7.0-alpha.5 — 2022-01-20
- 3.7.0-alpha.4 — 2022-01-05
- 3.7.0-alpha.3 — 2021-12-22
- 3.7.0-alpha.2 — 2021-11-29
- 3.7.0-alpha.1 — 2021-11-21
- 3.7.0-alpha.0 — 2021-11-21
- … 222 more at https://npm.io/package/@sls-next/lambda-at-edge/versions

## README

# @sls-next/lambda-at-edge
> Library to build and deploy Next.js apps for AWS Lambda@Edge

This library uses the handlers provided by `@sls-next/core` and wraps them with a Lambda@Edge/CloudFront-compatible layer.

## Usage

```ts
const path = require('path');
const { Builder } = require("@sls-next/lambda-at-edge");

const nextConfigPath = '/path/to/my/nextapp';
const outputDir = path.join(nextConfigPath, ".serverless_nextjs");

const builder = new Builder(
  nextConfigPath,
  outputDir,
  {
    cmd: './node_modules/.bin/next',
    cwd: process.cwd(),
    env: {},
    args: ['build'],
    minifyHandlers: true,
    // it is recommended to let your CF distribution do the compression as per the docs - https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
    // however there have been issues in the past where CF doesn't compress lambda@edge responses, so we provide our own implementation in case is needed
    enableHTTPCompression: false
  }
);

await builder.build()
    .then(() => {
      console.log("Application built successfully!");
    })
    .catch((e) => {
      console.log("Could not build app due the exception: ", e);
      process.exit(1);
    });
```

You can configure more options regarding building process. Configurable inputs you can find in 'build.ts' file ('packages/libs/lambda-at-edge/src/build.ts'). If you want to see debug logs during building, use 'await builder.build(true)' instead.
After running the above, the output directory will contain the Lambda@Edge handlers necessary to server side render at the edge.

```
/dir/to/my/next-app/.serverless_nextjs/

 > default-lambda
   > manifest.json
   > routes-manifest.json
   > prerender-manifest.json
   > pages/
   > index.js # handler

 > api-lambda
   > manifest.json
   > routes-manifest.json
   > pages/api/
   > index.js # handler

 > image-lambda
   > manifest.json
   > routes-manifest.json
   > images-manifest.json
   > node_modules/...
   > index.js # handler
```

The handlers need to be attached to the `origin-request` trigger of CloudFront.
The `api-lambda` edge function should be attached to a CloudFront behaviour that only triggers in the event of `/api/*` requests.
The `image-lambda` edge function should be attached to a CloudFront behaviour that only triggers in the event of `_next/image*` requests.

For full usage docs, please refer to (TBA).

## Architecture
Once built and packaged, the app consists of the following components:

* `default-lambda` (v2): handles page and API requests.
* `default-lambda` (v1): handles page requests only.
* `api-lambda` (legacy v1 handlers only): handles API requests.
* `regeneration-lambda`: handles regeneration requests used for ISR.
* `image-lambda`: handles image optimization requests.
* `assets`: all static assets used by your app.

## Infrastructure
You will need the following infrastructure to deploy your app:

* AWS Lambda@Edge
* AWS CloudFront
* AWS API Gateway
* AWS S3 Bucket
* AWS SQS Queue (if you are using ISR)
* additional roles, permissions, etc.

## Deployment

Currently, you will need to deploy via the Serverless Components deployer, `@sls-next/serverless-component`. We also provide a CDK construct at `@sls-next/nextjs-cdk-construct`.

If you'd like to write your own custom deployment logic, please see the CDK construct or legacy Serverless Components deployer to see an example of all the infrastructure you need to setup.

## Limitations

* Lambda@Edge limitations apply: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-functions-restrictions.html. Most notably, there is a 1 MB response size limit, which could especially affect static files.
* CloudFront limitations apply: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html
* The image handler only serves image optimization requests. It cannot redirect, rewrite or add headers (yet).
* In v1 handlers (legacy), the default, API and image handlers are separate, so you cannot rewrite from default routes -> image routes nor rewrite between default routes and API routes.
* In v2 handlers, the default and image handlers are separate, so you cannot rewrite from default routes -> image routes.

## Acknowledgements

Special thanks for Daniel Conde Marin for the initial implementation.

---
_Source: https://npm.io/package/@sls-next/lambda-at-edge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
