Skip to content

Commit

Permalink
refresh with props
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 10, 2021
1 parent 86c7292 commit bec7d7f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Expand Up @@ -45,7 +45,14 @@ export function getRender({
const renderServerComponentData = isServerComponent
? query.__flight__ !== undefined
: false

const serverComponentProps =
isServerComponent && query.__props__
? JSON.parse(query.__props__)
: undefined

delete query.__flight__
delete query.__props__

const req = {
url: pathname,
Expand Down Expand Up @@ -73,6 +80,7 @@ export function getRender({
supportsDynamicHTML: true,
concurrentFeatures: true,
renderServerComponentData,
serverComponentProps,
serverComponentManifest: isServerComponent ? rscManifest : null,
ComponentMod: null,
}
Expand Down
9 changes: 6 additions & 3 deletions packages/next/client/index.tsx
Expand Up @@ -653,10 +653,13 @@ if (process.env.__NEXT_RSC) {

const rscCache = createResponseCache()

function fetchFlight(href: string) {
function fetchFlight(href: string, props?: any) {
const url = new URL(href, location.origin)
const searchParams = url.searchParams
searchParams.append('__flight__', '1')
if (props) {
searchParams.append('__props__', JSON.stringify(props))
}
return fetch(url.toString())
}

Expand Down Expand Up @@ -700,10 +703,10 @@ if (process.env.__NEXT_RSC) {
const startTransition = React.startTransition
const renrender = () => dispatch({})
// If there is no cache, or there is serialized data already
function refreshCache() {
function refreshCache(nextProps: any) {
startTransition(() => {
const href = getHref()
const response = createFromFetch(fetchFlight(href))
const response = createFromFetch(fetchFlight(href, nextProps))
// FIXME: router.asPath can be different from current location due to navigation
rscCache.set(href, response)
renrender()
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/rsc.ts
@@ -1,6 +1,6 @@
import { createContext, useContext } from 'react'

export const RefreshContext = createContext(() => {})
export const RefreshContext = createContext((_: any) => {})

export function unstable_useRefreshRoot() {
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down
4 changes: 3 additions & 1 deletion packages/next/server/render.tsx
Expand Up @@ -209,6 +209,7 @@ export type RenderOptsPartial = {
resolvedAsPath?: string
serverComponentManifest?: any
renderServerComponentData?: boolean
serverComponentProps?: any
distDir?: string
locale?: string
locales?: string[]
Expand Down Expand Up @@ -356,6 +357,7 @@ export async function renderToHTML(
getServerSideProps,
serverComponentManifest,
renderServerComponentData,
serverComponentProps,
isDataReq,
params,
previewProps,
Expand Down Expand Up @@ -1004,7 +1006,7 @@ export async function renderToHTML(

if (renderServerComponentData) {
const stream: ReadableStream = renderToReadableStream(
<OriginalComponent {...props.pageProps} />,
<OriginalComponent {...props.pageProps} {...serverComponentProps} />,
serverComponentManifest
)
const reader = stream.getReader()
Expand Down

0 comments on commit bec7d7f

Please sign in to comment.