-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
[NEXT-1156] Using an external package that imports next/server
incorrectly bundles @vercel/og
#48469
Comments
Hey, I am experiencing the same problem too. I can't deploy to vercel because edge function size is bigger than 1mb and getting the same errors like you. Did you find any workarounds or solutions for this? |
@av-erencelik The only workaround I found is patching next.js/packages/next-swc/crates/core/src/lib.rs Lines 162 to 169 in 8050a6c
One example of the patch for the --- a/dist/server/withClerkMiddleware.js
+++ b/dist/server/withClerkMiddleware.js
@@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleMiddlewareResult = exports.withClerkMiddleware = exports.decorateResponseWithObservabilityHeaders = void 0;
const backend_1 = require("@clerk/backend");
-const server_1 = require("next/server");
+const NextResponse = require("next/dist/server/web/exports/next-response").default;
const constants_1 = require("../constants");
const clerk_1 = require("./clerk");
const errors_1 = require("./errors");
@@ -93,7 +93,7 @@ exports.withClerkMiddleware = withClerkMiddleware;
function handleMiddlewareResult({ req, res, authStatus, authMessage, authReason, }) {
// pass-through case, convert to next()
if (!res) {
- res = server_1.NextResponse.next();
+ res = NextResponse.next();
}
// redirect() case, return early
if (res.headers.get(constants_1.constants.Headers.NextRedirect)) { Be aware that patching packages like this is not really a good practice. |
@solaldunckel thanks for very detailed answer. I tried your solution and it works. I posted this issue on Clerk's discord and they suggested to downgrading to earlier version of Next.js until Vercel fix it. They said they were in contact with the next team and reported the problem. |
next/server
incorrectly bundles @vercel/og
next/server
incorrectly bundles @vercel/og
The |
### What? Implement a CJS optimizer for next-swc ### Why? x-ref: https://vercel.slack.com/archives/C02HY34AKME/p1684341093462309 Assuming most CJS files are transpiled from ESM, we can mimic tree-shaking using an AST transform. ### How? Closes WEB-1072 Fixes #48469 fix NEXT-1156 --------- Co-authored-by: Shu Ding <g@shud.in>
This problem should be fixed with the next canary release, thanks for bringing it up! |
### What? Implement a CJS optimizer for next-swc ### Why? x-ref: https://vercel.slack.com/archives/C02HY34AKME/p1684341093462309 Assuming most CJS files are transpiled from ESM, we can mimic tree-shaking using an AST transform. ### How? Closes WEB-1072 Fixes vercel#48469 fix NEXT-1156 --------- Co-authored-by: Shu Ding <g@shud.in>
### What? This reverts commit 6ebc725 / vercel#50247. ### Why? vercel#49972 is reverted due to bugs, and I'm retrying it. ### How? Closes WEB-1072 Closes WEB-1097 Closes NEXT-1156 (as it's reopened by the revert PR) fix vercel#48469 --------- Co-authored-by: Shu Ding <g@shud.in>
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. |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.3.0: Thu Jan 5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000 Binaries: Node: 18.15.0 npm: 9.6.3 Yarn: 1.22.19 pnpm: 8.2.0 Relevant packages: next: 13.3.1-canary.8 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Middleware / Edge (API routes, runtime)
Link to the code that reproduces this issue
https://github.com/solaldunckel/next-13-bundle-size-issue
To Reproduce
NextResponse
orNextRequest
(e.g@clerk/nextjs
)@vercel/og
gets bundled in the route handlerDescribe the Bug
After updating to Next.js version 13.3, I noticed a significant increase in the size of my edge functions on Vercel. Upon investigation, I found that a new
ImageResponse
export innext/server
is now included by default.One of the packages I use,
@clerk/nextjs
, importsNextResponse
which causes the@vercel/og
package to be bundled into the function.You can see in those screenshots the bundle size increase when importing the external package
@clerk/nextjs
An error message also appears when running
next build
, which correspond to the size of@vercel/og/index.edge.js
The only way I found to fix the issue was to patch the external package to import
NextResponse
andNextRequest
directly fromnext/dist/server/web/exports/
Expected Behavior
Importing from
next/server
in an external package shouldn't cause@vercel/og
to be bundled.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
NEXT-1156
The text was updated successfully, but these errors were encountered: