Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrites.beforeFiles should work the same with or without i18n configuration. #34371

Closed
LeonardDrs opened this issue Feb 15, 2022 · 6 comments
Closed
Labels
bug Issue was opened via the bug report template. Internationalization (i18n) Related to Internationalization with Next.js.

Comments

@LeonardDrs
Copy link

Run next info (available from version 12.0.8 and up)

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT Tue, 01 Feb 2022 21:42:50 +0000
Binaries:
  Node: 17.3.0
  npm: 8.4.1
  Yarn: 1.22.17
  pnpm: N/A
Relevant packages:
  next: 12.0.11-canary.15
  react: 17.0.2
  react-dom: 17.0.2

What version of Next.js are you using?

12.0.11-canary.15

What version of Node.js are you using?

17.3.0

What browser are you using?

Firefox

What operating system are you using?

Linux

How are you deploying your application?

next start

Describe the Bug

Rewrites with beforeFiles does not work as it used to be with i18n enabled. (last known to be working in next@11.1.2)

Back in next@11.1.2 I used to have this rewrites configuration:

  i18n,
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: "/base/:path*",
          destination: "/:path*",
        },
      ],
    };
  },

and my urls such as /base/_next/static/chunks/main.js used to work. (Use case: CDN shenanigans, CDN are configured to rewrites assets.xxx.com/foo/* through foo.xxx.com/assets/* for micro frontends.)

Upon upgrading to next@12.0.10, this behavior does not work anymore.

This bug does not occurs without i18n configured.

I'm providing a PR with tests to confirm the broken behavior, although I've simplified the use case by using an image and not compiled file. Should I add a check for compiled files also?

Related: #23270
History: #23455

Expected Behavior

rewrites.beforeFiles should work the same with or without i18n configuration.

To Reproduce

#34370

@LeonardDrs LeonardDrs added the bug Issue was opened via the bug report template. label Feb 15, 2022
@balazsorban44 balazsorban44 added the Internationalization (i18n) Related to Internationalization with Next.js. label Feb 16, 2022
@kristiankalb
Copy link

We are experiencing the same issue.
Our rewrites look like this:

 async rewrites() {
    return {
      beforeFiles: [
        {
             source: `${assetPrefix}/:path*`,
             destination: '/:path*',
        },
      ],
    };
  },

next@12.0.4 seems to introduce it. In versions before that the rewrites.beforeFiles above works fine both in projects with and without i18n. But from 12.0.4 on they only work in our projects without the i18n.

@4zuko
Copy link

4zuko commented Apr 15, 2022

I am facing the same issue that can be reproduced in the following sandbox:

But, it's interesting that I am not able to reproduce the issue in NextJS monorepo running any of these two commands (dev and prod mode):

next dev ./examples/basic-css/

or

next build ./examples/basic-css/ && next start ./examples/basic-css/

Note: I've added next.config.js configuration into basic-css - the same one from the sandbox.


What version of Next.js are you using?
next -v
Next.js v12.1.5

What version of Node.js are you using?
node -v
v14.18.2

What browser are you using?
Chrome v100.0.4896.88 (Official Build) (x86_64)

What operating system are you using?
macOS Monterey

How are you deploying your application?

next dev
next build && next start

@indrajithc
Copy link

indrajithc commented Jun 5, 2022

I am facing the same issue, needs to be fixed ASAP

for the time being found a workaround using _middleware

import { NextResponse } from "next/server";

const assetPrefixRegEx = new RegExp(`^${assetPrefix}`);

export default function middleware(request) {
  const response = NextResponse.next();
  if (assetPrefixRegEx.test(request.nextUrl.pathname)) {
    request.nextUrl.pathname = request.nextUrl.pathname.replace(assetPrefixRegEx, "");
    return NextResponse.rewrite(request.nextUrl);
  }
  return response;
}

@hanneslund
Copy link
Contributor

This should work by doing something like this 👍

{
  source: '/:locale/base/:path*', // ignore locale
  destination: '/:path*',
  locale: false,
}

@ijjk
Copy link
Member

ijjk commented Jun 13, 2022

Closing as the above has been documented in #37581 and we also added regression tests for this.

@ijjk ijjk closed this as completed Jun 13, 2022
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Internationalization (i18n) Related to Internationalization with Next.js.
Projects
None yet
Development

No branches or pull requests

7 participants