Repository navigation
Description
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
"use server"and"use cache"files. The rewrite makes the function non-async and usesthisandarguments, 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?- pnpm. A rewritten dependency imports the runtime from inside its own package, where pnpm's layout cannot resolve it. The
resolveAliasabove 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
[-]Is the Nextjs 16 Turbopack supported? Config we use , and 3 problems[/-][+]Is the Nextjs 16 Turbopack supported? [/+]vctqs1 commentedon Nov 13, 2026
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?[-]Is the Nextjs 16 Turbopack supported? [/-][+]Is the Nextjs 16 Turbopack supported?[/+]fix(babel): do not change "use server" and "use cache" code (#77)
mark1russell7 commentedon Nov 15, 2026
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.2The preset changed these functions into functions that are not async and that use
thisandarguments, so Next.js stopped the build. This happened with your Turbopack rule, and also with thebabel.config.jssetup 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 innode_modules.The guide: https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/build-tools#nextjs
Some differences from your config, which you can use or not:
contentcondition.{ 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.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.runtimeoption is not necessary. In the browser bundle,async-browser-context/runtimealready resolves to the browser runtime. Yourasync-browser-context/browser/runtimeis also correct.The
babel.config.jssetup still works, and CI still builds it. But with it, Babel compiles all your code in place of SWC, andnode_modulesneedstranspilePackages. Thus we now recommend it only fornext build --webpack. With webpack, also givenext/babelthe browsers of Next.js aspreset-envtargets, 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 innode_modules/.pnpm/<package>/node_modules/<package>must find the library from its own folder:hoistPattern(*)node_modules/.pnpm/node_moduleshoist: false, or ahoistPatternwithoutasync-browser-contextasync-browser-contexttopublicHoistPattern, or use the aliasenableGlobalVirtualStore: trueYour
resolveAliasis 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
browserslistofpackage.json, for example"chrome 54". SWC then changes each async function into a generator, and Turbopack applies this also tonode_modules. CI builds this setup too (next-turbopack-swc).The costs:
next()call, not the context of the call that made the generator (rule C5).For most tracing, the context through
awaitis 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
contentcondition, 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 buildand the bundle size with and without the rule. To make Babel do less work, exclude the packages that do not start your code after anawait, with apathcondition. 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 buildtook 3.1 s with SWC andbrowserslist, 5.8 s with the Turbopack rule, 5.2 s withbabel.config.jsand Turbopack, and 18.2 s withbabel.config.jsand 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.
mark1russell7 commentedon Nov 15, 2026
@vctqs1 also, thank you for the kind words and raising an issue!
vctqs1 commentedon Nov 15, 2026
thanks @mark1russell7, for the quick fix and detailed answers:
{ all : ["browser", { content : /\basync\b|\byield\b|function\s*\*/ }] }enableGlobalVritualStore: true+hoistWorkspacePackages: false+publicHoistPattern: *prettier*only soo i'll keep theresolveAliasconfig