Skip to content

Commit edb7835

Browse files
committed
feat: add edge support
1 parent 5f12808 commit edb7835

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1861
-48
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
node_modules
33
test
44
lib
5-
demos
5+
demos
6+
src/templates/edge

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = {
1515
'node/exports-style': 0,
1616
'node/global-require': 0,
1717
'node/prefer-global/process': 0,
18+
// Allow a single word inline so that it can do language tags for syntax highlighting
19+
// ['error', { ignorePattern: /^ (\w+) $/ }],
20+
'no-inline-comments': 0,
1821
'no-magic-numbers': 0,
1922
'no-param-reassign': 0,
2023
'no-promise-executor-return': 0,

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@netlify-labs:registry=https://npm.pkg.github.com

.vscode/settings.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"deno.enablePaths": [
3+
"src/templates/edge",
4+
"demos/middleware/.netlify/edge-functions",
5+
"demos/server-components/.netlify/edge-functions",
6+
],
7+
"deno.unstable": true,
8+
"deno.importMap": "demos/server-components/.netlify/edge-functions-import-map.json"
9+
}

demos/default/package.json

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
{
22
"name": "default-demo",
33
"version": "1.0.0",
4-
"private": true,
54
"description": "",
5+
"main": " ",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/netlify/netlify-plugin-nextjs.git"
12-
},
139
"author": "",
1410
"license": "MIT",
15-
"bugs": {
16-
"url": "https://github.com/netlify/netlify-plugin-nextjs/issues"
17-
},
18-
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
1911
"dependencies": {
2012
"@reach/dialog": "^0.16.2",
2113
"@reach/visually-hidden": "^0.16.0"

demos/middleware/.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

demos/middleware/.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel

demos/middleware/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
16+
17+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
18+
19+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

demos/middleware/netlify.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[build]
2+
command = "npm run build"
3+
publish = ".next"
4+
5+
[environment]
6+
NEXT_USE_NETLIFY_EDGE = "true"
7+
8+
[[plugins]]
9+
package = "../../src/index.ts"
10+
11+
[[redirects]]
12+
from = "/_next/static/*"
13+
to = "/static/:splat"
14+
status = 200
15+
16+
[[redirects]]
17+
from = "/*"
18+
to = "/.netlify/functions/___netlify-handler"
19+
status = 200
20+
21+
[functions]
22+
included_files = [
23+
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
24+
"!node_modules/sharp/build/Release/sharp-darwin-*"
25+
]
26+
27+
[dev]
28+
framework = "#static"

demos/middleware/next-env.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

demos/middleware/next.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
}
5+
6+
module.exports = nextConfig

demos/middleware/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "middleware",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint",
10+
"ntl": "ntl-internal"
11+
},
12+
"dependencies": {
13+
"next": "^12.1.5-canary.2",
14+
"react": "18.0.0",
15+
"react-dom": "18.0.0"
16+
},
17+
"devDependencies": {
18+
"@types/react": "^17.0.43",
19+
"eslint": "8.12.0",
20+
"eslint-config-next": "^12.1.5-canary.2"
21+
}
22+
}

demos/middleware/pages/_app.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../styles/globals.css'
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp

demos/middleware/pages/api/hello.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
3+
export default function handler(req, res) {
4+
res.status(200).json({ name: 'John Doe' })
5+
}

