File size: 3,085 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import type { IncomingMessage } from 'http'
import type { DevBundler } from './router-utils/setup-dev-bundler'
import type { WorkerRequestHandler } from './types'
import { LRUCache } from './lru-cache'
import { createRequestResponseMocks } from './mock-request'
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../dev/hot-reloader-types'
/**
* The DevBundlerService provides an interface to perform tasks with the
* bundler while in development.
*/
export class DevBundlerService {
public appIsrManifestInner: InstanceType<typeof LRUCache>
constructor(
private readonly bundler: DevBundler,
private readonly handler: WorkerRequestHandler
) {
this.appIsrManifestInner = new LRUCache(
8_000,
function length() {
return 16
}
) as any
}
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 =
this.bundler.logErrorWithOriginalStack.bind(this.bundler)
public async getFallbackErrorComponents(url?: string) {
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,
url,
})
}
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 !== 200 &&
!(mocked.res.statusCode === 404 && revalidateOpts.unstable_onlyGenerated)
) {
throw new Error(`Invalid response ${mocked.res.statusCode}`)
}
return {}
}
public get appIsrManifest() {
const serializableManifest: Record<string, boolean> = {}
for (const key of this.appIsrManifestInner.keys() as string[]) {
serializableManifest[key] = this.appIsrManifestInner.get(key) as boolean
}
return serializableManifest
}
public setIsrStatus(key: string, value: boolean | null) {
if (value === null) {
this.appIsrManifestInner.remove(key)
} else {
this.appIsrManifestInner.set(key, value)
}
this.bundler?.hotReloader?.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.ISR_MANIFEST,
data: this.appIsrManifest,
})
}
public close() {
this.bundler.hotReloader.close()
}
}
|