-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: bring dev service into managed object
- Loading branch information
Showing
7 changed files
with
135 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { IncomingMessage } from 'http' | ||
import type { DevBundler } from './router-utils/setup-dev-bundler' | ||
import type { WorkerRequestHandler } from './types' | ||
|
||
import { createRequestResponseMocks } from './mock-request' | ||
|
||
/** | ||
* The DevBundlerService provides an interface to perform tasks with the | ||
* bundler while in development. | ||
*/ | ||
export class DevBundlerService { | ||
constructor( | ||
private readonly bundler: DevBundler, | ||
private readonly handler: WorkerRequestHandler | ||
) {} | ||
|
||
public ensurePage: typeof this.bundler.hotReloader.ensurePage = async ( | ||
definition | ||
) => { | ||
// TODO: remove after ensure is pulled out of server | ||
return await this.bundler.hotReloader.ensurePage(definition) | ||
} | ||
|
||
public logErrorWithOriginalStack: typeof this.bundler.logErrorWithOriginalStack = | ||
async (...args) => { | ||
return await this.bundler.logErrorWithOriginalStack(...args) | ||
} | ||
|
||
public async getFallbackErrorComponents() { | ||
await this.bundler.hotReloader.buildFallbackError() | ||
// Build the error page to ensure the fallback is built too. | ||
// TODO: See if this can be moved into hotReloader or removed. | ||
await this.bundler.hotReloader.ensurePage({ | ||
page: '/_error', | ||
clientOnly: false, | ||
definition: undefined, | ||
}) | ||
} | ||
|
||
public async getCompilationError(page: string) { | ||
const errors = await this.bundler.hotReloader.getCompilationErrors(page) | ||
if (!errors) return | ||
|
||
// Return the very first error we found. | ||
return errors[0] | ||
} | ||
|
||
public async revalidate({ | ||
urlPath, | ||
revalidateHeaders, | ||
opts: revalidateOpts, | ||
}: { | ||
urlPath: string | ||
revalidateHeaders: IncomingMessage['headers'] | ||
opts: any | ||
}) { | ||
const mocked = createRequestResponseMocks({ | ||
url: urlPath, | ||
headers: revalidateHeaders, | ||
}) | ||
|
||
await this.handler(mocked.req, mocked.res) | ||
await mocked.res.hasStreamed | ||
|
||
if ( | ||
mocked.res.getHeader('x-nextjs-cache') !== 'REVALIDATED' && | ||
!(mocked.res.statusCode === 404 && revalidateOpts.unstable_onlyGenerated) | ||
) { | ||
throw new Error(`Invalid response ${mocked.res.statusCode}`) | ||
} | ||
|
||
return {} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.