Skip to content

Commit

Permalink
[next] ensure action outputs are created for edge functions (#11568)
Browse files Browse the repository at this point in the history
Since we're rewriting `next-action` requests to `.action` outputs, we need to ensure that we also duplicate the edge function handler for it. 

This runs all of the existing Node runtime tests in the Edge runtime as well. However, there's a discrepancy in how `x-vercel-cache` is returned, so for now we instead validate that its responding with `x-edge-runtime`.
  • Loading branch information
ztanner committed May 9, 2024
1 parent 3023122 commit 11584b0
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 128 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-cooks-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vercel/next': patch
---

ensure `.action` outputs are created for edge functions
4 changes: 4 additions & 0 deletions packages/next/src/server-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,10 @@ export async function serverBuild({
edgeFunctions[`${pathname}${RSC_PREFETCH_SUFFIX}`] =
edgeFunctions[pathname];
}

if (hasActionOutputSupport) {
edgeFunctions[`${pathname}.action`] = edgeFunctions[pathname];
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { useState } from 'react';
import { increment } from '../../../../actions';

export default function Page() {
const [count, setCount] = useState(0);
async function updateCount() {
const newCount = await increment(count);
setCount(newCount);
}

return (
<form action={updateCount}>
<div id="count">{count}</div>
<button>Submit</button>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-dynamic';

export default function Layout({ children }) {
return children;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { useState } from 'react';
import { increment } from '../../../../actions';

export default function Page() {
const [count, setCount] = useState(0);
async function updateCount() {
const newCount = await increment(count);
setCount(newCount);
}

return (
<form action={updateCount}>
<div id="count">{count}</div>
<button>Submit</button>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-static';

export default function Layout({ children }) {
return children;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { useState } from 'react';
import { increment } from '../../../actions';

export default function Page() {
const [count, setCount] = useState(0);
async function updateCount() {
const newCount = await increment(count);
setCount(newCount);
}

return (
<form action={updateCount}>
<div id="count">{count}</div>
<button>Submit</button>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const runtime = 'edge';

export default function Layout({ children }) {
return <div>{children}</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use client';

// @ts-ignore
import { useCallback, useState } from 'react';

function request(method) {
return fetch('/api/test', {
method,
headers: {
'content-type': 'multipart/form-data;.*',
},
});
}

export default function Home() {
const [result, setResult] = useState('Press submit');
const onClick = useCallback(async method => {
const res = await request(method);
const text = await res.text();

setResult(res.ok ? `${method} ${text}` : 'Error: ' + res.status);
}, []);

return (
<>
<div className="flex flex-col items-center justify-center h-screen">
<div className="flex flex-row space-x-2 items-center justify-center">
<button
className="border border-white rounded-sm p-4 mb-4"
onClick={() => onClick('GET')}
>
Submit GET
</button>
<button
className="border border-white rounded-sm p-4 mb-4"
onClick={() => onClick('POST')}
>
Submit POST
</button>
</div>
<div className="text-white">{result}</div>
</div>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-dynamic';

export default function Layout({ children }) {
return children;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { revalidatePath } from 'next/cache';

export default async function Page() {
async function serverAction() {
'use server';
await new Promise(resolve => setTimeout(resolve, 1000));
revalidatePath('/dynamic');
}

return (
<form action={serverAction}>
<button>Submit</button>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { revalidatePath } from 'next/cache';

export default async function Page() {
async function serverAction() {
'use server';
await new Promise(resolve => setTimeout(resolve, 1000));
revalidatePath('/dynamic');
}

return (
<form action={serverAction}>
<button>Submit</button>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { revalidatePath } from 'next/cache';

export default async function Page() {
async function serverAction() {
'use server';
await new Promise(resolve => setTimeout(resolve, 1000));
revalidatePath('/dynamic');
}

return (
<form action={serverAction}>
<button>Submit</button>
</form>
);
}

export function generateStaticParams() {
return [{ slug: 'pre-generated' }];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-static';

export default function Layout({ children }) {
return children;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { revalidatePath } from 'next/cache'

export default async function Page() {
async function serverAction() {
'use server';
await new Promise((resolve) => setTimeout(resolve, 1000));
revalidatePath('/dynamic');
}

return (
<form action={serverAction}>
<button>Submit</button>
</form>
);
}
Loading

0 comments on commit 11584b0

Please sign in to comment.