---
title: "Cache Miss Route Fetching"
description: "The execution flow starting from fetchRouteOnCacheMiss down to encodeCacheBustingSearchParam coordinates fetching prefetch route information when a segment cache miss occurs, and ensures network re..."
last_updated: "2026-10-12T06:38:56.872063+00:00"
canonical_url: "https://www.doc0.dev/docs/8f4009b0-65bd-4480-9b00-e201f0914bb3/how-it-works/cache-miss-route-fetching"
---


## Overview

The execution flow starting from `fetchRouteOnCacheMiss` down to `encodeCacheBustingSearchParam` coordinates fetching prefetch route information when a segment cache miss occurs, and ensures network requests bypass CDNs that fail to respect custom router headers. By encoding key request headers into a compact query parameter (often `_rsc`), Next.js guarantees cache uniqueness across diverse hosting environments.


---

### Step 1: `fetchRouteOnCacheMiss`

When a route is requested and results in a cache miss, `fetchRouteOnCacheMiss` initializes headers specifying the RSC prefetch type and segment path (`/_tree`). It prepares the target URL for fetching the route tree structure.


---

### Step 2: `fetchPrefetchResponse`

Next, `fetchPrefetchResponse` executes the network call using underlying fetch utilities. It wraps request creation and configures prefetch priorities without instantly decoding the binary stream.


---

### Step 3: `createFetch`

`createFetch` coordinates the outbound HTTP request. Depending on CDN capabilities and environment parameters, it may invoke cache-busting mechanisms to decorate the outgoing request URL with query parameters derived from headers.


---

### Step 4: `setCacheBustingSearchParam`

To handle static hosting providers and CDNs that ignore custom headers like `Next-Router-Prefetch`, `setCacheBustingSearchParam` computes a unique hash from the request headers and appends it to the URL query string.


---

### Step 5: `computeClientCacheBustingSearchParam`

This function verifies whether the browser environment supports the modern `Crypto` API. If cryptographic digest operations are available via `globalThis.crypto.subtle`, it proceeds with the secure hashing path; otherwise, it falls back to a legacy hashing mechanism.


---

### Step 6: `computeCacheBustingSearchParam`

`computeCacheBustingSearchParam` aggregates relevant prefetch headers (such as prefetch flags, segment prefetch identifiers, state tree paths, and next URLs) into a normalized input string before passing it down for digest computation.


---

### Step 7: `computeCacheBustingSearchParamFromInput`

Using the Web Crypto API, this function computes a SHA-256 hash of the header input string. It truncates the resulting digest to a compact 96-bit window (12 bytes) to keep the generated `_rsc` search query short and clean.


---

### Step 8: `encodeCacheBustingSearchParam`

Finally, `encodeCacheBustingSearchParam` takes the binary digest buffer, converts it to a standard base64 string, and replaces URL-unsafe characters (swapping `+` for `-`, `/` for `_`, and stripping padding `=` characters) to produce a safe cache-busting token.


---

## Architectural Diagrams

### Sequence Diagram

```mermaid
sequenceDiagram
    participant Cache as cache.ts
    participant Reducer as set-cache-busting-search-param.ts
    participant Utils as cache-busting-search-param.ts

    Cache->>Cache: fetchRouteOnCacheMiss()
    Cache->>Cache: fetchPrefetchResponse()
    Cache->>Reducer: setCacheBustingSearchParam(url, headers)
    Reducer->>Reducer: computeClientCacheBustingSearchParam(headers)
    Reducer->>Utils: computeCacheBustingSearchParam(...)
    Utils->>Utils: computeCacheBustingSearchParamFromInput(input)
    Utils->>Utils: encodeCacheBustingSearchParam(bytes)
    Utils-->>Reducer: return hash string
    Reducer->>Reducer: setCacheBustingSearchParamWithHash(url, hash)
    Reducer-->>Cache: URL mutated with query parameter
```


### Flowchart

```mermaid
flowchart TD
    A[Cache Miss] --> B[fetchRouteOnCacheMiss]
    B --> C[fetchPrefetchResponse]
    C --> D[setCacheBustingSearchParam]
    D --> E{Crypto API Available?}
    E -- Yes --> F[computeCacheBustingSearchParam]
    E -- No --> G[computeLegacyCacheBustingSearchParam]
    F --> H[computeCacheBustingSearchParamFromInput]
    H --> I[encodeCacheBustingSearchParam]
    I --> J[Append _rsc Hash to URL]
    G --> J
    J --> K[Execute HTTP Request]
```


---

## Key Observations

- **Cross-Module Boundaries:** This execution path bridges high-level segment cache management (`cache.ts`), router action reducers (`set-cache-busting-search-param.ts`), and lower-level shared URL utilities (`cache-busting-search-param.ts`).
- **Resilience on Static CDNs:** By hashing headers into a URL query parameter (`_rsc`), Next.js bypasses limitations on edge networks and proxies that strip custom routing headers or fail to evaluate `Vary` headers properly.
- **Graceful Fallbacks:** The system checks for the presence of modern cryptographic APIs (`globalThis.crypto.subtle`) and smoothly falls back to legacy hashing strategies if needed, ensuring continuous operation across varied runtime runtimes.

## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/8f4009b0-65bd-4480-9b00-e201f0914bb3/llms.txt) for all pages in this wiki.
