Skip to content

Commit d39ce27

Browse files
authored
feat: add support for Edge Functions (#1310)
* refactor: move into npm workspace * fix: correct tsconfig * chore: use plugin wrapper * ci: always install using latest node * feat: add edge support * chore: don't use Next to lint demos * fix: remove image redirect if using edge * chore: move plugin into subdirectory * chore: fix workspace * chore: fix test * chore: add typescript devdep * fix: switch to early return for static assets * chore: remove npmrc * chore: update name * chore: fix template path * chore: revert unneeded changes * ci: fix test script * chore: regenerate package-lock * chore: dedupe react * chore: update demos * chore: more devdeps for demos * chore: add dep on plugin
1 parent 78ec8e7 commit d39ce27

Some content is hidden

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

71 files changed

+8608
-6407
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+
plugin/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,

.github/workflows/test.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ jobs:
2727
- uses: actions/checkout@v2
2828
- name: Installing with latest Node.js
2929
uses: actions/setup-node@v2
30-
if: "${{ matrix.node-version != '*' }}"
3130
with:
3231
node-version: '*'
3332
check-latest: true
3433
- name: NPM Install
35-
run: npm ci
34+
run: npm install
3635
- name: Switching to Node.js ${{ matrix.node-version }} to run tests
3736
uses: actions/setup-node@v2
3837
if: "${{ matrix.node-version != '*' }}"
@@ -64,12 +63,11 @@ jobs:
6463
- uses: actions/checkout@v2
6564
- name: Installing with latest Node.js
6665
uses: actions/setup-node@v2
67-
if: "${{ matrix.node-version != '*' }}"
6866
with:
6967
node-version: '*'
7068
check-latest: true
7169
- name: NPM Install
72-
run: npm ci
70+
run: npm install
7371
- name: Install Next.js Canary
7472
run: npm install -D next@canary --legacy-peer-deps
7573
- name: Switching to Node.js ${{ matrix.node-version }} to run tests

.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ node_modules
2020
# Test
2121
lib
2222
tsconfig.json
23-
demos/nx-next-monorepo-demo
23+
demos/nx-next-monorepo-demo
24+
plugin/src/templates/edge

.vscode/settings.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"deno.enablePaths": [
3+
"plugin/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/base-path/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "",
55
"devDependencies": {
66
"@netlify/plugin-nextjs": "*",
7+
"@types/fs-extra": "^9.0.13",
8+
"@types/jest": "^27.4.1",
9+
"@types/node": "^17.0.25",
710
"husky": "^7.0.4",
811
"if-env": "^1.0.4",
912
"npm-run-all": "^4.1.5",

demos/default/package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"description": "",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"build": "next build"
88
},
99
"repository": {
1010
"type": "git",
@@ -18,10 +18,15 @@
1818
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
1919
"dependencies": {
2020
"@reach/dialog": "^0.16.2",
21-
"@reach/visually-hidden": "^0.16.0"
21+
"@reach/visually-hidden": "^0.16.0",
22+
"react": "^18.0.0",
23+
"react-dom": "^18.0.0"
2224
},
2325
"devDependencies": {
2426
"@netlify/plugin-nextjs": "*",
27+
"@types/fs-extra": "^9.0.13",
28+
"@types/jest": "^27.4.1",
29+
"@types/node": "^17.0.25",
2530
"husky": "^7.0.4",
2631
"if-env": "^1.0.4",
2732
"npm-run-all": "^4.1.5",

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

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

demos/middleware/netlify.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[build]
2+
command = "npm run build"
3+
publish = ".next"
4+
5+
[environment]
6+
NEXT_USE_NETLIFY_EDGE = "true"
7+
8+
[[plugins]]
9+
package = "../plugin-wrapper/"
10+
11+
# This is a fake plugin, that makes it run npm install
12+
[[plugins]]
13+
package = "@netlify/plugin-local-install-core"
14+
15+
[functions]
16+
included_files = [
17+
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
18+
"!node_modules/sharp/build/Release/sharp-darwin-*"
19+
]
20+
21+
[dev]
22+
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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
eslint: {
5+
// Warning: This allows production builds to successfully complete even if
6+
// your project has ESLint errors.
7+
ignoreDuringBuilds: true,
8+
},
9+
}
10+
11+
module.exports = nextConfig

demos/middleware/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
"ntl": "ntl-internal"
10+
},
11+
"dependencies": {
12+
"next": "^12.1.5-canary.2",
13+
"react": "18.0.0",
14+
"react-dom": "18.0.0"
15+
},
16+
"devDependencies": {
17+
"@netlify/plugin-nextjs": "*",
18+
"@types/fs-extra": "^9.0.13",
19+
"@types/jest": "^27.4.1",
20+
"@types/node": "^17.0.25",
21+
"@types/react": "^17.0.43",
22+
"husky": "^7.0.4",
23+
"npm-run-all": "^4.1.5",
24+
"typescript": "^4.6.3"
25+
}
26+
}

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+
}

0 commit comments

Comments
 (0)