serverless-nextjs / serverless-next.js

⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
MIT License
4.46k stars 457 forks source link

Rewrites object format not supported #1003

Open myrjola opened 3 years ago

myrjola commented 3 years ago

Describe the bug

The rewrites configuration also supports an object, but this does not currently work in the default Lambda. https://nextjs.org/docs/api-reference/next.config.js/rewrites

Rewrites are applied after checking the filesystem (pages and /public files) and before dynamic routes by default. This behavior can be changed by instead returning an object instead of an array from the rewrites function

Actual behavior

The default Lambda crashes with the following:

{
  "errorType": "TypeError",
  "errorMessage": "rewrites is not iterable",
  "trace": [
    "TypeError: rewrites is not iterable",
    "    at getRewritePath (/var/task/index.js:3461:27)",
    "    at handleOriginRequest (/var/task/index.js:8867:31)",
    "    at Runtime.handler$1 [as handler] (/var/task/index.js:8774:26)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Expected behavior

The rewrites should apply without any crash.

Steps to reproduce

Add rewrites with the object format to next.config.js:

module.exports = {
  async rewrites() {
    return {
      beforeFiles: [{
        source: '/fi/varaus',
        destination: '/fi/reservation',
        locale: false
      }]
    }
  },
}

When navigating to /fi/varaus, you will get the "rewrites is not iterable" error instead of navigating to the /pages/reservation.tsx page.

Screenshots/Code/Logs

The crash looks like this from the user's perspective:

crash image

Versions

dphang commented 3 years ago

Yes it seems Next.js might have recently added rewrites object format? I didn't recall it when I first implemented rewrites. So it would need to be implemented. I'm working on some other stuff at the moment but can tag this as good first issue / enhancement in case someone wants to pick it up.

seifeddinebesbes commented 3 years ago

Hello, I would like to contribute and can i start by taking this task ?

dphang commented 3 years ago

@seifeddinebesbes Yes! I think no one else is working on it at the moment. Definitely let us know if you need help to get started or you can join the slack as well (email us at serverlessnextjs@gmail.com)

seifeddinebesbes commented 3 years ago

@dphang thank you and i already sent you an email :) :)

Best regards, Seif

zburk commented 3 years ago

Having the same issue. Is there any workaround currently or is downgrading to Next 9 the best option til this is fixed?

seifeddinebesbes commented 3 years ago

@zburk you can use the old rewrites configuration without downgrading to Next9 for example : image

zburk commented 3 years ago

Are there any logs created for rewrites? I tried setting it up and keep getting a 404 error. If I deploy the exact same code to Vercel, it works fine.

MarceloAzeved0 commented 2 years ago

Hello, i have the same error, have any updates on this issue?

danwestonwunderman commented 1 year ago

This is is still happening. Any news?

My scenario is trying to create a dynamic robots

async rewrites() { return { beforeFiles:[{ source: '/robots.txt', destination: '/api/robots', locale: false }] }; },