Skip to content

Is the Nextjs 16 Turbopack supported? #69

Open

Description

@vctqs1

Versions: async-browser-context 0.3.0, Next.js 16.4.5 (Turbopack, next build), pnpm 12.

Config we use. The README's babel.config with next/babel and transpilePackages did not work for us. It broke Server Actions, could not skip files per file, and Turbopack's Babel pass did not cover node_modules. So we use a Turbopack rule that runs babel-loader with only your preset:

turbopack: {
  resolveAlias: {
    "async-browser-context/browser/runtime":
      "../../node_modules/async-browser-context/dist/runtime.js",
  },
  rules: {
    "*.{ts,tsx,js,jsx,mjs}": {
      condition: {
        // browser bundle only; first-party TS/TSX and every dependency,
        // except the files and packages listed under "Problems" below
      },
      loaders: [
        {
          loader: "babel-loader",
          options: {
            babelrc: false,
            configFile: false,
            sourceType: "unambiguous",
            presets: [
              [
                "async-browser-context/babel-preset",
                { runtime: "async-browser-context/browser/runtime" },
              ],
            ],
            overrides: [
              /* parserOpts: typescript for .ts, typescript + jsx for .tsx */
            ],
          },
        },
      ],
    },
  },
},

We rewrite dependencies as well as our own code, because Next's server action reducer and react-hook-form both await between an event and our code.

Problems

  1. "use server" and "use cache" files. The rewrite makes the function non-async and uses this and arguments, which Next rejects there. We exclude those files by matching the directive in the file content. Is there a supported way, for example a preset option?
  2. pnpm. A rewritten dependency imports the runtime from inside its own package, where pnpm's layout cannot resolve it. The resolveAlias above is our workaround.

Questions: Is this setup supported, and do you recommend a different one for Next.js (Turbopack or webpack)? Would you take a PR that adds a Next.js section to the docs?

Clarification about rule coverage and performance: Our Turbopack rule matches .ts, .tsx, .js, and .jsx files across the application, as well as dependencies subject to the exclusions above. That covers most of the code, rather than only a few selected files. I am concerned that running babel-loader over this much code may affect build performance. Is there a way to use SWC for this transformation, or otherwise let SWC handle the files while retaining the required AsyncBrowserContext behavior? If not, what performance impact should we expect from Babel in this Turbopack setup?

