Skip to content

feat: add support for Edge Functions #1310

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 22 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 21 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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
test
lib
demos
demos
plugin/src/templates/edge
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
'node/exports-style': 0,
'node/global-require': 0,
'node/prefer-global/process': 0,
// Allow a single word inline so that it can do language tags for syntax highlighting
// ['error', { ignorePattern: /^ (\w+) $/ }],
'no-inline-comments': 0,
'no-magic-numbers': 0,
'no-param-reassign': 0,
'no-promise-executor-return': 0,
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ jobs:
- uses: actions/checkout@v2
- name: Installing with latest Node.js
uses: actions/setup-node@v2
if: "${{ matrix.node-version != '*' }}"
with:
node-version: '*'
check-latest: true
- name: NPM Install
run: npm ci
run: npm install
- name: Switching to Node.js ${{ matrix.node-version }} to run tests
uses: actions/setup-node@v2
if: "${{ matrix.node-version != '*' }}"
Expand Down Expand Up @@ -64,12 +63,11 @@ jobs:
- uses: actions/checkout@v2
- name: Installing with latest Node.js
uses: actions/setup-node@v2
if: "${{ matrix.node-version != '*' }}"
with:
node-version: '*'
check-latest: true
- name: NPM Install
run: npm ci
run: npm install
- name: Install Next.js Canary
run: npm install -D next@canary --legacy-peer-deps
- name: Switching to Node.js ${{ matrix.node-version }} to run tests
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ node_modules
# Test
lib
tsconfig.json
demos/nx-next-monorepo-demo
demos/nx-next-monorepo-demo
plugin/src/templates/edge
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"deno.enablePaths": [
"plugin/src/templates/edge",
"demos/middleware/.netlify/edge-functions",
"demos/server-components/.netlify/edge-functions",
],
"deno.unstable": true,
"deno.importMap": "demos/server-components/.netlify/edge-functions-import-map.json"
}
3 changes: 3 additions & 0 deletions demos/base-path/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "",
"devDependencies": {
"@netlify/plugin-nextjs": "*",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.25",
"husky": "^7.0.4",
"if-env": "^1.0.4",
"npm-run-all": "^4.1.5",
Expand Down
9 changes: 7 additions & 2 deletions demos/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "next build"
},
"repository": {
"type": "git",
Expand All @@ -18,10 +18,15 @@
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
"dependencies": {
"@reach/dialog": "^0.16.2",
"@reach/visually-hidden": "^0.16.0"
"@reach/visually-hidden": "^0.16.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@netlify/plugin-nextjs": "*",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.25",
"husky": "^7.0.4",
"if-env": "^1.0.4",
"npm-run-all": "^4.1.5",
Expand Down
32 changes: 32 additions & 0 deletions demos/middleware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel
29 changes: 29 additions & 0 deletions demos/middleware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.

[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`.

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.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
22 changes: 22 additions & 0 deletions demos/middleware/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build]
command = "npm run build"
publish = ".next"

[environment]
NEXT_USE_NETLIFY_EDGE = "true"

[[plugins]]
package = "../plugin-wrapper/"

# This is a fake plugin, that makes it run npm install
[[plugins]]
package = "@netlify/plugin-local-install-core"

[functions]
included_files = [
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
"!node_modules/sharp/build/Release/sharp-darwin-*"
]

[dev]
framework = "#static"
5 changes: 5 additions & 0 deletions demos/middleware/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
11 changes: 11 additions & 0 deletions demos/middleware/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
}

module.exports = nextConfig
25 changes: 25 additions & 0 deletions demos/middleware/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "middleware",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"ntl": "ntl-internal"
},
"dependencies": {
"next": "^12.1.5-canary.2",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.25",
"@types/react": "^17.0.43",
"husky": "^7.0.4",
"npm-run-all": "^4.1.5",
"typescript": "^4.6.3"
}
}
7 changes: 7 additions & 0 deletions demos/middleware/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}

export default MyApp
5 changes: 5 additions & 0 deletions demos/middleware/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
69 changes: 69 additions & 0 deletions demos/middleware/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'

export default function Home() {
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!</a>
</h1>

<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</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>
)
}
55 changes: 55 additions & 0 deletions demos/middleware/pages/shows/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Error from 'next/error'
import Link from 'next/link'

const Show = ({ errorCode, show, env }) => {
// If show item was not found, render 404 page
if (errorCode) {
return <Error statusCode={errorCode} />
}

// Otherwise, render show
return (
<div>
<p>
This page uses getInitialProps() to fetch the show with the ID provided
in the URL: /shows/:id
<br />
Refresh the page to see server-side rendering in action.
<br />
You can also try changing the ID to any other number between 1-10000.
Env: {env}
</p>
<hr />

<h1>Show #{show.id}</h1>
<p>{show.name}</p>

<hr />

<Link href="/">
<a>Go back home</a>
</Link>
</div>
)
}

export const getServerSideProps = async ({ params, req }) => {
// The ID to render
const { id } = params
console.log(req.headers)
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
const data = await res.json()

// Set error code if show item could not be found
const errorCode = res.status > 200 ? res.status : false

return {
props: {
errorCode,
show: data,
env: process.env.HELLO_WORLD || null,
},
}
}

export default Show
13 changes: 13 additions & 0 deletions demos/middleware/pages/shows/_middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextResponse } from 'next/server'
import type { NextFetchEvent, NextRequest } from 'next/server'

export async function middleware(req: NextRequest, ev: NextFetchEvent) {
const response = NextResponse.next()

// Set custom header
response.headers.set('x-modified-edge', 'true')
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')

// Return response
return response
}
Loading