Skip to content

Commit

Permalink
Use createFromFetch instead of createFromReadableStream to fetch Flig…
Browse files Browse the repository at this point in the history
…ht (#40656)

Small change leveraging the `createFromFetch` method instead.


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
timneutkens committed Sep 18, 2022
1 parent 34df81e commit 1bf7d4d
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions packages/next/client/components/app-router.client.tsx
Expand Up @@ -2,7 +2,7 @@

import type { PropsWithChildren, ReactElement, ReactNode } from 'react'
import React, { useEffect, useMemo, useCallback } from 'react'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { createFromFetch } from 'next/dist/compiled/react-server-dom-webpack'
import {
AppRouterContext,
LayoutRouterContext,
Expand Down Expand Up @@ -32,11 +32,11 @@ import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'
/**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
*/
function fetchFlight(
export function fetchServerResponse(
url: URL,
flightRouterState: FlightRouterState,
prefetch?: true
): ReadableStream {
): Promise<FlightData> {
const flightUrl = new URL(url)
const searchParams = flightUrl.searchParams
// Enable flight response
Expand All @@ -50,26 +50,9 @@ function fetchFlight(
searchParams.append('__flight_prefetch__', '1')
}

// TODO-APP: Verify that TransformStream is supported.
const { readable, writable } = new TransformStream()

fetch(flightUrl.toString()).then((res) => {
res.body?.pipeTo(writable)
})

return readable
}

/**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
*/
export function fetchServerResponse(
url: URL,
flightRouterState: FlightRouterState,
prefetch?: true
): Promise<FlightData> {
const promise = fetch(flightUrl.toString())
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
return createFromReadableStream(fetchFlight(url, flightRouterState, prefetch))
return createFromFetch(promise)
}

/**
Expand Down

0 comments on commit 1bf7d4d

Please sign in to comment.