Activity

  1. changed the title [-]Is the Nextjs 16 Turbopack supported? Config we use , and 3 problems[/-] [+]Is the Nextjs 16 Turbopack supported? [/+] on Nov 13, 2026
  2. vctqs1 commented on Nov 13, 2026

    @vctqs1
    Author

    Hi @mark1russell7 thanks for creating this library, its soo cold. I've been looking for this for a while.

    I would like to inject it with our Nextjs, to able get one trace even after await. Could you have a look if that is right config or if not could you please add a support for the Nextjs turbopack project?

  3. changed the title [-]Is the Nextjs 16 Turbopack supported? [/-] [+]Is the Nextjs 16 Turbopack supported?[/+] on Nov 13, 2026
  4. mark1russell7 commented on Nov 15, 2026

    @mark1russell7
    Owner

    Thank you for the detailed report and the config. It found a real bug. Here are the answers, in the order of your questions.

    1. "use server" and "use cache" files: a bug, fixed in 0.3.2

    The preset changed these functions into functions that are not async and that use this and arguments, so Next.js stopped the build. This happened with your Turbopack rule, and also with the babel.config.js setup of our README. The CI test did not have a server action, so it did not find the problem.

    Since 0.3.2 (#77), the preset does not change a file or a function with "use server", "use cache" or "use cache: <kind>". It still changes the other functions of the file: for example, an async page with an inline server action changes, but the action stays a native async function. Thus you can remove your content exclusion for these files. No option is necessary.

    2. Is your setup supported?

    Yes. Your Turbopack rule is now the recommended Next.js setup in the guide, and CI builds it with Next.js 16.4 (the case next-turbopack-rule). The test page starts a server action and a "use cache" function, and examines the context in the browser, also in a callback of a dependency in node_modules.

    The guide: https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/build-tools#nextjs

    Some differences from your config, which you can use or not:

    • A content condition. { all : ["browser", { content : /\basync\b|\byield\b|function\s*\*/ }] } gives Babel only the files that the preset can change. This is the filter of our Vite plugin.
    • One rule for each kind of file in place of overrides: ["jsx"] for *.{js,jsx,mjs,cjs}, ["typescript"] for *.{ts,mts,cts}, and ["typescript", "jsx"] for *.tsx. Turbopack gives the loader options as JSON, so the options must not contain a regular expression or a function.
    • The runtime option is not necessary. In the browser bundle, async-browser-context/runtime already resolves to the browser runtime. Your async-browser-context/browser/runtime is also correct.

    The babel.config.js setup still works, and CI still builds it. But with it, Babel compiles all your code in place of SWC, and node_modules needs transpilePackages. Thus we now recommend it only for next build --webpack. With webpack, also give next/babel the browsers of Next.js as preset-env targets, as the guide shows. Next.js gives Babel no targets there. Without them, preset-env changes the async functions of a server action, and Next.js stops the build, also without our preset. The new CI test found this too.

    3. pnpm

    This depends on your pnpm settings. A file that the preset changed imports async-browser-context/runtime. A dependency in node_modules/.pnpm/<package>/node_modules/<package> must find the library from its own folder:

    pnpm setting A dependency finds the runtime What to do
    The default hoistPattern (*) Yes, in node_modules/.pnpm/node_modules Nothing
    hoist: false, or a hoistPattern without async-browser-context No Add async-browser-context to publicHoistPattern, or use the alias
    enableGlobalVirtualStore: true No Use the alias

    Your resolveAlias is a correct solution, and the guide now shows it. We use the form { browser : "<path>" }, so the server is not affected. CI examines that Next.js accepts it.

    Can you tell us your pnpm settings (hoist, hoistPattern, publicHoistPattern, enableGlobalVirtualStore, nodeLinker), and the full error text without the alias? Then we can find the cause, and tell it in the guide if your case is different from the table.

    4. SWC

    The library has no SWC plugin. But SWC can make the change without Babel: add a browser without async functions to the browserslist of package.json, for example "chrome 54". SWC then changes each async function into a generator, and Turbopack applies this also to node_modules. CI builds this setup too (next-turbopack-swc).

    The costs:

    • A generator body gets the context of each next() call, not the context of the call that made the generator (rule C5).
    • The callback hooks get no names.
    • SWC also changes all the syntax that is newer than the query, so the bundle gets larger.

    For most tracing, the context through await is the important part, and SWC keeps it.

    5. Performance

    We do not have numbers for a large app. Babel is slower than SWC. With the content condition, Babel gets only the browser files with async functions or generators, and SWC compiles all the other files. Each changed file also gets larger, because generators replace the async functions.

    To measure, compare next build and the bundle size with and without the rule. To make Babel do less work, exclude the packages that do not start your code after an await, with a path condition. The CI test now logs the time of each Next.js build, but that is one small page, so it is not a benchmark. In the CI run of #77, next build took 3.1 s with SWC and browserslist, 5.8 s with the Turbopack rule, 5.2 s with babel.config.js and Turbopack, and 18.2 s with babel.config.js and webpack. If you can share your numbers, we can add them to the guide.

    6. Docs

    Thank you for the offer. The Next.js section is now in the guide (link above). If something in it is not clear, or your setup needs more steps, a PR or a comment here is welcome.

  5. mark1russell7 commented on Nov 15, 2026

    @mark1russell7
    Owner

    @vctqs1 also, thank you for the kind words and raising an issue!

  6. vctqs1 commented on Nov 15, 2026

    @vctqs1
    Author

    thanks @mark1russell7, for the quick fix and detailed answers:

    1. Setup: I'll try your content condition { all : ["browser", { content : /\basync\b|\byield\b|function\s*\*/ }] }
    2. pnpm: Our settings is enableGlobalVritualStore: true + hoistWorkspacePackages: false + publicHoistPattern: *prettier* only soo i'll keep the resolveAlias config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions