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

Double RSC fetch after server action redirect #66166

Closed
cfrank opened this issue May 24, 2024 · 3 comments
Closed

Double RSC fetch after server action redirect #66166

cfrank opened this issue May 24, 2024 · 3 comments
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@cfrank
Copy link
Contributor

cfrank commented May 24, 2024

Link to the code that reproduces this issue

https://github.com/cfrank/next.js

To Reproduce

  1. Clone NextJS canary
  2. pnpm i
  3. pnpm build
  4. NEXT_TEST_SKIP_CLEANUP=1 pnpm test-start test/e2e/app-dir/actions/app-action.test.ts
  5. Visit temporary next install directory (/tmp/next-install-*)
  6. Run pnpm next
  7. Visit http://localhost:3000/client
  8. Click "redirect"

Current vs. Expected behavior

Currently when performing a redirect within a server action we are correctly resolving the RSC bundle within the POST request and performing the client side redirect to the destination path. But upon loading the destination path we are performing a subsequent fetch of the destination path:

image

Provide environment information

Operating System:
  Platform: Linux
  Arch: amd64
  Version: `6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux`

Binaries:
  Node: `v21.7.3`
  NPM: `10.5.0`
  pnpm: `8.15.7`

Relevant Packages:
  NextJS: `v14.3.0-canary.80`

Which area(s) are affected? (Select all that apply)

Navigation

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Other (Deployed)

Additional context

I encountered this bug while working on #64604 where I added more robust tests to verify that only a single request was being made during server action redirects. When I opened the PR the tests were passing and only a single POST request was being made with all relevant data being shipped within that single request, but shortly after a change was landed which introduced this second request.

@cfrank cfrank added the bug Issue was opened via the bug report template. label May 24, 2024
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label May 24, 2024
@icyJoseph
Copy link
Contributor

icyJoseph commented May 24, 2024

This has been reported here as well:

Since the _rsc request is discarded, in light of a redirect, the tests setup in test/e2e/app-dir/navigation/navigation.test.ts pass, but the issue is there too.

For example, say, you have this route handler:

// test/e2e/app-dir/navigation/app/redirect/result/api/route.js
let t = 0
const getTime = () => t++

export const GET = (req) => {
  const rsc = req.nextUrl.searchParams.get('_rsc')
  return new Response(
    `<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>

    <body>
        <h1 id="result-page">Result Page</h1>
        <div id="counter">${getTime()}</div>
        <div id="rsc">${rsc || 'no-rsc'}</div>
    </body>
</html>
  `,
    { status: 200, statusText: 'OK', headers: { 'Content-Type': 'text/html' } }
  )
}

Then you can write a test:

// test/e2e/app-dir/navigation/navigation.test.ts around line 500-ish
   it('should only trigger the redirect once for Route Handlers', async () => {
        const path = '/redirect/redirect-with-loading/to-api'
        const browser = await next.browser(path)

        const counter = await browser.waitForElementByCss('#counter').text()

        expect(counter).toBe('0')
      })

And that'll error out because counter will be actually 1.

I haven't had time to deep dive into why the _rsc request is made though

@cfrank
Copy link
Contributor Author

cfrank commented Jun 8, 2024

Fixed #66620

@cfrank cfrank closed this as completed Jun 8, 2024
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

2 participants