Skip to content

Next.js 13 #22

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

Merged
merged 1 commit into from
Nov 17, 2022
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
87 changes: 84 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@ Reference:

1. `npx create-next-app@latest my-app`

Note that if you elect to use eslint, there are a couple of places you need to add return types to make the default template pass the pre-build checks.

1. `cd` into the new directory (e.g. `cd my-app`)

1. `npm install -D @cloudflare/next-on-pages vercel`

1. Configure the project to use the Edge Runtime:

1. Replace `pages/api/hello.js` with the following:
1. Replace `pages/api/hello.ts` with the following:

```js
```typescript
// Next.js Edge API Routes: https://nextjs.org/docs/api-routes/edge-api-routes
import type { NextRequest } from "next/server";

export const config = {
runtime: "experimental-edge",
};

export default async function (req) {
export default async function handler(
req: NextRequest
): Promise<Response> {
return new Response(JSON.stringify({ name: "John Doe" }), {
status: 200,
headers: {
Expand Down Expand Up @@ -114,6 +119,82 @@ In one terminal, run `npx @cloudflare/next-on-pages --watch`, and in another `np

## Examples

### [Next.js 13's `app` Directory](https://beta.nextjs.org/docs/routing/fundamentals#the-app-directory)

Add the following to `next.config.js`:

```diff
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
runtime: "experimental-edge",
+ appDir: true,
},
reactStrictMode: true,
swcMinify: true,
};

module.exports = nextConfig;
```

If you're following the [Next.js 12 → 13 Upgrade Guide](https://beta.nextjs.org/docs/upgrade-guide#step-4-migrating-pages), delete any `./pages/_app.tsx` and `./pages/index.tsx` files and replace with `./app/layout.tsx` and `./app/page.tsx`:

```typescript
// ./app/layout.tsx
import "../styles/globals.css";
import { FC } from "react";

const RootLayout: FC<{
children: React.ReactNode;
}> = ({
// Layouts must accept a children prop.
// This will be populated with nested layouts or pages
children,
}) => {
return (
<html lang="en">
<body>{children}</body>
</html>
);
};

export default RootLayout;
```

```typescript
// ./app/page.tsx
import { FC } from "react";
import styles from "../styles/Home.module.css";

const Home = async (): Promise<ReturnType<FC>> => {
const uuid = await fetch("https://uuid.rocks/plain").then(
async (response) => await response.text()
);

return (
<div className={styles.container}>
<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</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>
</main>
</div>
);
};

export default Home;
```

### [Edge API Routes](https://nextjs.org/docs/api-routes/edge-api-routes)

```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudflare/next-on-pages",
"version": "0.1.0",
"version": "0.2.0",
"bin": "./bin/index.js",
"scripts": {
"build": "npx esbuild --bundle --platform=node ./src/index.ts --external:esbuild --external:chokidar --outfile=./dist/index.js"
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,23 @@ const transform = async ({
}

for (const entry of functionsEntries) {
if (`pages/${name}` === entry?.name) {
if (
`pages/${name}` === entry?.name ||
`app${name !== "index" ? `/${name}` : ""}/page` === entry?.name
) {
hydratedFunctions.set(name, { matchers: entry.matchers, filepath });
}
}
}

if (hydratedMiddleware.size + hydratedFunctions.size !== functionsMap.size) {
const rscFunctions = [...functionsMap.keys()].filter((name) =>
name.endsWith(".rsc")
);

if (
hydratedMiddleware.size + hydratedFunctions.size !==
functionsMap.size - rscFunctions.length
) {
console.error(
"⚡️ ERROR: Could not map all functions to an entry in the manifest."
);
Expand Down
6 changes: 5 additions & 1 deletion templates/_worker.js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ export default {
let found = false;
for (const matcher of matchers) {
if (matcher.regexp) {
if (pathname.match(new RegExp(matcher?.regexp))) {
const regexp = new RegExp(matcher?.regexp);
if (
pathname.match(regexp) ||
`${pathname}/page`.replace("//page", "/page").match(regexp)
) {
found = true;
break;
}
Expand Down