Skip to content

Commit 2e850b9

Browse files
committed
Initial commit
0 parents  commit 2e850b9

File tree

8 files changed

+1502
-0
lines changed

8 files changed

+1502
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.npmrc

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

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# vite-plugin-netlify-edge
2+
3+
This plugin helps add support for generating Netlify Edge Functions. This is mostly intended for frameworks that need to generate a catch-all Edge Function to serve all requests.
4+
5+
By default, it sets `outDir` to `.netlify/edge-functions/handler`, and generates an Edge Functions manifest that defines the `handler` function for all requests.
6+
7+
To help with handling static files, it registers a virtual module called `@static-manifest` that exports a `Set` that includes the paths of all files in `publicDir`. This can be used in the handler to identify requests for static files.
8+
9+
# Usage
10+
11+
Install the plugin:
12+
13+
```shell
14+
npm i -D @netlify-labs/vite-plugin-netlify-edge
15+
```
16+
17+
Then add the following to your `.vite.config.js`:
18+
19+
```js
20+
// vite.config.js
21+
import { defineConfig } from 'vite'
22+
import netlifyEdge from '@netlify-labs/vite-plugin-netlify-edge'
23+
24+
export default defineConfig({
25+
plugins: [netlifyEdge()],
26+
})
27+
```
28+
29+
You can disable any of these features by passing options to the `netlifyEdge()` function:
30+
31+
```js
32+
// vite.config.js
33+
34+
// ...
35+
export default defineConfig({
36+
plugins: [netlifyEdge({ generateEdgeFunctionsManifest: false })],
37+
})
38+
```
39+
40+
You can pass additional static paths to the plugin, so that they are also included. They are paths not filenames, so should include a leading slash and be URL-encoded.
41+
42+
```js
43+
// vite.config.js
44+
import { defineConfig } from 'vite'
45+
import netlifyEdge from '@netlify-labs/vite-plugin-netlify-edge'
46+
import glob from 'fast-glob'
47+
48+
export default defineConfig({
49+
plugins: [
50+
netlifyEdge({
51+
additionalStaticPaths: glob
52+
.sync('**/*.{js,css}', { cwd: 'dist/client' })
53+
.map((path) => `/${encodeURI(path)}`),
54+
}),
55+
],
56+
})
57+
```
58+
59+
If you need to add all paths under a directory then it is likely to be more efficient to check the prefix instead of adding all files individually. See the example below, where every path under `/assets/` is served from the CDN.
60+
61+
In order to use this plugin to create Edge Functions you must define an SSR entrypoint:
62+
63+
```js
64+
// handler.js
65+
import { handleRequest } from 'my-framework'
66+
import staticFiles from '@static-manifest'
67+
68+
export const handler = async (request, { next }) => {
69+
// Handle static files
70+
71+
const { pathname } = new URL(request.url)
72+
73+
// If your framework generates client assets in a subdirectory, you can add these too
74+
if (staticFiles.includes(pathname) || pathname.startsWith('assets/')) {
75+
return next()
76+
}
77+
78+
// "handleRequest" is defined by your framework
79+
try {
80+
return await handleRequest(request)
81+
} catch (err) {
82+
return {
83+
body: 'Internal Server Error',
84+
statusCode: 500,
85+
}
86+
}
87+
}
88+
```
89+
90+
You can then build it using the vite CLI:
91+
92+
```shell
93+
vite build --ssr = handler.js
94+
```
95+
96+
This will generate the Edge Function `.netlify/edge-functions/handler/index.js` and a manifest file `.netlify/edge-functions/manifest.json` that defines the `handler` function.

0 commit comments

Comments
 (0)