Skip to content

Commit f69d9db

Browse files
authored
fix: handle missing config base (#1555)
* fix: fall back to process.cwd() for base * fix: use js for dev edge fn
1 parent b208ff4 commit f69d9db

File tree

3 files changed

+12
-82
lines changed

3 files changed

+12
-82
lines changed

packages/runtime/src/helpers/dev.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import { patchNextFiles } from './files'
99

1010
// The types haven't been updated yet
1111
export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
12+
const base = netlifyConfig.build.base ?? process.cwd()
13+
1214
// Need to patch the files, because build might not have been run
13-
await patchNextFiles(resolve(netlifyConfig.build.publish, '..'))
15+
await patchNextFiles(base)
1416

1517
// Clean up old functions
1618
await unlink(resolve('.netlify', 'middleware.js')).catch(() => {
1719
// Ignore if it doesn't exist
1820
})
1921
await writeDevEdgeFunction(constants)
20-
if (
21-
!existsSync(resolve(netlifyConfig.build.base, 'middleware.ts')) &&
22-
!existsSync(resolve(netlifyConfig.build.base, 'middleware.js'))
23-
) {
22+
if (!existsSync(resolve(base, 'middleware.ts')) && !existsSync(resolve(base, 'middleware.js'))) {
2423
console.log(
2524
"No middleware found. Create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
2625
)
@@ -34,8 +33,8 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
3433
`--format=esm`,
3534
'--watch',
3635
// 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'),
36+
resolve(base, 'middleware.ts'),
37+
resolve(base, 'middleware.js'),
3938
])
4039

4140
childProcess.stdout.pipe(process.stdout)

packages/runtime/src/helpers/edge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const writeDevEdgeFunction = async ({
144144

145145
const edgeFunctionDir = join(edgeFunctionRoot, 'next-dev')
146146
await ensureDir(edgeFunctionDir)
147-
await copyEdgeSourceFile({ edgeFunctionDir, file: 'next-dev.ts', target: 'index.ts' })
147+
await copyEdgeSourceFile({ edgeFunctionDir, file: 'next-dev.js', target: 'index.js' })
148148
await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' })
149149
}
150150

packages/runtime/src/templates/edge/next-dev.ts renamed to packages/runtime/src/templates/edge/next-dev.js

+5-74
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,12 @@
1-
import type { Context } from 'https://edge.netlify.com'
21
import { NextRequest, NextResponse } from 'https://esm.sh/next/server'
32
import { fromFileUrl } from 'https://deno.land/std/path/mod.ts'
43
import { buildResponse } from './utils.ts'
54

6-
export interface FetchEventResult {
7-
response: Response
8-
waitUntil: Promise<unknown>
9-
}
10-
11-
interface I18NConfig {
12-
defaultLocale: string
13-
domains?: DomainLocale[]
14-
localeDetection?: false
15-
locales: string[]
16-
}
17-
18-
interface DomainLocale {
19-
defaultLocale: string
20-
domain: string
21-
http?: true
22-
locales?: string[]
23-
}
24-
export interface NextRequestInit extends RequestInit {
25-
geo?: {
26-
city?: string
27-
country?: string
28-
region?: string
29-
}
30-
ip?: string
31-
nextConfig?: {
32-
basePath?: string
33-
i18n?: I18NConfig | null
34-
trailingSlash?: boolean
35-
}
36-
}
37-
38-
export interface RequestData {
39-
geo?: {
40-
city?: string
41-
country?: string
42-
region?: string
43-
latitude?: string
44-
longitude?: string
45-
}
46-
headers: Record<string, string>
47-
ip?: string
48-
method: string
49-
nextConfig?: {
50-
basePath?: string
51-
i18n?: Record<string, unknown>
52-
trailingSlash?: boolean
53-
}
54-
page?: {
55-
name?: string
56-
params?: { [key: string]: string }
57-
}
58-
url: string
59-
body?: ReadableStream<Uint8Array>
60-
}
61-
62-
export interface RequestContext {
63-
request: Request
64-
context: Context
65-
}
66-
67-
declare global {
68-
// deno-lint-ignore no-var
69-
var NFRequestContextMap: Map<string, RequestContext>
70-
// deno-lint-ignore no-var
71-
var __dirname: string
72-
}
73-
745
globalThis.NFRequestContextMap ||= new Map()
756
globalThis.__dirname = fromFileUrl(new URL('./', import.meta.url)).slice(0, -1)
767

778
// Check if a file exists, given a relative path
78-
const exists = async (relativePath: string) => {
9+
const exists = async (relativePath) => {
7910
const path = fromFileUrl(new URL(relativePath, import.meta.url))
8011
try {
8112
await Deno.stat(path)
@@ -88,7 +19,7 @@ const exists = async (relativePath: string) => {
8819
}
8920
}
9021

91-
const handler = async (req: Request, context: Context) => {
22+
const handler = async (req, context) => {
9223
// Uncomment when CLI update lands
9324
// if (!Deno.env.get('NETLIFY_DEV')) {
9425
// // Only run in dev
@@ -111,7 +42,7 @@ const handler = async (req: Request, context: Context) => {
11142
}
11243

11344
// This is the format expected by Next.js
114-
const geo: NextRequestInit['geo'] = {
45+
const geo = {
11546
country: context.geo.country?.code,
11647
region: context.geo.subdivision?.code,
11748
city: context.geo.city,
@@ -125,15 +56,15 @@ const handler = async (req: Request, context: Context) => {
12556
context,
12657
})
12758

128-
const request: NextRequestInit = {
59+
const request = {
12960
headers: Object.fromEntries(req.headers.entries()),
13061
geo,
13162
method: req.method,
13263
ip: context.ip,
13364
body: req.body || undefined,
13465
}
13566

136-
const nextRequest: NextRequest = new NextRequest(req, request)
67+
const nextRequest = new NextRequest(req, request)
13768

13869
try {
13970
const response = await middleware(nextRequest)

0 commit comments

Comments
 (0)