Skip to content

fetch: respect HTTP Cache-Control headers with TTL-based invalidation (#91729)#92322

Draft
lukesandberg wants to merge 1 commit intore_apply_session_dependent_tasksfrom
respect_http_cache_control_headers_for_fetches
Draft

fetch: respect HTTP Cache-Control headers with TTL-based invalidation (#91729)#92322
lukesandberg wants to merge 1 commit intore_apply_session_dependent_tasksfrom
respect_http_cache_control_headers_for_fetches

Conversation

@lukesandberg
Copy link
Copy Markdown
Contributor

@lukesandberg lukesandberg commented Apr 3, 2026

Summary

This is a re-application of the HTTP fetch part of #91729. Stacked on #92358.

Fix fetch to respect HTTP Cache-Control headers

Previously, fetch results were cached indefinitely, meaning results would never be refreshed (unless the cache was invalidated). Now they are session_dependent with a TTL to ensure we respect HTTP cache settings (e.g. Google Fonts with max-age=86400).

New two-task pattern:

  • fetch_inner (NOT session_dependent): Performs the HTTP request, grabs an Invalidator for itself, and returns the response + invalidator + absolute deadline. Cached across sessions.
  • fetch (network, session_dependent): Reads the cached fetch_inner result and spawns a timer to invalidate when the TTL expires.

On warm cache restore, fetch re-executes (session-dependent), reads the persisted deadline from fetch_inner's cached result, computes remaining TTL, and spawns a timer — no HTTP request unless the TTL has already expired. Mid-session, the timer fires and triggers a re-fetch.

Error handling: On fetch failure, fetch_inner takes a dependency on Completion::session_dependent() so transient errors (network down, DNS failure) are retried on the next session without busy-looping.

Test Plan

3 new integration tests in turbo-tasks-fetch/tests/fetch.rs:

  • ttl_invalidates_within_session — mock server returns max-age=1, body changes, verifies re-fetch after TTL
  • ttl_invalidates_on_session_restore — fetches with TTL, stops TT, waits for expiry, warm restores with new TT, verifies re-fetch
  • errors_retried_on_session_restore — server returns 500, stops TT, fixes server, warm restores, verifies success
  • Existing 6 fetch tests continue to pass

Closes NEXT-

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Apr 3, 2026
Copy link
Copy Markdown
Contributor Author

lukesandberg commented Apr 3, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

impl Default for FetchClientConfig {
fn default() -> Self {
Self {
min_cache_control_secs: 60 * 60,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a duration

@nextjs-bot
Copy link
Copy Markdown
Collaborator

nextjs-bot commented Apr 3, 2026

Tests Passed

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 3, 2026

Merging this PR will not alter performance

✅ 8 untouched benchmarks
⏩ 12 skipped benchmarks1


Comparing respect_http_cache_control_headers_for_fetches (04c1db8) with re_apply_session_dependent_tasks (a550e51)2

Open in CodSpeed

Footnotes

  1. 12 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.

  2. No successful run was found on re_apply_session_dependent_tasks (c7c7689) during the generation of this report, so d11a213 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 97f761f to 1ca16ac Compare April 3, 2026 20:30
@nextjs-bot
Copy link
Copy Markdown
Collaborator

nextjs-bot commented Apr 3, 2026

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) 456ms 456ms ▅▅▅▁█
Cold (Ready in log) 440ms 439ms ▇█▂▁▂
Cold (First Request) 1.124s 1.117s ▆██▆▁
Warm (Listen) 457ms 457ms ▁▁██▁
Warm (Ready in log) 441ms 441ms ▇█▁▂▂
Warm (First Request) 335ms 341ms ▅▇▁▃▅
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 456ms 455ms ▆▃▆▃▃
Cold (Ready in log) 435ms 434ms ▆▃▂▁▃
Cold (First Request) 1.930s 1.928s █▄▆▇▇
Warm (Listen) 456ms 456ms ▁▁▁▁▁
Warm (Ready in log) 434ms 433ms ▆▃▃▁▂
Warm (First Request) 1.944s 1.944s ▇▃▆▆▇

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.894s 3.825s ▅█▅▇▆
Cached Build 3.905s 3.883s ▄▅█▅█
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.366s 14.495s ▇▁▁▁▂
Cached Build 14.461s 14.544s █▃▂▁▂
node_modules Size 488 MB 488 MB ▅████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
02fkg8wfh0iju.js gzip 9.19 kB N/A -
050zwt5xh_0tx.js gzip 10.4 kB N/A -
06rvbj82bhyo0.js gzip 13 kB N/A -
087fzjd-gvlzv.js gzip 450 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0j9nyyebpzuve.js gzip 65.7 kB N/A -
0liwiknewtyj-.js gzip 155 B N/A -
0lz6rvqk4dq28.js gzip 157 B N/A -
0ppxcl_z43mad.js gzip 8.52 kB N/A -
0t-vypuopaopr.js gzip 156 B N/A -
0taq90fnaps1c.js gzip 154 B N/A -
0w-q2j65cf4d_.js gzip 70.8 kB N/A -
177wlvvlyngbh.js gzip 162 B N/A -
19oha6-znmkcv.js gzip 8.55 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1jiaxunj6aqjm.js gzip 154 B N/A -
1lwlgx9s5r-bl.js gzip 155 B N/A -
2_5rjb7lqxntf.js gzip 221 B 221 B
219prxwxgaalc.js gzip 7.61 kB N/A -
26elcgxnn9zjd.js gzip 8.52 kB N/A -
2900hudr6gvm0.js gzip 2.28 kB N/A -
2lv2js3kmdeho.js gzip 8.48 kB N/A -
2pcakafkxmy0y.js gzip 159 B N/A -
2rehygrd36hqv.js gzip 8.58 kB N/A -
2srwswih0m9_h.js gzip 13.3 kB N/A -
3-p9p9mheqhzx.js gzip 8.55 kB N/A -
31030bryqpolg.js gzip 8.53 kB N/A -
31dx5nmrzzuy7.js gzip 225 B N/A -
35jcqqltah41n.js gzip 161 B N/A -
3925v09gtu-5k.js gzip 49 kB N/A -
39x4zj5mjb4d_.js gzip 9.77 kB N/A -
3em05vpajqpwz.js gzip 169 B N/A -
3k-48b78ys_vy.js gzip 10.1 kB N/A -
3m7-5rfj0avoz.js gzip 12.9 kB N/A -
3s-7xts2e4w68.js gzip 158 B N/A -
3uqce_6sa526g.js gzip 8.47 kB N/A -
3yurjqk-sjs3y.js gzip 1.46 kB N/A -
40ybjx9c192n0.js gzip 13.8 kB N/A -
421vzwdt9j1b_.js gzip 5.62 kB N/A -
450cprx9a1he-.js gzip 157 B N/A -
458kx8xybb_ty.js gzip 157 B N/A -
turbopack-0g..vaan.js gzip 4.18 kB N/A -
turbopack-0x..zr41.js gzip 4.18 kB N/A -
turbopack-1d..d4y6.js gzip 4.18 kB N/A -
turbopack-1j..5f0x.js gzip 4.18 kB N/A -
turbopack-1q..mobr.js gzip 4.18 kB N/A -
turbopack-1x..zepa.js gzip 4.18 kB N/A -
turbopack-20..hso6.js gzip 4.17 kB N/A -
turbopack-24..svkv.js gzip 4.17 kB N/A -
turbopack-2i..2mpu.js gzip 4.18 kB N/A -
turbopack-2m..v5gs.js gzip 4.18 kB N/A -
turbopack-2t..mebg.js gzip 4.16 kB N/A -
turbopack-39..hd24.js gzip 4.19 kB N/A -
turbopack-3m..e-_l.js gzip 4.17 kB N/A -
turbopack-42..g8c-.js gzip 4.17 kB N/A -
03dgzoo-qf3sm.js gzip N/A 9.19 kB -
05tx5f25dlivn.js gzip N/A 8.53 kB -
085gix7vncldb.js gzip N/A 154 B -
08qxf1bxk2sy0.js gzip N/A 153 B -
0c7ez6p2qc57f.js gzip N/A 5.62 kB -
0duvj3qk5pvgn.js gzip N/A 13.8 kB -
0hvsdb3udqtqq.js gzip N/A 156 B -
0k12a08xkwlz-.js gzip N/A 157 B -
0m-34rm9w_wpm.js gzip N/A 7.6 kB -
0nlykkpfyyg2w.js gzip N/A 155 B -
0qnwuk92m8i7o.js gzip N/A 10.4 kB -
0r4wrn6n0ue2m.js gzip N/A 8.55 kB -
0rp0fodtbt_6m.js gzip N/A 8.52 kB -
0sfck-km4dl1k.js gzip N/A 8.47 kB -
0x0xuhmxzwkp8.js gzip N/A 8.47 kB -
1_t7k492adi7n.js gzip N/A 155 B -
1_xgyhyp7ql91.js gzip N/A 157 B -
1-wdvgxnzicj7.js gzip N/A 1.46 kB -
11u6nxujb2eg4.js gzip N/A 450 B -
15w1ub8e8wqfw.js gzip N/A 159 B -
1aeiaf859ymle.js gzip N/A 154 B -
1jv-o1_s-zmua.js gzip N/A 49 kB -
2in9gz64_b29q.js gzip N/A 168 B -
2k9ax08cjl2id.js gzip N/A 12.9 kB -
2lms6k76q5-6m.js gzip N/A 13.3 kB -
2qx4twi9i3xus.js gzip N/A 2.28 kB -
2srnqic6tvxxd.js gzip N/A 8.52 kB -
30beetijii86g.js gzip N/A 65.7 kB -
30l7m4nayp73a.js gzip N/A 8.55 kB -
33hhl7rvbciep.js gzip N/A 159 B -
38rr7d3kfutni.js gzip N/A 13 kB -
3b-ri3-nxw0g7.js gzip N/A 70.8 kB -
3c73-g0fvlevb.js gzip N/A 154 B -
3h_ecpiaatwgc.js gzip N/A 10.1 kB -
3ity0aahajapd.js gzip N/A 225 B -
3tn3pab2yum0u.js gzip N/A 155 B -
3wrhpuc-j1aw9.js gzip N/A 9.77 kB -
43mlw9dy_8f02.js gzip N/A 8.58 kB -
turbopack-08..n7x8.js gzip N/A 4.18 kB -
turbopack-0a..t8jc.js gzip N/A 4.18 kB -
turbopack-0a..nf4n.js gzip N/A 4.17 kB -
turbopack-0r..v9zz.js gzip N/A 4.18 kB -
turbopack-0z..a5r2.js gzip N/A 4.16 kB -
turbopack-1-..bedd.js gzip N/A 4.17 kB -
turbopack-1c..qjua.js gzip N/A 4.17 kB -
turbopack-1p..1_tg.js gzip N/A 4.17 kB -
turbopack-1q..-f4x.js gzip N/A 4.17 kB -
turbopack-2u..6dws.js gzip N/A 4.17 kB -
turbopack-2u..dzlg.js gzip N/A 4.18 kB -
turbopack-2y..u7ck.js gzip N/A 4.17 kB -
turbopack-30..s215.js gzip N/A 4.18 kB -
turbopack-44..868h.js gzip N/A 4.19 kB -
Total 464 kB 464 kB ✅ -60 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 719 B 714 B
Total 719 B 714 B ✅ -5 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 432 B 427 B 🟢 5 B (-1%)
Total 432 B 427 B ✅ -5 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 60.7 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 256 B 254 B
main-HASH.js gzip 39.3 kB 39.2 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.8 kB -
9544-HASH.js gzip N/A 61.4 kB -
Total 235 kB 235 kB ⚠️ +649 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 194 B
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 351 B 352 B
hooks-HASH.js gzip 384 B 383 B
image-HASH.js gzip 580 B 581 B
index-HASH.js gzip 260 B 260 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 320 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.98 kB ✅ -1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 126 kB
page.js gzip 273 kB 273 kB
Total 398 kB 398 kB ⚠️ +297 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 617 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 44.2 kB 44.3 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.8 kB 45.9 kB ⚠️ +60 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.38 MB 4.37 MB
index.pack gzip 115 kB 116 kB 🔴 +1.71 kB (+1%)
index.pack.old gzip 113 kB 116 kB 🔴 +2.11 kB (+2%)
Total 4.61 MB 4.61 MB ✅ -383 B

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 342 kB 342 kB
app-page-exp..prod.js gzip 189 kB 189 kB
app-page-tur...dev.js gzip 341 kB 341 kB
app-page-tur..prod.js gzip 189 kB 189 kB
app-page-tur...dev.js gzip 338 kB 338 kB
app-page-tur..prod.js gzip 187 kB 187 kB
app-page.run...dev.js gzip 338 kB 338 kB
app-page.run..prod.js gzip 187 kB 187 kB
app-route-ex...dev.js gzip 76.6 kB 76.6 kB
app-route-ex..prod.js gzip 52.2 kB 52.2 kB
app-route-tu...dev.js gzip 76.6 kB 76.6 kB
app-route-tu..prod.js gzip 52.2 kB 52.2 kB
app-route-tu...dev.js gzip 76.2 kB 76.2 kB
app-route-tu..prod.js gzip 52 kB 52 kB
app-route.ru...dev.js gzip 76.2 kB 76.2 kB
app-route.ru..prod.js gzip 52 kB 52 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 43.8 kB 43.8 kB
pages-api-tu..prod.js gzip 33.4 kB 33.4 kB
pages-api.ru...dev.js gzip 43.8 kB 43.8 kB
pages-api.ru..prod.js gzip 33.4 kB 33.4 kB
pages-turbo....dev.js gzip 53.2 kB 53.2 kB
pages-turbo...prod.js gzip 39 kB 39 kB
pages.runtim...dev.js gzip 53.2 kB 53.2 kB
pages.runtim..prod.js gzip 39 kB 39 kB
server.runti..prod.js gzip 62.8 kB 62.8 kB
Total 3.03 MB 3.03 MB
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/7c5aad05dcc8ae6879b348b614d3e2630279765c/next

@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch 2 times, most recently from c338444 to a88b102 Compare April 3, 2026 21:47
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 1ca16ac to 805b915 Compare April 3, 2026 21:47
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from a88b102 to f5f9dc0 Compare April 4, 2026 19:11
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 805b915 to 68f3514 Compare April 4, 2026 19:11
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 68f3514 to d6da110 Compare April 6, 2026 17:26
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from f5f9dc0 to 0cae08f Compare April 6, 2026 17:26
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from 0cae08f to 70da271 Compare April 6, 2026 17:46
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch 2 times, most recently from 83fb56d to 708b5d2 Compare April 6, 2026 17:57
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from 70da271 to 781db31 Compare April 6, 2026 17:57
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from 781db31 to 0522d66 Compare April 6, 2026 20:11
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 708b5d2 to 746c98f Compare April 6, 2026 20:11
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from 0522d66 to 00769ef Compare April 6, 2026 20:26
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 746c98f to 7c5aad0 Compare April 6, 2026 20:26
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from 00769ef to a550e51 Compare April 7, 2026 16:24
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 7c5aad0 to 8524df5 Compare April 7, 2026 16:24
@lukesandberg lukesandberg force-pushed the re_apply_session_dependent_tasks branch from a550e51 to c7c7689 Compare April 8, 2026 00:04
@lukesandberg lukesandberg force-pushed the respect_http_cache_control_headers_for_fetches branch from 8524df5 to 04c1db8 Compare April 8, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants