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' ...@@ -39,25 +39,32 @@ import { MIDDLEWARE_FILENAME, PROXY_FILENAME } from '../../lib/constants'
const onceErrorSet = new Set() const onceErrorSet = new Set()
// Guards against concurrent writeToDisk() on the same Endpoint object. // Guards against concurrent writeToDisk() on shared global Endpoint objects
// This prevents handleRouteType and handlePagesErrorRoute from racing on // (_app, _document, _error). Both handleRouteType (for any pages-router page)
// shared _app/_document endpoint writes. // and handlePagesErrorRoute write these same endpoints, so concurrent
const writeToDiskInFlight = new WeakMap< // 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, object,
Promise<TurbopackResult<WrittenEndpoint>> Promise<TurbopackResult<WrittenEndpoint>>
>() >()
async function writeToDiskOnce( async function writeSharedEndpointToDisk(
endpoint: Endpoint endpoint: Endpoint
): Promise<TurbopackResult<WrittenEndpoint>> { ): Promise<TurbopackResult<WrittenEndpoint>> {
const existing = writeToDiskInFlight.get(endpoint) const existing = sharedEndpointWriteInFlight.get(endpoint)
if (existing) return existing if (existing) return existing
const promise = endpoint.writeToDisk() const promise = endpoint.writeToDisk()
writeToDiskInFlight.set(endpoint, promise) sharedEndpointWriteInFlight.set(endpoint, promise)
try { try {
return await promise return await promise
} finally { } finally {
writeToDiskInFlight.delete(endpoint) sharedEndpointWriteInFlight.delete(endpoint)
} }
} }
...@@ -206,7 +213,9 @@ export async function handleRouteType({ ...@@ -206,7 +213,9 @@ export async function handleRouteType({
if (entrypoints.global.app) { if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app') const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await writeToDiskOnce(entrypoints.global.app) const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.app
)
documentOrAppChanged ||= documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues( processIssues(
...@@ -223,7 +232,7 @@ export async function handleRouteType({ ...@@ -223,7 +232,7 @@ export async function handleRouteType({
if (entrypoints.global.document) { if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_document') const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await writeToDiskOnce( const writtenEndpoint = await writeSharedEndpointToDisk(
entrypoints.global.document entrypoints.global.document
) )
documentOrAppChanged ||= documentOrAppChanged ||=
...@@ -886,7 +895,9 @@ export async function handlePagesErrorRoute({ ...@@ -886,7 +895,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.app) { if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_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.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, key,
...@@ -915,7 +926,9 @@ export async function handlePagesErrorRoute({ ...@@ -915,7 +926,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.document) { if (entrypoints.global.document) {
const key = getEntryKey('pages', 'server', '_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.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, key,
...@@ -941,7 +954,9 @@ export async function handlePagesErrorRoute({ ...@@ -941,7 +954,9 @@ export async function handlePagesErrorRoute({
if (entrypoints.global.error) { if (entrypoints.global.error) {
const key = getEntryKey('pages', 'server', '_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.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, 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