Skip to content

Commit

Permalink
fix: import server in module scope (#363)
Browse files Browse the repository at this point in the history
* fix: import server in module scope

* chore: rmeove comment
  • Loading branch information
ascorbic authored Mar 21, 2024
1 parent 7775941 commit bce994d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { logger } from '../systemlog.js'
import { createRequestContext, getRequestContext } from './request-context.cjs'
import { getTracer } from './tracer.cjs'

const nextImportPromise = import('../next.cjs')

let nextHandler: WorkerRequestHandler, nextConfig: NextConfigComplete, tagsManifest: TagsManifest

/**
Expand Down Expand Up @@ -63,7 +65,7 @@ export default async (request: Request) => {
),
)

const { getMockedRequestHandlers } = await import('../next.cjs')
const { getMockedRequestHandlers } = await nextImportPromise
const url = new URL(request.url)

;[nextHandler] = await getMockedRequestHandlers({
Expand Down
17 changes: 8 additions & 9 deletions src/run/next.cts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import { relative, resolve } from 'path'
import { getDeployStore } from '@netlify/blobs'
// @ts-expect-error no types installed
import { patchFs } from 'fs-monkey'
import type { getRequestHandlers as GetRequestHandlersSignature } from 'next/dist/server/lib/start-server.js'

import { getRequestContext } from './handlers/request-context.cjs'
import { getTracer } from './handlers/tracer.cjs'

console.time('import next server')

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { getRequestHandlers } = require('next/dist/server/lib/start-server.js')

console.timeEnd('import next server')

type FS = typeof import('fs')

const fetchBeforeNextPatchedIt = globalThis.fetch

export async function getMockedRequestHandlers(
...args: Parameters<typeof GetRequestHandlersSignature>
) {
export async function getMockedRequestHandlers(...args: Parameters<typeof getRequestHandlers>) {
const tracer = getTracer()
return tracer.withActiveSpan('mocked request handler', async () => {
const ofs = { ...fs }
Expand Down Expand Up @@ -57,11 +61,6 @@ export async function getMockedRequestHandlers(
require('fs').promises,
)

const { getRequestHandlers } = await tracer.withActiveSpan(
'import next server',
async () => import('next/dist/server/lib/start-server.js'),
)

return getRequestHandlers(...args)
})
}

0 comments on commit bce994d

Please sign in to comment.