Skip to content

Commit 3beb85c

Browse files
committed
fix: backport fix from 2f24fb1 to @netlify/remix-edge-adapter
Turns out we had the same problem here as well.
1 parent 47dd051 commit 3beb85c

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

packages/remix-edge-adapter/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,31 @@ However, if you are using **Remix Vite**, you can instead deploy your existing s
1616

1717
```js
1818
// vite.config.js
19-
import { vitePlugin as remix } from "@remix-run/dev";
20-
import { netlifyPlugin } from "@netlify/remix-edge-adapter/plugin";
19+
import { vitePlugin as remix } from '@remix-run/dev'
20+
import { netlifyPlugin } from '@netlify/remix-edge-adapter/plugin'
2121

2222
export default defineConfig({
23-
plugins: [remix(), netlifyPlugin(),
24-
});
23+
plugins: [remix(), netlifyPlugin()],
24+
})
25+
```
26+
27+
If you have your own Netlify Functions (typically in `netlify/functions`) for which you've configured a `path`, you must
28+
exclude those paths to avoid conflicts with the generated Remix SSR handler, which would otherwise run on all dynamic
29+
paths:
30+
31+
```js
32+
// vite.config.js
33+
import { vitePlugin as remix } from '@remix-run/dev'
34+
import { netlifyPlugin } from '@netlify/remix-edge-adapter/plugin'
35+
36+
export default defineConfig({
37+
plugins: [
38+
remix(),
39+
netlifyPlugin({
40+
excludedPaths: ['/ping', '/api/*', '/webhooks/*'],
41+
}),
42+
],
43+
})
2544
```
2645

2746
3. Add an `app/entry.jsx` (.tsx if using TypeScript) with these contents:

packages/remix-edge-adapter/src/vite/plugin.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default createRequestHandler({
8585

8686
// This is written to the edge functions directory. It just re-exports
8787
// the compiled entrypoint, along with the Netlify function config.
88-
function generateEdgeFunction(handlerPath: string, exclude: Array<string> = []) {
88+
function generateEdgeFunction(handlerPath: string, excludedPath: Array<string>) {
8989
return /* js */ `
9090
export { default } from "${handlerPath}";
9191
@@ -94,7 +94,7 @@ function generateEdgeFunction(handlerPath: string, exclude: Array<string> = [])
9494
generator: "${name}@${version}",
9595
cache: "manual",
9696
path: "/*",
97-
excludedPath: ${JSON.stringify(exclude)},
97+
excludedPath: ${JSON.stringify(excludedPath)},
9898
};`
9999
}
100100

@@ -125,7 +125,21 @@ const getEdgeFunctionHandlerModuleId = async (root: string, isHydrogenSite: bool
125125
return findUserEdgeFunctionHandlerFile(root)
126126
}
127127

128-
export function netlifyPlugin(): Plugin {
128+
export interface NetlifyPluginOptions {
129+
/**
130+
* Paths to exclude from being handled by the Remix handler.
131+
*
132+
* @IMPORTANT If you have your own Netlify Functions running on custom `path`s, you
133+
* must exclude those paths here to avoid conflicts.
134+
*
135+
* @type {string[]}
136+
* @default []
137+
*/
138+
excludedPaths?: string[]
139+
}
140+
141+
export function netlifyPlugin(options: NetlifyPluginOptions = {}): Plugin {
142+
const additionalExcludedPaths = options.excludedPaths ?? []
129143
let resolvedConfig: ResolvedConfig
130144
let currentCommand: string
131145
let isSsr: boolean | undefined
@@ -264,23 +278,25 @@ export function netlifyPlugin(): Plugin {
264278
async writeBundle() {
265279
// Write the server entrypoint to the Netlify functions directory
266280
if (currentCommand === 'build' && isSsr) {
267-
const exclude: Array<string> = ['/.netlify/*']
281+
const excludedPath: Array<string> = ['/.netlify/*']
268282
try {
269283
// Get the client files so we can skip them in the edge function
270284
const clientDirectory = join(resolvedConfig.build.outDir, '..', 'client')
271285
const entries = await readdir(clientDirectory, { withFileTypes: true })
272286
for (const entry of entries) {
273287
// With directories we don't bother to recurse into it and just skip the whole thing.
274288
if (entry.isDirectory()) {
275-
exclude.push(`/${entry.name}/*`)
289+
excludedPath.push(`/${entry.name}/*`)
276290
} else if (entry.isFile()) {
277-
exclude.push(`/${entry.name}`)
291+
excludedPath.push(`/${entry.name}`)
278292
}
279293
}
280294
} catch {
281295
// Ignore if it doesn't exist
282296
}
283297

298+
excludedPath.push(...additionalExcludedPaths)
299+
284300
const edgeFunctionsDirectory = join(resolvedConfig.root, NETLIFY_EDGE_FUNCTIONS_DIR)
285301

286302
await mkdir(edgeFunctionsDirectory, { recursive: true })
@@ -290,7 +306,7 @@ export function netlifyPlugin(): Plugin {
290306

291307
await writeFile(
292308
join(edgeFunctionsDirectory, EDGE_FUNCTION_FILENAME),
293-
generateEdgeFunction(relativeHandlerPath, exclude),
309+
generateEdgeFunction(relativeHandlerPath, excludedPath),
294310
)
295311
}
296312
},

tests/e2e/fixtures/edge-site/vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ import tsconfigPaths from "vite-tsconfig-paths";
44
import { netlifyPlugin } from "@netlify/remix-edge-adapter/plugin";
55

66
export default defineConfig({
7-
plugins: [remix(), netlifyPlugin(), tsconfigPaths()],
7+
plugins: [
8+
remix(),
9+
netlifyPlugin({ excludedPaths: ["/please-blorble", "/purge-cdn"] }),
10+
tsconfigPaths(),
11+
],
812
});

0 commit comments

Comments
 (0)