Skip to content

Turbopack: add support for matching loaders on resource queries#88644

Merged
mischnic merged 5 commits intocanaryfrom
sokra/query-condition
Jan 16, 2026
Merged

Turbopack: add support for matching loaders on resource queries#88644
mischnic merged 5 commits intocanaryfrom
sokra/query-condition

Conversation

@sokra
Copy link
Member

@sokra sokra commented Jan 16, 2026

What?

Part of PACK-1007
Closes PACK-4958
Closes #79311

Adds condition.query support to Turbopack's loader rules, enabling loaders to match based on import query strings (e.g., import './file?raw'). This mirrors webpack's resourceQuery functionality.

The query condition accepts either a string (exact match) or RegExp (pattern match).

Adds tests and documentation for the new condition.query option.

How to use

// next.config.js
module.exports = {
  turbopack: {
    rules: {
      '*.txt': [
        // String: exact match
        { condition: { query: '?raw' }, loaders: ['raw-loader'], as: '*.js' },
        // RegExp: pattern match  
        { condition: { query: /\?transform/ }, loaders: ['transform-loader'], as: '*.js' },
      ],
    },
  },
}

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. tests Turbopack Related to Turbopack with Next.js. type: next labels Jan 16, 2026
@nextjs-bot
Copy link
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 05ee77e

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Jan 16, 2026

Allow CI Workflow Run

  • approve CI run for commit: ecc4e18

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link
Member Author

sokra commented Jan 16, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@nextjs-bot nextjs-bot added the Documentation Related to Next.js' official documentation. label Jan 16, 2026
@sokra sokra requested a review from mischnic January 16, 2026 15:43
@sokra sokra marked this pull request as ready for review January 16, 2026 15:43
@sokra sokra changed the title add test case for resource queries Turbopack: add support for resource queries Jan 16, 2026
@sokra sokra changed the title Turbopack: add support for resource queries Turbopack: add support for matching loaders on resource queries Jan 16, 2026
@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Jan 16, 2026

Failing test suites

Commit: 4bb3e1d | About building and testing Next.js

pnpm test test/integration/telemetry/test/index.test.ts (turbopack) (job)

  • Telemetry CLI > production mode > emits event when swc fails to load (DD)
Expand output

● Telemetry CLI › production mode › emits event when swc fails to load

expect(received).toMatch(expected)

Expected pattern: /NEXT_SWC_LOAD_FAILURE/
Received string:  "⚠ Attempted to load @next/swc-linux-x64-gnu, but an error occurred: Cannot load native addon because loading addons is disabled.
⚠ Attempted to load @next/swc-linux-x64-musl, but an error occurred: Cannot load native addon because loading addons is disabled.
⚠ Attempted to load @next/swc-wasm-nodejs, but it was not installed
⚠ Attempted to load @next/swc-wasm-web, but it was not installed
⨯ Failed to load SWC binary for linux/x64, see more info here: https://nextjs.org/docs/messages/failed-loading-swc·
> Build error occurred
Error: Failed to load SWC binary for linux/x64, see more info here: https://nextjs.org/docs/messages/failed-loading-swc

  at <unknown> (../dist/build/swc/index.js:318:38)
  "
  at Object.toMatch (integration/telemetry/test/index.test.ts:111:24)

pnpm test-dev-turbo test/development/acceptance-app/app-hmr-changes.test.ts (turbopack) (job)

  • Error overlay - RSC build errors > Skipped in webpack > should handle successive HMR changes with errors correctly (DD)
Expand output

● Error overlay - RSC build errors › Skipped in webpack › should handle successive HMR changes with errors correctly

Expected no visible Redbox but found one
header: Runtime TypeError
frame.join is not a function
description: frame.join is not a function
source: null

  58 |
  59 |           await session.patch(pagePath, originalPage)
> 60 |           await session.waitForNoRedbox()
     |           ^
  61 |         }
  62 |
  63 |         expect(

  at Object.<anonymous> (development/acceptance-app/app-hmr-changes.test.ts:60:11)

pnpm test-start-turbo test/e2e/app-dir/segment-cache/revalidation/segment-cache-revalidation.test.ts (turbopack) (job)

  • segment cache (revalidation) > re-fetch visible links after a navigation, if needed (DD)
Expand output

● segment cache (revalidation) › re-fetch visible links after a navigation, if needed

thrown: "Exceeded timeout of 120000 ms for a test.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

  248 |   })
  249 |
