Hassan Syyid
Hassan Syyid

Reputation: 1581

next.config.js rewrites not working on S3 but work locally

I am having some weird behavior with my rewrites on S3 vs hosting locally.

My next.config.js

module.exports = withImages({
  async rewrites() {
    return [
      {
        source: '/dashboard/:any*',
        destination: '/admin/app',
      },
      {
        source: '/env/:any*',
        destination: '/admin/app',
      },
      {
        source: '/app/:any*',
        destination: '/admin/app',
      }
    ]
  }
});

I merely uploaded the out/ folder to S3. My bucket static hosting config:

S3 bucket config

Locally, when I run next build and next start I see the redirects working as expected. However, when deployed to S3 only the /dashboard/:any* redirect seems to work. Any idea why that would happen?

Thanks in advance!

Upvotes: 0

Views: 1439

Answers (1)

Anthony Garcia-Labiad
Anthony Garcia-Labiad

Reputation: 3711

A S3 bucket is only suitable to host a static website, however you are using next start which starts a local server that can resolve dynamic routes.

Instead you need to run next export after next build, then use a tool like serve to statically serve your out folder:

serve out

This will be very close to what S3 is doing, and it will be easier to test that your webapp works before the deployment.

I am not familiar with this withImages function, so I don't know whether this is supposed to work out of the box with next export or not. If not you might have to play with exportPathMap - have a look to next documentation for static export.

Upvotes: 2

Related Questions