demos/middleware/pages/index.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Head from 'next/head'
2+
import Image from 'next/image'
3+
import styles from '../styles/Home.module.css'
4+
5+
export default function Home() {
6+
return (
7+
<div className={styles.container}>
8+
<Head>
9+
<title>Create Next App</title>
10+
<meta name="description" content="Generated by create next app" />
11+
<link rel="icon" href="/favicon.ico" />
12+
</Head>
13+
14+
<main className={styles.main}>
15+
<h1 className={styles.title}>
16+
Welcome to <a href="https://nextjs.org">Next.js!</a>
17+
</h1>
18+
19+
<p className={styles.description}>
20+
Get started by editing{' '}
21+
<code className={styles.code}>pages/index.js</code>
22+
</p>
23+
24+
<div className={styles.grid}>
25+
<a href="https://nextjs.org/docs" className={styles.card}>
26+
<h2>Documentation &rarr;</h2>
27+
<p>Find in-depth information about Next.js features and API.</p>
28+
</a>
29+
30+
<a href="https://nextjs.org/learn" className={styles.card}>
31+
<h2>Learn &rarr;</h2>
32+
<p>Learn about Next.js in an interactive course with quizzes!</p>
33+
</a>
34+
35+
<a
36+
href="https://github.com/vercel/next.js/tree/canary/examples"
37+
className={styles.card}
38+
>
39+
<h2>Examples &rarr;</h2>
40+
<p>Discover and deploy boilerplate example Next.js projects.</p>
41+
</a>
42+
43+
<a
44+
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
45+
className={styles.card}
46+
>
47+
<h2>Deploy &rarr;</h2>
48+
<p>
49+
Instantly deploy your Next.js site to a public URL with Vercel.
50+
</p>
51+
</a>
52+
</div>
53+
</main>
54+
55+
<footer className={styles.footer}>
56+
<a
57+
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
58+
target="_blank"
59+
rel="noopener noreferrer"
60+
>
61+
Powered by{' '}
62+
<span className={styles.logo}>
63+
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
64+
</span>
65+
</a>
66+
</footer>
67+
</div>
68+
)
69+
}

demos/middleware/pages/shows/[id].js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Error from 'next/error'
2+
import Link from 'next/link'
3+
4+
const Show = ({ errorCode, show, env }) => {
5+
// If show item was not found, render 404 page
6+
if (errorCode) {
7+
return <Error statusCode={errorCode} />
8+
}
9+
10+
// Otherwise, render show
11+
return (
12+
<div>
13+
<p>
14+
This page uses getInitialProps() to fetch the show with the ID provided
15+
in the URL: /shows/:id
16+
<br />
17+
Refresh the page to see server-side rendering in action.
18+
<br />
19+
You can also try changing the ID to any other number between 1-10000.
20+
Env: {env}
21+
</p>
22+
<hr />
23+
24+
<h1>Show #{show.id}</h1>
25+
<p>{show.name}</p>
26+
27+
<hr />
28+
29+
<Link href="/">
30+
<a>Go back home</a>
31+
</Link>
32+
</div>
33+
)
34+
}
35+
36+
export const getServerSideProps = async ({ params, req }) => {
37+
// The ID to render
38+
const { id } = params
39+
console.log(req.headers)
40+
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
41+
const data = await res.json()
42+
43+
// Set error code if show item could not be found
44+
const errorCode = res.status > 200 ? res.status : false
45+
46+
return {
47+
props: {
48+
errorCode,
49+
show: data,
50+
env: process.env.HELLO_WORLD || null,
51+
},
52+
}
53+
}
54+
55+
export default Show
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NextResponse } from 'next/server'
2+
import type { NextFetchEvent, NextRequest } from 'next/server'
3+
4+
export async function middleware(req: NextRequest, ev: NextFetchEvent) {
5+
const response = NextResponse.next()
6+
7+
// Set custom header
8+
response.headers.set('x-modified-edge', 'true')
9+
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
10+
11+
// Return response
12+
return response
13+
}

demos/middleware/pages/shows/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Error from 'next/error'
2+
import Link from 'next/link'
3+
4+
const Show = () => {
5+
return (
6+
<div>
7+
<p>Add a number between 1 and 10000 to the URL</p>
8+
<hr />
9+
10+
<Link href="/">
11+
<a>Go back home</a>
12+
</Link>
13+
</div>
14+
)
15+
}
16+
17+
export default Show
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NextResponse } from 'next/server'
2+
import { NextFetchEvent, NextRequest } from 'next/server'
3+
4+
export function middleware(req: NextRequest, ev: NextFetchEvent) {
5+
const url = req.nextUrl.clone()
6+
url.pathname = '/shows/100'
7+
const res = NextResponse.rewrite(url)
8+
res.headers.set('x-modified-in-rewrite', 'true')
9+
return res
10+
}

demos/middleware/public/favicon.ico

25.3 KB
Binary file not shown.

demos/middleware/public/vercel.svg

+4
Loading

0 commit comments

Comments
 (0)