next.js: Cant handle static assets with serverless framework

Bug report

Describe the bug

I cannot load the assets with a lambda with serverless framework.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to https://github.com/romainquellec/boilerplate, clone
  2. yarn
  3. yarn (global) add serverless
  4. yarn build
  5. yarn lambda
  6. See error on console or chrome console (assets don’t load)

System information

  • OS: Windows
  • Version of Next.js: 8.0.1

Additional context

In lambda function, i’m doing :

app.use("/_next/static", express.static("../static"));
app.use("/static", express.static("../static"));

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (12 by maintainers)

Most upvoted comments

Static assets should not be part of the lambdas basically. So indeed use s3 or another static file host.

Changing app.use("/_next/static", express.static(path.join(__dirname, "/static"))); to app.use("/_next", express.static(path.join(__dirname, ".next"))); fixed it.

in next.config.js, add:

assetPrefix: './' 

then in you next app’s root html:

src="/_next/…

will become to :

src="./_next/…

then in your nginx conf:

location ^~/yourpath {
  proxy_pass http://localhost:yournextappport/;
}

I’m closing this. Will try to make an example out of it later.