> 250 |   it('re-fetch visible links after a navigation, if needed', async () => {
      |   ^
  251 |     let act: ReturnType<typeof createRouterAct>
  252 |     const browser = await next.browser('/refetch-on-new-base-tree/a', {
  253 |       beforePageLoad(page) {

  at it (e2e/app-dir/segment-cache/revalidation/segment-cache-revalidation.test.ts:250:3)
  at Object.describe (e2e/app-dir/segment-cache/revalidation/segment-cache-revalidation.test.ts:7:1)

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 16, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing sokra/query-condition (4bb3e1d) with canary (f67bc23)

Summary

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@mischnic mischnic merged commit 7e9d7cb into canary Jan 16, 2026
291 of 299 checks passed
@mischnic mischnic deleted the sokra/query-condition branch January 16, 2026 20:12
@nextjs-bot
Copy link
Collaborator

Stats from current PR

✅ No significant changes detected

📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 455ms 456ms ▁▁▁▁▁
Cold (Ready in log) 439ms 440ms ▂▂▂▁▂
Cold (First Request) 1.116s 1.166s ▄▄▄▄▄
Warm (Listen) 458ms 456ms ▁▁▁▁▁
Warm (Ready in log) 442ms 444ms ▁▁▁▁▁
Warm (First Request) 340ms 343ms ▂▁▁▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 455ms 455ms ▁▁▁█▅
Cold (Ready in log) 437ms 435ms ▆█▆█▁
Cold (First Request) 1.820s 1.817s ▄▄▄█▁
Warm (Listen) 456ms 456ms ▁▅▅█▅
Warm (Ready in log) 436ms 437ms ▆▆▆█▁
Warm (First Request) 1.825s 1.823s ▄▄▄█▁

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 4.428s 4.302s ▁▁▁▁▁
Cached Build 4.357s 4.356s ▁▁▁▁▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 13.925s 13.894s ▁▁▂█▁
Cached Build 13.969s 13.995s ▁▂▃█▂
node_modules Size 458 MB 458 MB ▃▃▃▃█
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles: **430 kB** → **430 kB** ✅ -82 B

82 files with content-based hashes (individual files not comparable between builds)

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 790 B 790 B
Total 790 B 790 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 452 B 451 B
Total 452 B 451 B ✅ -1 B

📦 Webpack

Client

Main Bundles
Canary PR Change
2086.HASH.js gzip 169 B N/A -
2161-HASH.js gzip 5.41 kB N/A -
2747-HASH.js gzip 4.48 kB N/A -
4322-HASH.js gzip 52.3 kB N/A -
ec793fe8-HASH.js gzip 62.3 kB N/A -
framework-HASH.js gzip 59.8 kB 59.8 kB
main-app-HASH.js gzip 251 B 254 B 🔴 +3 B (+1%)
main-HASH.js gzip 38.6 kB 38.9 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
1596.HASH.js gzip N/A 169 B -
2658-HASH.js gzip N/A 51.9 kB -
6349-HASH.js gzip N/A 4.46 kB -
7019-HASH.js gzip N/A 5.43 kB -
b17a3386-HASH.js gzip N/A 62.3 kB -
Total 225 kB 225 kB ✅ -12 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 193 B
_error-HASH.js gzip 182 B 182 B
css-HASH.js gzip 336 B 335 B
dynamic-HASH.js gzip 1.8 kB 1.8 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 352 B 351 B
hooks-HASH.js gzip 385 B 384 B
image-HASH.js gzip 580 B 580 B
index-HASH.js gzip 259 B 258 B
link-HASH.js gzip 2.5 kB 2.51 kB
routerDirect..HASH.js gzip 319 B 317 B
script-HASH.js gzip 385 B 387 B
withRouter-HASH.js gzip 316 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.97 kB 7.97 kB ✅ -6 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 125 kB
page.js gzip 242 kB 237 kB 🟢 4.92 kB (-2%)
Total 367 kB 362 kB ✅ -4.91 kB
Middleware
Canary PR Change
middleware-b..fest.js gzip 653 B 654 B
middleware-r..fest.js gzip 155 B 156 B
middleware.js gzip 33.1 kB 33.3 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 34.7 kB 35 kB ⚠️ +259 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 738 B 737 B
Total 738 B 737 B ✅ -1 B
Build Cache
Canary PR Change
0.pack gzip 3.66 MB 3.67 MB 🔴 +9.92 kB (+0%)
index.pack gzip 100 kB 101 kB
index.pack.old gzip 99 kB 99.4 kB
Total 3.86 MB 3.87 MB ⚠️ +11.2 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 304 kB 304 kB
app-page-exp..prod.js gzip 158 kB 158 kB
app-page-tur...dev.js gzip 304 kB 304 kB
app-page-tur..prod.js gzip 158 kB 158 kB
app-page-tur...dev.js gzip 300 kB 300 kB
app-page-tur..prod.js gzip 156 kB 156 kB
app-page.run...dev.js gzip 300 kB 300 kB
app-page.run..prod.js gzip 156 kB 156 kB
app-route-ex...dev.js gzip 68.8 kB 68.8 kB
app-route-ex..prod.js gzip 47.6 kB 47.6 kB
app-route-tu...dev.js gzip 68.8 kB 68.8 kB
app-route-tu..prod.js gzip 47.6 kB 47.6 kB
app-route-tu...dev.js gzip 68.4 kB 68.4 kB
app-route-tu..prod.js gzip 47.4 kB 47.4 kB
app-route.ru...dev.js gzip 68.4 kB 68.4 kB
app-route.ru..prod.js gzip 47.4 kB 47.4 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 41.2 kB 41.2 kB
pages-api-tu..prod.js gzip 31.3 kB 31.3 kB
pages-api.ru...dev.js gzip 41.1 kB 41.1 kB
pages-api.ru..prod.js gzip 31.2 kB 31.2 kB
pages-turbo....dev.js gzip 50.8 kB 50.8 kB
pages-turbo...prod.js gzip 38.2 kB 38.2 kB
pages.runtim...dev.js gzip 50.7 kB 50.7 kB
pages.runtim..prod.js gzip 38.2 kB 38.2 kB
server.runti..prod.js gzip 62.2 kB 62.2 kB
Total 2.69 MB 2.69 MB ✅ -2 B
📝 Changed Files (2 files)

Files with changes:

  • pages-api.runtime.dev.js
  • pages.runtime.dev.js
View diffs
pages-api.runtime.dev.js

Diff too large to display

pages.runtime.dev.js

Diff too large to display

@jwueller
Copy link

jwueller commented Jan 20, 2026

@sokra As you could have read in the discussions this very issue links to, I had already implemented this weeks ago in #87291. I gotta say that it's quite disappointing to go through the effort of fixing a long-standing issue, only to not even get a review on the PR that was mentioned in all of the right places, including ones this PR doesn't. Feel free to close my PR, unless you can find something useful in it, still.

For what it's worth, I agree with @bgw in that using exact string matching against query strings is essentially always the wrong choice, and should likely always be RegExp instead. Otherwise it immediately breaks once additional query parameters are added.

Overall I'm glad this finally got addressed, even at the cost of my own work getting discarded.

@sokra
Copy link
Member Author

sokra commented Jan 20, 2026

@jwueller I'm very sorry about that. I didn't see your PR, but it looks good. I rebased it and got some value-able test, doc and performance improvements => #88801

I think we need to improve our workflow of handling PRs, but I don't really know how. We have >1k open PRs and github doesn't do the best job in making them discoverable...

@jwueller
Copy link

@sokra Thank you, I very much appreciate that! I'm happy to hear you got some mileage out of it still!

And I understand that it's hard to keep track of literally over a thousand PRs, of course. I'm honestly not sure how I'd deal with that either. Looking at those other issue keys, I'm assuming you came at it from an internal issue tracker. That probably made surfacing related work in other places harder as well.

@kevva
Copy link
Contributor

kevva commented Jan 20, 2026

Thanks for getting this over the line and huge thanks for @jwueller for the initial work. I've been waiting for this for almost 2 years now.

@sokra, how can we load files with the default/internal file/url loader that turbopack uses? E.g. if we have queries with ?url, how to load them as external URLs/files? I guess I can do it with not perhaps.

@jwueller
Copy link

jwueller commented Jan 20, 2026

@kevva I've been thinking about referencing internal loaders too, but that seems like a separate problem. Maybe we could have a "spread-like" placeholder for those, that can be inserted into custom loader arrays.

As for URL loading, you can actually use this literal syntax with Turbopack, although I'm not sure if it's documented anywhere:

new URL("./asset.png", import.meta.url)

It gets replaced with the public URL during compile.

@kevva
Copy link
Contributor

kevva commented Jan 20, 2026

@jwueller, yeah it's somewhat documented here. But this configuration seems to work too:

'*.svg': {
  condition: { not: { query: '?url' } },
  loaders: [{ loader: '@svgr/webpack' }],
  as: '*.js',
}

@jwueller
Copy link

jwueller commented Jan 20, 2026

@kevva Ah right, because it avoids matching to begin with, so the defaults still apply. I guess that's a decent workaround.

Interesting that the URL literal is only documented in the context of experimental URL imports. It worked for local paths for quite a while before that already, I'm pretty sure.

Copy link
Member Author

sokra commented Jan 20, 2026

This PR also adds type: "asset" (#88788) that is slightly related.

@github-actions github-actions bot added the locked label Feb 4, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. Documentation Related to Next.js' official documentation. locked tests Turbopack Related to Turbopack with Next.js. type: next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turbopack Fails with ResourceQuery in Glob Pattern

7 participants