Skip to content

Commit

Permalink
Add server-actions and RSC example
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiaslins committed Jul 28, 2023
1 parent f56294f commit 1825c2e
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 38 deletions.
16 changes: 16 additions & 0 deletions apps/nextjs/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
10 changes: 10 additions & 0 deletions apps/nextjs/app/rsc/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { headers } from 'next/headers';
import { track } from '@vercel/analytics/server';

export default async function RSC() {
track('Viewed Experiment', undefined, {
headers: headers(),
});

return <div>I did track a server action on render</div>;
}
26 changes: 26 additions & 0 deletions apps/nextjs/app/server-actions/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { headers } from 'next/headers';
import { track } from '@vercel/analytics/server';

export default function FeedbackPage() {
async function submitFeedback(data: FormData) {
'use server';

await track(
'Feedback',
{
message: data.get('feedback') as string,
},
{
// If we pass headers, the event will be connected to the page views/session
headers: headers(),
},
);
}

return (
<form action={submitFeedback}>
<input type="text" name="feedback" placeholder="Feedback" />
<button type="submit">Submit Feedback</button>
</form>
);
}
8 changes: 8 additions & 0 deletions apps/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverActions: true,
},
};

module.exports = nextConfig;
2 changes: 1 addition & 1 deletion apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@vercel/analytics": "workspace:*",
"next": "^13.4.7",
"next": "^13.4.12",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand Down
Loading

0 comments on commit 1825c2e

Please sign in to comment.