Skip to content

vinext@1.3.0: resolve.alias of next/image to a wrapper that imports vinext/shims/image now resolves to itself (infinite render, request hangs, no build error) #4262

Closed

Description

@BetoFrega

Summary

Since #4055 ("fix(image): allow getImageProps in Server Components"), the plugin's resolveId re-resolves vinext/shims/image to next/image outside the rsc environment. If a project aliases next/image (via Vite resolve.alias) to its own "use client" wrapper, and that wrapper imports vinext/shims/image to render the stock component, the wrapper's import is resolved through the alias back to the wrapper. The SSR bundle then contains a component that renders itself:

function Image(props) {
  return jsx(Image, { ...props });
}

vite build succeeds with no warning. Any request that renders the page never finishes: the process sits at high CPU with growing memory (on Cloudflare Workers: "Worker exceeded memory limit" / CPU time limit). vinext@1.2.1 with the identical project works.

Projects wrap next/image this way because vinext does not honor images.loaderFile (#2699; open PR #3686).

Versions

works broken
vinext 1.2.1 1.3.0
@vinext/cloudflare 1.1.1 1.2.0

Also: vite 8.3.5, @cloudflare/vite-plugin 1.65.0, @vitejs/plugin-rsc 0.5.38, react / react-dom 19.3.1, next 16.4.2, wrangler 4.152.0, Node 22.22.0, Linux. (vinext@1.3.0 / @vinext/cloudflare@1.2.0 are pinned together because @vinext/cloudflare@1.2.0 peers vinext@^1.3.0; @vinext/cloudflare@1.1.1 peers ^1.2.0.)

Minimal reproduction

package.json

{
  "name": "vinext-next-image-alias-repro",
  "private": true,
  "type": "module",
  "scripts": { "build": "vite build" },
  "dependencies": {
    "next": "^16.4.2",
    "react": "^19.3.1",
    "react-dom": "^19.3.1"
  },
  "devDependencies": {
    "@cloudflare/vite-plugin": "1.65.0",
    "@vinext/cloudflare": "1.2.0",
    "@vitejs/plugin-rsc": "^0.5.37",
    "react-server-dom-webpack": "^19.3.1",
    "vinext": "1.3.0",
    "vite": "^8.3.5",
    "wrangler": "^4"
  }
}

vite.config.ts

import { fileURLToPath } from "node:url";
import { cloudflare } from "@cloudflare/vite-plugin";
import vinext from "vinext";
import { defineConfig } from "vite";

export default defineConfig({
  resolve: {
    alias: [
      {
        find: /^next\/image$/,
        replacement: fileURLToPath(new URL("./app/image-wrapper.tsx", import.meta.url)),
      },
    ],
  },
  plugins: [
    vinext(),
    cloudflare({ viteEnvironment: { name: "rsc", childEnvironments: ["ssr"] } }),
  ],
});

wrangler.jsonc

{
  "name": "vinext-image-alias-repro",
  "main": "worker/index.ts",
  "compatibility_date": "2026-09-27",
  "compatibility_flags": ["nodejs_compat"],
  "assets": { "not_found_handling": "none", "binding": "ASSETS" }
}

worker/index.ts

import handler from "vinext/server/fetch-handler";

export default {
  fetch(request: Request, env: unknown, ctx: ExecutionContext) {
    return handler.fetch(request, env, ctx);
  },
};

tsconfig.json

{ "compilerOptions": { "jsx": "react-jsx", "module": "esnext", "moduleResolution": "bundler", "target": "es2022", "strict": true, "skipLibCheck": true } }

app/layout.tsx

import type { ReactNode } from "react";

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

app/page.tsx

import Image from "next/image";

export default function Page() {
  return <Image src="/x.png" width={10} height={10} alt="" />;
}

app/image-wrapper.tsx

"use client";

import VinextImage from "vinext/shims/image";
import type { ComponentProps } from "react";

export default function Image(props: ComponentProps<typeof VinextImage>) {
  return <VinextImage {...props} />;
}

Commands:

npm install
npx vite build --minify false          # succeeds; --minify false only to make the chunk readable
npx wrangler dev -c dist/server/wrangler.json --local --port 8901
curl -m 20 -w '\nHTTP %{http_code}\n' localhost:8901/

Same project with "vinext": "1.2.1" and "@vinext/cloudflare": "1.1.1" is the control.

Expected vs actual

Expected (and observed on 1.2.1): HTTP 200, body contains <img src="/_next/image?url=%2Fx.png&amp;w=32&amp;q=75" ... data-nimg="1"/>.

Actual on 1.3.0: the request never completes.

$ curl -m 20 localhost:8901/
curl: (28) Operation timed out after 20002 milliseconds with 0 bytes received
HTTP 000

While the request is pending, the workerd process (ps -eo pid,pcpu,rss) read 39% CPU / 259 MB RSS at about 8 s, then 61% CPU / 371 MB RSS at about 20 s, i.e. still growing.

It is not Workers-specific. The same project without @cloudflare/vite-plugin (plugins: [vinext()], vite build, vinext start -p 8905) on 1.3.0 also times out after 20 s with the Node server at ~80% CPU / ~408 MB RSS. (Only 1.3.0 was run for the Node variant; I did not run 1.2.1 for it.)

Built SSR chunk on 1.3.0 (dist/server/ssr/_next/static/image-wrapper-*.js):

// app/image-wrapper.tsx
var import_jsx_runtime = require_jsx_runtime();
function Image(props) {
	return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Image, { ...props });
}
export { Image as default };

On 1.2.1 the equivalent chunk instead inlines the real shim (// node_modules/vinext/dist/shims/use-merged-ref.js etc.).

Root cause

node_modules/vinext/dist/index.js in 1.3.0 (line 2184), in the plugin's resolveId, added by #4055:

if (cleanId === "vinext/shims/image" && this.environment?.name !== "rsc")
  return this.resolve("next/image", importer, { skipSelf: true });

this.resolve("next/image", ...) runs the remaining resolvers, including Vite's resolve.alias. With the alias in the repro, next/image resolves to app/image-wrapper.tsx. The wrapper's own import ... from "vinext/shims/image" therefore resolves to the wrapper. (The RSC environment is excluded from the branch, which is why the server-component side, page.tsx, still sees a client reference to the wrapper, as intended.) This is my reading of the code and of the built output; I did not bisect inside the plugin beyond confirming the line above is the only vinext/shims/image resolve special-case (the other matches are the getImageProps re-export virtual module at lines 2367-2368).

Why it is silent

  • Resolution succeeds, there is just no cycle detection: a module importing its own id is legal ESM.
  • The build output showed no warning about it (only unrelated SOURCEMAP_BROKEN notices).
  • The cycle is not in the module graph but in the render: Image calls jsx(Image, ...) and React renders it forever, so the failure only appears at request time as a hang / resource exhaustion rather than a stack overflow with a useful message.

Suggested fixes (for maintainers to judge)

  1. In that branch, when importer is the file that next/image resolves to through user config (i.e. the importer equals the result of this.resolve("next/image", ...)), resolve the shim's real file instead of re-resolving next/image.
  2. More generally, only forward to next/image when the importer is not itself the alias target; otherwise fall through to the shim path, restoring the 1.2.1 behavior for wrappers.
  3. If that is not feasible, detect resolved.id === importer in this branch and throw a build error naming the alias, rather than emitting a self-importing module.
  4. Longer term, supporting images.loaderFile (next/image ignores images.loaderFile for remote src (renders unoptimized); per-component loader prop yields width=0 #2699, PR fix(image): fix custom-loader srcSet, support images.loaderFile #3686) removes the main reason projects need this wrapper.

Workaround

Stop aliasing next/image: keep the wrapper at a normal path (for example components/image.tsx) and import it explicitly (import Image from "@/components/image") instead of next/image. Staying on vinext@1.2.1 / @vinext/cloudflare@1.1.1 also works.

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions