Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redirect logic missing basePath in App Render #60184

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ async function createRedirectRenderResult(
req: IncomingMessage,
res: ServerResponse,
redirectUrl: string,
basePath: string,
staticGenerationStore: StaticGenerationStore
) {
res.setHeader('x-action-redirect', redirectUrl)
Expand All @@ -158,7 +159,7 @@ async function createRedirectRenderResult(
const host = req.headers['host']
const proto =
staticGenerationStore.incrementalCache?.requestProtocol || 'https'
const fetchUrl = new URL(`${proto}://${host}${redirectUrl}`)
const fetchUrl = new URL(`${proto}://${host}${basePath}${redirectUrl}`)

if (staticGenerationStore.revalidatedTags) {
forwardedHeaders.set(
Expand Down Expand Up @@ -615,6 +616,7 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
req,
res,
redirectUrl,
ctx.renderOpts.basePath,
staticGenerationStore
),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div id="another">Another Page</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Counter } from '../components/counter'
export default function Root({ children }) {
return (
<html>
<body>
<Counter />
{children}
</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { redirect } from 'next/navigation'

async function action() {
'use server'

redirect('/another')
}

export default function Page() {
return (
<form action={action}>
<input type="submit" value="Submit" id="submit-server-action-redirect" />
</form>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client'
import { useState } from 'react'

export function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p id="current-count">Count: {count}</p>
<button onClick={() => setCount(count + 1)} id="increase-count">
Increment
</button>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
basePath: '/base',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const http = require('http')
const { parse } = require('url')
const next = require('next')
const getPort = require('get-port')

async function main() {
const dev = process.env.NEXT_TEST_MODE === 'dev'
process.env.NODE_ENV = dev ? 'development' : 'production'

const port = await getPort()

const app = next({ dev, port })
const handle = app.getRequestHandler()

await app.prepare()

const server = http.createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname } = parsedUrl

if (pathname.startsWith('/base')) {
await handle(req, res, parsedUrl)
} else {
res.end()
}
} catch (err) {
res.statusCode = 500
res.end('Internal Server Error')
}
})

server.once('error', (err) => {
console.error(err)
process.exit(1)
})

server.listen(port, () => {
console.log(`- Local: http://localhost:${port}`)
console.log(`- Next mode: ${dev ? 'development' : process.env.NODE_ENV}`)
})
}

main().catch((err) => {
console.error(err)
process.exit(1)
})
39 changes: 39 additions & 0 deletions test/e2e/app-dir/app-basepath-custom-server/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { retry } from 'next-test-utils'

createNextDescribe(
'custom-app-server-action-redirect',
{
files: join(__dirname, 'custom-server'),
skipDeployment: true,
startCommand: 'node server.js',
dependencies: {
'get-port': '5.1.1',
},
},
({ next }) => {
it('redirects with basepath properly when server action handler uses `redirect`', async () => {
const browser = await next.browser('/base')
const getCount = async () => browser.elementByCss('#current-count').text()

// Increase count to track if the page reloaded
await browser.elementByCss('#increase-count').click().click()
await retry(async () => {
expect(await getCount()).toBe('Count: 2')
})

await browser.elementById('submit-server-action-redirect').click()

expect(await browser.waitForElementByCss('#another').text()).toBe(
'Another Page'
)
expect(await browser.url()).toBe(
`http://localhost:${next.appPort}/base/another`
)

// Count should still be 2 as the browser should not have reloaded the page.
expect(await getCount()).toBe('Count: 2')
})
}
)
Loading