Skip to content

Commit

Permalink
reject redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
feedthejim committed May 1, 2023
1 parent 6057337 commit c64ec58
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../router-reducer-types'
import { addBasePath } from '../../../add-base-path'
import { createHrefFromUrl } from '../create-href-from-url'
import { getRedirectError } from '../../redirect'

type FetchServerActionResult = {
result: ActionFlightData | undefined
Expand Down Expand Up @@ -122,13 +123,8 @@ export function serverActionReducer(
})
}

// TODO: do this instead when experimental React is enabled
// action.reject(getRedirectError(redirectLocation.toString()))

// this is an intentional hack around React: we want to redirect in a new render
setTimeout(() => {
action.navigate(redirectLocation.toString(), 'push', !flightData)
})
// we throw the redirection in the action handler so that it is caught during render
action.reject(getRedirectError(redirectLocation.toString()))
} else {
const [actionResult, flightData] = result ?? [undefined, undefined]
// TODO: populate the prefetch cache with the new flight data
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/app-dir/actions/app/client/page.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use client'

import { useState } from 'react'
import { useState, useTransition } from 'react'

import double, { inc, dec, redirectAction, refreshAction } from './actions'

export default function Counter() {
const [count, setCount] = useState(0)

const [_, startTransition] = useTransition()
return (
<div>
<h1>{count}</h1>
Expand Down Expand Up @@ -40,7 +40,9 @@ export default function Counter() {
<button
id="redirect"
onClick={async () => {
redirectAction('/client/target')
startTransition(async () => {
await redirectAction('/client/target')
})
}}
>
redirect
Expand Down

0 comments on commit c64ec58

Please sign in to comment.