@cfpreview/vercel-to-pages

0.0.2 • Public • Published

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

  1. npx create-next-app@latest --typescript --use-npm

    1. Enter a project name e.g. my-next-app
  2. cd into the new directory (e.g. cd my-next-app)

  3. npm install -D @cfpreview/vercel-to-pages-cli vercel

  4. 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.

  5. npx @cfpreview/vercel-to-pages

  6. npx wrangler pages project create

    1. Enter a project name e.g. my-next-app-on-pages
  7. 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"]}}}'

  8. 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&apos;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 &rarr;</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 &rarr;</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 &rarr;</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 &rarr;</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.

Readme

Keywords

none

Package Sidebar

Install

npm i @cfpreview/vercel-to-pages

Weekly Downloads

168

Version

0.0.2

License

none

Unpacked Size

20.7 kB

Total Files

8

Last publish

Collaborators

  • gregbrimble
  • 1000hz
  • geelen