Skip to content

Commit c6b5e6f

Browse files
committed
fix: add demo and use base path
1 parent e07b94b commit c6b5e6f

File tree

9 files changed

+62
-10
lines changed

9 files changed

+62
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
dist
2+
dist
3+
.netlify

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,17 @@ export const handler = async (request, { next }) => {
7979
try {
8080
return await handleRequest(request)
8181
} catch (err) {
82-
return {
83-
body: 'Internal Server Error',
84-
statusCode: 500,
85-
}
82+
return new Response(err.message || 'Internal Server Error', {
83+
status: err.status || 500,
84+
})
8685
}
8786
}
8887
```
8988

9089
You can then build it using the vite CLI:
9190

9291
```shell
93-
vite build --ssr = handler.js
92+
vite build --ssr handler.js
9493
```
9594

9695
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.

demo/handler.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import staticFiles from '@static-manifest'
2+
3+
export const handler = async (request, { next }) => {
4+
// Handle static files
5+
6+
const { pathname } = new URL(request.url)
7+
8+
// If your framework generates client assets in a subdirectory, you can add these too
9+
if (staticFiles.includes(pathname) || pathname.startsWith('assets/')) {
10+
return next()
11+
}
12+
13+
return new Response('Hello World!')
14+
}

demo/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
Hi
11+
</body>
12+
</html>

demo/public/favicon.ico

14.7 KB
Binary file not shown.

demo/vite.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'vite'
2+
import netlifyEdge from '../dist/index.js'
3+
4+
export default defineConfig({
5+
plugins: [netlifyEdge()],
6+
})

netlify.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
publish = "demo/dist"
3+
command = "npm run build:demo"

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"type": "module",
99
"scripts": {
1010
"build": "tsc",
11-
"prepublishOnly": "npm run build"
11+
"prepublishOnly": "npm run build",
12+
"build:demo": "npm run build:client && npm run build:ssr",
13+
"build:client": "vite build demo",
14+
"build:ssr": "vite build demo --ssr handler.js"
1215
},
1316
"devDependencies": {
1417
"@types/node": "^17.0.23",

src/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const netlifyEdge = ({
1414
generateStaticManifest = true,
1515
generateEdgeFunctionsManifest = true,
1616
additionalStaticPaths = [],
17-
}: NetlifyEdgePluginOptions): Plugin => {
17+
}: NetlifyEdgePluginOptions = {}): Plugin => {
1818
let resolvedConfig: ResolvedConfig
1919
let originalPublicDir: string
2020
const staticManifestModuleId = '@static-manifest'
@@ -29,6 +29,20 @@ const netlifyEdge = ({
2929
config.build.outDir ||= edgeFunctionsDir
3030
return {
3131
publicDir: false,
32+
ssr: {
33+
target: 'webworker',
34+
noExternal: true,
35+
},
36+
output: {
37+
format: 'es',
38+
},
39+
build: {
40+
rollupOptions: {
41+
output: {
42+
format: 'es',
43+
},
44+
},
45+
},
3246
}
3347
}
3448
},
@@ -44,9 +58,9 @@ const netlifyEdge = ({
4458
if (generateStaticManifest && id === resolvedStaticManifestModuleId) {
4559
const files = glob
4660
.sync('**/*', {
47-
cwd: path.resolve(originalPublicDir),
61+
cwd: path.resolve(resolvedConfig.root, originalPublicDir),
4862
})
49-
.map((file) => `/${encodeURIComponent(file)}`)
63+
.map((file) => `${resolvedConfig.base}${encodeURIComponent(file)}`)
5064

5165
return `export default new Set(${JSON.stringify([
5266
...files,

0 commit comments

Comments
 (0)