Skip to content

Middleware size is affected by messages in Next.js 15 #1669

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

Open
3 tasks done
polly3d opened this issue Jan 18, 2025 · 6 comments
Open
3 tasks done

Middleware size is affected by messages in Next.js 15 #1669

polly3d opened this issue Jan 18, 2025 · 6 comments
Labels
bug Something isn't working upstream-issue This issue is caused by an upstream dependency (e.g. Next.js)

Comments

@polly3d
Copy link

polly3d commented Jan 18, 2025

Description

I got an error when I deploy to Vercel. The error is: The Edge Function "src/middleware" size is 1.65 MB and your plan size limit is 1 MB. All translations are packaged into a middleware file.

I know, my translation file was big. Is there any solution for this?

Here's the screenshot of the translation files:

Image

Here's the configuration in i18n folder:

// request.ts
import { getRequestConfig } from "next-intl/server";
import { routing } from "./routing";

export default getRequestConfig(async ({ requestLocale }) => {
  // This typically corresponds to the `[locale]` segment
  let locale = await requestLocale;

  // Ensure that a valid locale is used
  if (!locale || !routing.locales.includes(locale as any)) {
    locale = routing.defaultLocale;
  }

  return {
    locale,
    messages: (await import(`../locales/${locale}.json`)).default,
  };
});
// routing.ts
import { defineRouting } from "next-intl/routing";
import { createNavigation } from "next-intl/navigation";

export const routing = defineRouting({
  // A list of all locales that are supported
  locales: ["en", "es"],
  localePrefix: "as-needed",
  // Used when no locale matches
  defaultLocale: "en",
});

// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const { Link, redirect, usePathname, useRouter, getPathname } =
  createNavigation(routing);

The middleware:

import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";

export default createMiddleware(routing);

export const config = {
  matcher: [
    // Skip Next.js internals and all static files, unless found in search params
    "/((?!_next|monitoring|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest|xml|txt)).*)",
    // Always run for API routes
    "/(api|trpc)(.*)",
  ],
};

Verifications

Mandatory reproduction URL

None

Reproduction description

None

Expected behaviour

Reduce middleware file size

@polly3d polly3d added bug Something isn't working unconfirmed Needs triage. labels Jan 18, 2025
Copy link

Thank you for your report!

Unfortunately, the reproduction is missing or incomplete, and as such we cannot investigate this issue. Please add a reproduction to the issue, otherwise it will be closed automatically.

Templates:

Creating a good reproduction takes time.

To help us resolve the issue quickly, please simplify the reproduction as much as possible by removing any unnecessary code, files, and dependencies that are not directly related to the problem. The easier it is for us to see the issue, the faster we can help you.

Apart from the reproduction, make sure to include the precise steps on how to reproduce the issue, e.g.:

  1. Open reproduction
  2. Click on …
  3. See error: …

Thank you for your understanding!

FAQ

"I've included a reproduction, what is missing?"

This comment might have been added because your reproduction doesn't point to a destination where the issue can be reproduced. Please make sure that the latest changes were saved in the reproduction and that the link is correct.

@polly3d
Copy link
Author

polly3d commented Jan 21, 2025

I'm sorry.

Here's the reproduction link:

https://github.com/polly3d/next-intl-json

Hope it's helpful. And let me know if anything else you need.

@amannn
Copy link
Owner

amannn commented Jan 21, 2025

Thanks for the reproduction!

It seems like Next.js 15 has changed something with the bundling behavior, I need to dig a bit deeper and potentially create a bug report upstream. I've confirmed that with Next.js 14 this is not the case.

As a quick fix, you can split this out from your routing.ts:

// i18n/navigation.ts

import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';

export const {Link, redirect, usePathname, useRouter, getPathname} =
  createNavigation(routing);

With this, your middleware size will not be affected anymore.

@amannn amannn changed the title Error: The Edge Function "src/middleware" size is 1.65 MB and your plan size limit is 1 MB Middleware size is affected by messages since Next.js 15 Jan 21, 2025
@amannn amannn changed the title Middleware size is affected by messages since Next.js 15 Middleware size is affected by messages in Next.js 15 Jan 21, 2025
@amannn
Copy link
Owner

amannn commented Jan 22, 2025

I've talked to the Next.js team via DM and they confirmed suspicious behavior in Next.js here. I've added an upstream report here: vercel/next.js#75180.

As a workaround for the time being, please consider #1669 (comment).

@amannn amannn added the upstream-issue This issue is caused by an upstream dependency (e.g. Next.js) label Jan 22, 2025
@polly3d
Copy link
Author

polly3d commented Jan 22, 2025

Thank you so much for your quick help!

It's fixed.

@polly3d polly3d closed this as completed Jan 22, 2025
@amannn
Copy link
Owner

amannn commented Jan 22, 2025

Sure, you're welcome! I'll leave the issue open as I'm currently waiting for the Next.js team to hopefully address this upstream.

@amannn amannn reopened this Jan 22, 2025
amannn added a commit that referenced this issue Feb 27, 2025
Fixes #1587

Note that #1669 is still
relevant, therefore you might want to use a
[workaround](#1669 (comment))
for the time being.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream-issue This issue is caused by an upstream dependency (e.g. Next.js)
Projects
None yet
2 participants