Commit 3535acd0 authored by Luke Sandberg's avatar Luke Sandberg

Rename writeToDiskOnce -> writeSharedEndpointToDisk and expand comments

Clarify that the dedup guard is scoped to shared global endpoints (_app,
_document, _error) and that coalesced callers' clearRequireCache calls
are harmless no-ops due to serverPathState hash matching.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent b27a467d
......@@ -39,25 +39,32 @@ import { MIDDLEWARE_FILENAME, PROXY_FILENAME } from '../../lib/constants'
const onceErrorSet = new Set()
// Guards against concurrent writeToDisk() on the same Endpoint object.
// This prevents handleRouteType and handlePagesErrorRoute from racing on
// shared _app/_document endpoint writes.
const writeToDiskInFlight = new WeakMap<
// Guards against concurrent writeToDisk() on shared global Endpoint objects
// (_app, _document, _error). Both handleRouteType (for any pages-router page)
// and handlePagesErrorRoute write these same endpoints, so concurrent
// ensurePage() calls can race. Per-route endpoints (page HTML, app-page,
// app-route, middleware, instrumentation) don't need this guard because they
// are unique to a single route and are not written from multiple code paths.
//
// When a second caller coalesces onto an in-flight write, it receives the same
// WrittenEndpoint result. Its subsequent handleWrittenEndpoint/clearRequireCache
// call is effectively a no-op because serverPathState hashes already match.
const sharedEndpointWriteInFlight = new WeakMap<
object,
Promise<TurbopackResult<WrittenEndpoint>>
>()
async function writeToDiskOnce(
async function writeSharedEndpointToDisk(
endpoint: Endpoint
): Promise<TurbopackResult<WrittenEndpoint>> {
const existing = writeToDiskInFlight.get(endpoint)
const existing = sharedEndpointWriteInFlight.get(endpoint)
if (existing) return existing
const promise = endpoint.writeToDisk()
writeToDiskInFlight.set(endpoint, promise)
sharedEndpointWriteInFlight.set(endpoint, promise)
try {
return await promise
} finally {
writeToDiskInFlight.delete(endpoint)
sharedEndpointWriteInFlight.delete(endpoint)
}
}
......@@ -206,7 +213,9 @@ export async function handleRouteType({
if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await writeToDiskOnce(entrypoints.global.app)
const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.app
)
documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues(
......@@ -223,7 +232,7 @@ export async function handleRouteType({
if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await writeToDiskOnce(
const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.document
)
documentOrAppChanged ||=
......@@ -886,7 +895,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await writeToDiskOnce(entrypoints.global.app)
const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.app
)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
......@@ -915,7 +926,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await writeToDiskOnce(entrypoints.global.document)
const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.document
)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
......@@ -941,7 +954,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.error) {
const key = getEntryKey('pages', 'server', '_error')
const writtenEndpoint = await writeToDiskOnce(entrypoints.global.error)
const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.error
)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment