Skip to content

fix: handle missing config base #1555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions packages/runtime/src/helpers/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import { patchNextFiles } from './files'

// The types haven't been updated yet
export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
const base = netlifyConfig.build.base ?? process.cwd()

// Need to patch the files, because build might not have been run
await patchNextFiles(resolve(netlifyConfig.build.publish, '..'))
await patchNextFiles(base)

// Clean up old functions
await unlink(resolve('.netlify', 'middleware.js')).catch(() => {
// Ignore if it doesn't exist
})
await writeDevEdgeFunction(constants)
if (
!existsSync(resolve(netlifyConfig.build.base, 'middleware.ts')) &&
!existsSync(resolve(netlifyConfig.build.base, 'middleware.js'))
) {
if (!existsSync(resolve(base, 'middleware.ts')) && !existsSync(resolve(base, 'middleware.js'))) {
console.log(
"No middleware found. Create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
)
Expand All @@ -34,8 +33,8 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
`--format=esm`,
'--watch',
// Watch for both, because it can have either ts or js
resolve(netlifyConfig.build.base, 'middleware.ts'),
resolve(netlifyConfig.build.base, 'middleware.js'),
resolve(base, 'middleware.ts'),
resolve(base, 'middleware.js'),
])

childProcess.stdout.pipe(process.stdout)
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const writeDevEdgeFunction = async ({

const edgeFunctionDir = join(edgeFunctionRoot, 'next-dev')
await ensureDir(edgeFunctionDir)
await copyEdgeSourceFile({ edgeFunctionDir, file: 'next-dev.ts', target: 'index.ts' })
await copyEdgeSourceFile({ edgeFunctionDir, file: 'next-dev.js', target: 'index.js' })
await copyEdgeSourceFile({ edgeFunctionDir, file: 'utils.ts' })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,12 @@
import type { Context } from 'https://edge.netlify.com'
import { NextRequest, NextResponse } from 'https://esm.sh/next/server'
import { fromFileUrl } from 'https://deno.land/std/path/mod.ts'
import { buildResponse } from './utils.ts'

export interface FetchEventResult {
response: Response
waitUntil: Promise<unknown>
}

interface I18NConfig {
defaultLocale: string
domains?: DomainLocale[]
localeDetection?: false
locales: string[]
}

interface DomainLocale {
defaultLocale: string
domain: string
http?: true
locales?: string[]
}
export interface NextRequestInit extends RequestInit {
geo?: {
city?: string
country?: string
region?: string
}
ip?: string
nextConfig?: {
basePath?: string
i18n?: I18NConfig | null
trailingSlash?: boolean
}
}

export interface RequestData {
geo?: {
city?: string
country?: string
region?: string
latitude?: string
longitude?: string
}
headers: Record<string, string>
ip?: string
method: string
nextConfig?: {
basePath?: string
i18n?: Record<string, unknown>
trailingSlash?: boolean
}
page?: {
name?: string
params?: { [key: string]: string }
}
url: string
body?: ReadableStream<Uint8Array>
}

export interface RequestContext {
request: Request
context: Context
}

declare global {
// deno-lint-ignore no-var
var NFRequestContextMap: Map<string, RequestContext>
// deno-lint-ignore no-var
var __dirname: string
}

globalThis.NFRequestContextMap ||= new Map()
globalThis.__dirname = fromFileUrl(new URL('./', import.meta.url)).slice(0, -1)

// Check if a file exists, given a relative path
const exists = async (relativePath: string) => {
const exists = async (relativePath) => {
const path = fromFileUrl(new URL(relativePath, import.meta.url))
try {
await Deno.stat(path)
Expand All @@ -88,7 +19,7 @@ const exists = async (relativePath: string) => {
}
}

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

// This is the format expected by Next.js
const geo: NextRequestInit['geo'] = {
const geo = {
country: context.geo.country?.code,
region: context.geo.subdivision?.code,
city: context.geo.city,
Expand All @@ -125,15 +56,15 @@ const handler = async (req: Request, context: Context) => {
context,
})

const request: NextRequestInit = {
const request = {
headers: Object.fromEntries(req.headers.entries()),
geo,
method: req.method,
ip: context.ip,
body: req.body || undefined,
}

const nextRequest: NextRequest = new NextRequest(req, request)
const nextRequest = new NextRequest(req, request)

try {
const response = await middleware(nextRequest)
Expand Down