Unverified Commit aec49ec6 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

actions: fix redirect on the edge (#49197)

- enables redirect for the edge
- enables production tests



link NEXT-1086
parent dc99c18e
......@@ -318,9 +318,6 @@ export async function handleAction({
return actionResult
} catch (err) {
if (isRedirectError(err)) {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new Error('Invariant: not implemented.')
}
const redirectUrl = getURLFromRedirectError(err)
// if it's a fetch action, we don't want to mess with the status code
......
......@@ -255,7 +255,7 @@ export async function adapter(
* the incoming request was a data request.
*/
const redirect = response?.headers.get('Location')
if (response && redirect) {
if (response && redirect && !isEdgeRendering) {
const redirectURL = new NextURL(redirect, {
forceLocale: false,
headers: params.request.headers,
......
......@@ -9,7 +9,6 @@ createNextDescribe(
'app-dir action handling',
{
files: __dirname,
skipDeployment: true,
},
({ next, isNextDev }) => {
it('should handle basic actions correctly', async () => {
......@@ -229,6 +228,35 @@ createNextDescribe(
return browser.elementByCss('h1').text()
}, 'Prefix: HELLO, WORLD')
})
it('should handle redirect to a relative URL in a single pass', async () => {
const browser = await next.browser('/client/edge')
await new Promise((resolve) => {
setTimeout(resolve, 3000)
})
let requests = []
browser.on('request', (req: Request) => {
requests.push(new URL(req.url()).pathname)
})
await browser.elementByCss('#redirect').click()
// no other requests should be made
expect(requests).toEqual(['/client/edge'])
})
it('should handle regular redirects', async () => {
const browser = await next.browser('/client/edge')
await browser.elementByCss('#redirect-external').click()
await check(async () => {
return browser.eval('window.location.toString()')
}, 'https://example.com/')
})
})
describe('fetch actions', () => {
......
'use client'
import { useState } from 'react'
import double, { inc, dec, redirectAction } from '../actions'
export default function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<h1>{count}</h1>
<button
id="inc"
onClick={async () => {
const newCount = await inc(count)
setCount(newCount)
}}
>
+1
</button>
<button
id="dec"
onClick={async () => {
const newCount = await dec(count)
setCount(newCount)
}}
>
-1
</button>
<button
id="double"
onClick={async () => {
const newCount = await double(count)
setCount(newCount)
}}
>
*2
</button>
<form>
<button
id="redirect"
formAction={() => redirectAction('/redirect-target')}
>
redirect
</button>
</form>
<form>
<button
id="redirect-external"
formAction={() => redirectAction('https://example.com')}
>
redirect external
</button>
</form>
</div>
)
}
export const runtime = 'edge'
import { getCookie, getHeader, setCookie } from '../actions'
import {
getCookie,
getHeader,
setCookie,
setCookieAndRedirect,
} from '../actions'
import UI from '../ui'
import { validator } from './validator'
......@@ -9,6 +14,7 @@ export default function Page() {
getCookie={getCookie}
getHeader={getHeader}
setCookie={setCookie}
setCookieAndRedirect={setCookieAndRedirect}
getAuthedUppercase={validator(async (str) => {
'use server'
return prefix + ' ' + str.toUpperCase()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment