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

Next.js 14 example w/ EF cloud function fetching #99

Merged
merged 4 commits into from
Nov 30, 2023
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
14 changes: 4 additions & 10 deletions examples/v7-ef-cloud-fetch/edge-functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ export async function handleHttpRequest(request) {
: undefined;

// Perform a fetch request to the original request URL with the same method, headers, and body.
// Specify the 'edgio_serverless' as the origin to fetch the original Cloud Functions response.
console.log('fetch', {
url: request.url,
method: request.method,
headers: request.headers,
body: requestBody,
});
// Specify 'edgio_serverless' as the origin to fetch the original Cloud Functions response.
const cloudFunctionsResponse = await fetch(request.url, {
edgio: { origin: 'edgio_serverless' },
method: request.method,
Expand All @@ -23,9 +17,9 @@ export async function handleHttpRequest(request) {
// Convert the response to text format.
let responseText = await cloudFunctionsResponse.text();

// Replace certain phrases in the response text to indicate processing by Edge Functions.
responseText = responseText.replace(/cloud functions/i, 'Edge Functions');
responseText = responseText.replace(/rendered by/i, 'changed by');
// // Replace certain phrases in the response text to indicate processing by Edge Functions.
responseText = responseText.replace(/cloud functions/gi, 'Edge Functions');
responseText = responseText.replace(/rendered by/gi, 'changed by');

// Return a new response with the modified text and original response status, status text, and headers.
return new Response(responseText, {
Expand Down
120 changes: 120 additions & 0 deletions examples/v7-ef-cloud-fetch/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/v7-ef-cloud-fetch/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export default new Router()
// NextRoutes automatically adds routes for all Next.js pages and their assets
.use(nextRoutes)
.match('/edge-override', {
edge_function: 'edge-functions/index.js',
edge_function: './edge-functions/index.js',
});
13 changes: 9 additions & 4 deletions examples/v7-ef-cloud-fetch/src/app/edge-override/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const EdgioCloudFunctionsPage = () => {
import { headers } from 'next/headers';

export default async function EdgioCloudFunctionsPage() {
// add in reference to headers() to force this page to be server-side rendered
const headersList = headers();
const referer = headersList.get('referer');

return (
<div
style={{
Expand All @@ -11,9 +17,8 @@ const EdgioCloudFunctionsPage = () => {
<div>
<h1>Edgio Cloud Functions</h1>
<p>This page was rendered by Edgio Cloud Functions.</p>
<div>Referer: {referer}</div>
</div>
</div>
);
};

export default EdgioCloudFunctionsPage;
}
4 changes: 2 additions & 2 deletions examples/v7-ef-cloud-fetch/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import './globals.css'
const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
title: 'Create Next App',
title: 'Edge Functions Cloud Fetch Example',
description: 'Generated by create next app',
}
};

export default function RootLayout({
children,
Expand Down
141 changes: 37 additions & 104 deletions examples/v7-ef-cloud-fetch/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,46 @@
import Image from 'next/image'

export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">src/app/page.tsx</code>
<main className="flex min-h-screen flex-col items-center justify-center p-24">
<div className="z-10 max-w-5xl w-full text-center font-mono text-sm">
<p className="mb-4 text-left mx-auto">
Welcome! This page demonstrates how Edgio Edge Functions can be used
with Next.js to modify cloud function responses at the edge. By
visiting the link below, the following request flow will occur:
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{' '}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>

<div className="relative flex place-items-center before:absolute before:h-[300px] before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 before:lg:h-[360px] z-[-1]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>

<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Docs{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Find in-depth information about Next.js features and API.
</p>
</a>

<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Learn{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>

<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Templates{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Explore starter templates for Next.js.
</p>
</a>

<ol className="list-decimal list-inside text-left my-4">
<li>
The browser will request the page{' '}
<code className="bg-gray-700 p-1 rounded">/edge-override</code>.
</li>
<li>
Edgio will match this request as defined in the{' '}
<code className="bg-gray-700 p-1 rounded">routes.ts</code> file and
process it through{' '}
<code className="bg-gray-700 p-1 rounded">
./edge-functions/index.js
</code>
.
</li>
<li>
The edge function will request the page from the Next.js server
cloud function.
</li>
<li>
The Next.js server will render the page and respond to the edge
function.
</li>
<li>
The edge function will modify the response and send it to the
browser.
</li>
</ol>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
href="/edge-override"
className="inline-block bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Deploy{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
Go to Edge Override
</a>
</div>
</main>
)
);
}
8 changes: 7 additions & 1 deletion examples/v7-ef-cloud-fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"edge-functions/index.js"
],
"exclude": ["node_modules"]
}
Loading