@cfpreview/vercel-to-pages
v0.0.2
Published
## WARNING!
Downloads
685
Readme
Vercel to Pages
WARNING!
This is a crazy super experimental alpha. Do not rely on any security mechanisms firing. We haven't finished implementing all the routing logic yet.
Getting Started
npx create-next-app@latest --typescript --use-npm
- Enter a project name e.g.
my-next-app
- Enter a project name e.g.
cd
into the new directory (e.g.cd my-next-app
)npm install -D @cfpreview/vercel-to-pages-cli vercel
rm -rf pages/api/hello.ts
The default API endpoint they give you is a Node.js one. Easiest to just remove it and start fresh.
npx @cfpreview/vercel-to-pages
npx wrangler pages project create
- Enter a project name e.g.
my-next-app-on-pages
- Enter a project name e.g.
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/pages/projects/$PROJECT_NAME -H 'X-Auth-Email: $ACCOUNT_EMAIL_ADDRESS' -H 'X-Auth-Key: $ACCOUNT_API_KEY' -X PATCH -d '{"deployment_configs":{"production":{"compatibility_date": "2022-08-16", "compatibility_flags": ["transformstream_enable_standard_constructor","streams_enable_constructors"]},"preview":{"compatibility_date": "2022-08-16", "compatibility_flags": ["transformstream_enable_standard_constructor","streams_enable_constructors"]}}}'
npx wrangler pages publish .vercel/output/static
What now?
Add some dynamic functionality to your app! Export the following on your routes:
export const config = {
runtime: "experimental-edge",
};
Edge API Routes
Next.js supports Edge API Routes:
import type { NextRequest } from "next/server";
export const config = {
runtime: "experimental-edge",
};
export default async function handler(req: NextRequest) {
return new Response(
JSON.stringify({
name: process.env.NEXT_RUNTIME,
}),
{
status: 200,
headers: {
"content-type": "application/json",
},
}
);
}
SSR Routes
getServerSideProps()
can be evaluated in Workers:
// ./pages/ssr.tsx
import type { NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";
export const config = {
runtime: "experimental-edge",
};
export const getServerSideProps = async () => {
return {
props: {
runtime: process.env.NEXT_RUNTIME,
uuid: await fetch("https://uuid.rocks/plain").then((response) =>
response.text()
),
},
};
};
const Home: NextPage<{ runtime: string; uuid: string }> = ({
runtime,
uuid,
}) => {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>
Welcome to{" "}
<a href="https://nextjs.org">Next.js, running at the {runtime}!</a>
</h1>
<p className={styles.description}>
Get started by editing{" "}
<code className={styles.code}>pages/index.tsx</code>
</p>
<p className={styles.description}>
Here's a server-side UUID:
<code className={styles.code}>{uuid}</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation →</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn →</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h2>Examples →</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h2>Deploy →</h2>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{" "}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
);
};
export default Home;
Middleware
Middleware is likely also be possible, but we haven't connected it up yet.