Skip to content

Commit cce9c0e

Browse files
committed
chore: refactor
1 parent 904bd05 commit cce9c0e

File tree

6 files changed

+53
-49
lines changed

6 files changed

+53
-49
lines changed

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"deno.enablePaths": [
33
"packages/runtime/src/templates/edge",
44
"demos/middleware/.netlify/edge-functions",
5-
"demos/middleware/netlify/edge-functions",
65
"demos/server-components/.netlify/edge-functions",
76
],
87
"deno.unstable": true

demos/middleware/netlify.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ command = "npm run build"
33
publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

6-
[build.environment]
7-
NEXT_USE_NETLIFY_EDGE = "true"
8-
9-
[[plugins]]
10-
package = "../plugin-wrapper/"
11-
6+
# [[plugins]]
7+
# package = "../plugin-wrapper/"
128
# This is a fake plugin, that makes it run npm install
139
[[plugins]]
1410
package = "@netlify/plugin-local-install-core"
@@ -18,6 +14,7 @@ included_files = [
1814
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
1915
"!node_modules/sharp/build/Release/sharp-darwin-*"
2016
]
17+
# Uncomment this if testing the built files rather than dev
2118
# [dev]
2219
# framework = "#static"
2320
# [[redirects]]

demos/middleware/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
},
1818
"exclude": [
1919
"node_modules",
20-
"netlify/edge-functions/**/*",
2120
"../../src/templates/edge/*"
2221
],
2322
"include": [

packages/next/src/middleware/request.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ export class MiddlewareRequest extends Request {
5050
}
5151

5252
/**
53-
*
54-
* @param options
55-
* @returns
53+
* Passes the request to the origin, allowing you to access the response
5654
*/
5755
async next(options?: NextOptions): Promise<MiddlewareResponse> {
5856
this.applyHeaders()

packages/runtime/src/helpers/dev.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { resolve } from 'path'
2+
3+
import { NetlifyPlugin } from '@netlify/build'
4+
import execa from 'execa'
5+
import { unlink, existsSync } from 'fs-extra'
6+
7+
import { writeDevEdgeFunction } from './edge'
8+
import { patchNextFiles } from './files'
9+
10+
// The types haven't been updated yet
11+
export const onPreDev: NetlifyPlugin['onPreBuild'] = async ({ constants, netlifyConfig }) => {
12+
// Need to patch the files, because build might not have been run
13+
await patchNextFiles(resolve(netlifyConfig.build.publish, '..'))
14+
15+
// Clean up old functions
16+
await unlink(resolve('.netlify', 'middleware.js')).catch(() => {
17+
// Ignore if it doesn't exist
18+
})
19+
await writeDevEdgeFunction(constants)
20+
if (
21+
!existsSync(resolve(netlifyConfig.build.base, 'middleware.ts')) &&
22+
!existsSync(resolve(netlifyConfig.build.base, 'middleware.js'))
23+
) {
24+
console.log(
25+
"No middleware found. Create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
26+
)
27+
} else {
28+
console.log('Watching for changes in Next.js middleware...')
29+
}
30+
// Eventually we might want to do this via esbuild's API, but for now the CLI works fine
31+
const childProcess = execa(`esbuild`, [
32+
`--bundle`,
33+
`--outdir=${resolve('.netlify')}`,
34+
`--format=esm`,
35+
'--watch',
36+
// Watch for both, because it can have either ts or js
37+
resolve(netlifyConfig.build.base, 'middleware.ts'),
38+
resolve(netlifyConfig.build.base, 'middleware.js'),
39+
])
40+
41+
childProcess.stdout.pipe(process.stdout)
42+
childProcess.stderr.pipe(process.stderr)
43+
// Don't return the promise because we don't want to wait for the child process to finish
44+
}

packages/runtime/src/index.ts

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint-disable max-lines */
2-
import { join, relative, resolve } from 'path'
2+
import { join, relative } from 'path'
33

44
import type { NetlifyPlugin, OnPreBuild } from '@netlify/build'
55
import { greenBright, yellowBright, bold } from 'chalk'
6-
import execa from 'execa'
7-
import { existsSync, readFileSync, unlink } from 'fs-extra'
6+
import { existsSync, readFileSync } from 'fs-extra'
87
import { outdent } from 'outdent'
98

109
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from './constants'
@@ -16,7 +15,8 @@ import {
1615
configureHandlerFunctions,
1716
generateCustomHeaders,
1817
} from './helpers/config'
19-
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest, writeDevEdgeFunction } from './helpers/edge'
18+
import { onPreDev } from './helpers/dev'
19+
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
2020
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
2121
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2222
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -219,40 +219,7 @@ const nextPlugin = (_inputs, meta: { events: Set<string> }): NetlifyPlugin & { o
219219
}
220220
return {
221221
...plugin,
222-
onPreDev: async ({ constants, netlifyConfig }) => {
223-
// Need to patch the files, because build might not have been run
224-
await patchNextFiles(resolve(netlifyConfig.build.publish, '..'))
225-
226-
// Clean up old functions
227-
await unlink(resolve('.netlify', 'middleware.js')).catch(() => {
228-
// Ignore if it doesn't exist
229-
})
230-
await writeDevEdgeFunction(constants)
231-
if (
232-
!existsSync(resolve(netlifyConfig.build.base, 'middleware.ts')) &&
233-
!existsSync(resolve(netlifyConfig.build.base, 'middleware.js'))
234-
) {
235-
console.log(
236-
"No middleware found. Create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
237-
)
238-
} else {
239-
console.log('Watching for changes in Next.js middleware...')
240-
}
241-
// Eventually we might want to do this via esbuild's API, but for now the CLI works fine
242-
const childProcess = execa(`esbuild`, [
243-
`--bundle`,
244-
`--outdir=${resolve('.netlify')}`,
245-
`--format=esm`,
246-
'--watch',
247-
// Watch for both, because it can have either ts or js
248-
resolve(netlifyConfig.build.base, 'middleware.ts'),
249-
resolve(netlifyConfig.build.base, 'middleware.js'),
250-
])
251-
252-
childProcess.stdout.pipe(process.stdout)
253-
childProcess.stderr.pipe(process.stderr)
254-
// Don't return the promise because we don't want to wait for the child process to finish
255-
},
222+
onPreDev,
256223
}
257224
}
258225

0 commit comments

Comments
 (0)