forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create spans for
createServerReference
and registerServerReference
(
vercel#70564) Creating proper source location spans for `createServerReference` and `registerServerReference` is the next step in enabling source mapping of server actions. With the added e2e test app, we can already verify that this works by mocking `findSourceMapURL`. Properly implementing `findSourceMapURL` will be the last missing piece to complete the puzzle. <img width="751" alt="server action go to definition" src="https://github.com/user-attachments/assets/bfe26e92-c497-40d0-ab39-ba0febbd412f"> <img width="751" alt="server action source" src="https://github.com/user-attachments/assets/daaecf4e-0bd7-44ab-b082-68a24cdf33f9"> <img width="1039" alt="source map viz server" src="https://github.com/user-attachments/assets/9b2fc944-29e9-4ed4-82a9-8a27f82e913c"> <img width="1039" alt="source map viz client" src="https://github.com/user-attachments/assets/7fc2f86c-8e74-49e6-be59-9edf5494eb57">
- Loading branch information
1 parent
8894bed
commit a572740
Showing
18 changed files
with
262 additions
and
70 deletions.
There are no files selected for viewing
137 changes: 76 additions & 61 deletions
137
crates/next-custom-transforms/src/transforms/server_actions.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// TODO: Will be implemented later. | ||
export function findSourceMapURL(_filename: string): string | null { | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The main purpose of this end-to-end test app is to allow manual testing of | ||
server action source mapping within the React DevTools. | ||
|
||
Until we have properly implemented `findSourceMapURL` in Next.js, this demo only | ||
works with Turbopack. This is because we can mock `findSourceMapURL` for the | ||
test app, as Turbopack generates source map files, whereas Webpack uses | ||
`eval-source-map`. | ||
|
||
For client bundles, the source map files are served directly through | ||
`/_next/static/chunks`, and for server bundles, the source map files are read | ||
from disk and served through the `/source-maps-turbopack` route handler. | ||
|
||
To check the source mapping of server actions, follow these steps: | ||
|
||
1. Run `pnpm next dev --turbo test/e2e/app-dir/actions-simple`. | ||
2. Go to [http://localhost:3000]() or [http://localhost:3000/client](). | ||
3. Open the Components panel of the React DevTools. | ||
4. Select the `Form` element. | ||
5. In the props section, right-click on the `action` prop and select "Go to | ||
definition" (sometimes it needs two tries). | ||
6. You should end up in the Chrome DevTools Sources panel with the `actions.ts` | ||
file open and the cursor at `foo()`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry } from 'next-test-utils' | ||
|
||
describe('actions-simple', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should work with server actions passed to client components', async () => { | ||
const browser = await next.browser('/') | ||
expect(await browser.elementByCss('p').text()).toBe('initial') | ||
await browser.elementByCss('button').click() | ||
|
||
await retry(async () => { | ||
expect(await browser.elementByCss('p').text()).toBe('result') | ||
}) | ||
}) | ||
|
||
it('should work with server actions imported from client components', async () => { | ||
const browser = await next.browser('/client') | ||
expect(await browser.elementByCss('p').text()).toBe('initial') | ||
await browser.elementByCss('button').click() | ||
|
||
await retry(async () => { | ||
expect(await browser.elementByCss('p').text()).toBe('result') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use server' | ||
|
||
export async function foo() { | ||
return 'result' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use client' | ||
|
||
import { Form } from '../form' | ||
import { foo } from '../actions' | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<main> | ||
<h1>client component page</h1> | ||
<Form action={foo} /> | ||
<Link href="/">server component page</Link> | ||
</main> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use client' | ||
|
||
import { useActionState } from 'react' | ||
|
||
export function Form({ action }: { action: () => Promise<string> }) { | ||
const [result, formAction] = useActionState(action, 'initial') | ||
|
||
return ( | ||
<form action={formAction}> | ||
<button>Submit</button> | ||
<p>{result}</p> | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Form } from './form' | ||
import { foo } from './actions' | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<main> | ||
<h1>server component page</h1> | ||
<Form action={foo} /> | ||
<Link href="/client">client component page</Link> | ||
</main> | ||
) | ||
} |
18 changes: 18 additions & 0 deletions
18
test/e2e/app-dir/actions-simple/app/source-maps-turbopack/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { readFile } from 'fs/promises' | ||
import { NextRequest } from 'next/server' | ||
|
||
// This is for mocking findSourceMapURL until we've implemented it properly. | ||
export async function GET(request: NextRequest): Promise<Response> { | ||
const filename = request.nextUrl.searchParams.get('filename') | ||
|
||
try { | ||
// It's not safe not to sanitize the query param, but it's just for a test. | ||
const sourceMapContents = await readFile(`${filename}.map`) | ||
|
||
return new Response(sourceMapContents) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
|
||
return new Response(null, { status: 404 }) | ||
} |
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/actions-simple/find-source-map-url-turbopack-mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function findSourceMapURL(filename: string): string | null { | ||
if (filename.startsWith(`${document.location.origin}/_next/static`)) { | ||
return `${filename}.map` | ||
} | ||
|
||
const url = new URL('/source-maps-turbopack', document.location.origin) | ||
url.searchParams.set('filename', filename) | ||
|
||
return url.href | ||
} |
4 changes: 4 additions & 0 deletions
4
test/e2e/app-dir/actions-simple/find-source-map-url-webpack-mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function findSourceMapURL(_filename: string): string | null { | ||
// TODO | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
|
||
const nextConfig = { | ||
webpack(config) { | ||
config.resolve.alias['next/dist/client/app-find-source-map-url'] = | ||
require.resolve('./find-source-map-url-webpack-mock.ts') | ||
|
||
return config | ||
}, | ||
experimental: { | ||
turbo: { | ||
resolveAlias: { | ||
'next/dist/client/app-find-source-map-url': | ||
'./find-source-map-url-turbopack-mock.ts', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |