0

We're using the Next.js Serverless Component from: https://serverless-nextjs.com/ to deploy our Next.js app to AWS using the Serverless framework.

It takes around 7-8 mins for the app to be built and then deployed to AWS.

We were hoping to instead build the app up-front and then deploy the pre-built code...

You can tell Serverless to not build and just do the deployment with build.enabled: false:

# serverless.yml

myNextApplication:
  component: "@sls-next/serverless-component@3.7.0"
  inputs:
    bucketName: our-bucket-name
    build:
      enabled: false

However when we run yarn next build the app is built under ./next.

When serverless builds the app it stores the state in .stateless and the files in .stateless_nextjs. So therefore the next build isn't compatible with deploying a prebuilt app...

This means if we try and build the app and then call serverless after we get the following error:

myNextApplication › Error: ENOENT: no such file or directory, open '/Users/cameron/Projects/nextjs-serverless/.serverless_nextjs/default-lambda/manifest.json'

So how do you build the app first and then deploy the build using Next.js Serverless Component?

Versions:

"dependencies": {
  "next": "12.2.2",
  "react": "18.2.0",
  "react-dom": "18.2.0",
  "serverless": "2.72.2"
},

component: "@sls-next/serverless-component@3.7.0"
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • What's your package versions? and that of nextjs? – sungryeol Jul 27 '22 at 03:20
  • [Custom build directory](https://nextjs.org/docs/api-reference/next.config.js/setting-a-custom-build-directory) in NextJS? Or missing a [nextConfigDir](https://github.com/serverless-nextjs/serverless-next.js#inputs) in serverless config? – David Rubin Jul 27 '22 at 07:58

1 Answers1

0

The reason is probably wrong next.js middleware setup. manifest.json is built upon simulated requests that goes through nextjs middlewares. please refer to this stackoverflow answer.

nextjs middleware had some structural changes around v12.x that requires migration. it's hard to figure out what's wrong because it gives little or no warning messages and it builds okay. I had similar issues too.

Solution

  • check if your deploy includes proper package lock file(yarn.lock, package-lock.json...) so that fixed version of next.js is included when deploying.
  • check latest middleware examples from github repo and try match middleware file structure and code.
  • check out nextjs middleware migration guide mentioned earlier; try build in local environment and make sure manifest.json is created.
sungryeol
  • 3,677
  • 7
  • 20
  • The reason the `manifest.json` file isn't present is because Next.js builds to `.next` and the Next.js Serverless Component builds to `.serverless_nextjs` so the build command the the component are not compatible with one another... or so it seems... so it's not clear how you'd build up-front... – Cameron Jul 25 '22 at 20:20
  • @Cameron sorry I had not catched that by the time I was writing it. I'll fix the answer – sungryeol Jul 26 '22 at 02:32