Unverified Commit d054fe4a authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Persist storage cache in Docker environments (#100790)

### What?

Next.js now persists its server-side storage cache in Docker
environments, the same as other environments. This covers the RSC /
Server Actions encryption key, preview-mode keys, and the development
Chrome DevTools workspace identifier.

### Why?

The shared storage-directory helper previously disabled `.next/cache`
persistence whenever Docker was detected. As a result, Docker
environments could not reuse cached RSC encryption secrets across
separate key-generation processes.

### How?

The shared helper now always resolves the cache directory beneath the
configured dist directory. Because storage is always available, each
consumer drops the now-unreachable fallback path and uses the existing
cache read/write flow directly. Existing key validation, expiry-based
rotation during builds, and `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY`
precedence are unchanged.

Telemetry is intentionally unchanged: it has its own storage-location
logic for CI and Docker, which already stores telemetry configuration in
`.next/cache` there.

### Verification

- Ran a focused probe against the updated source with Docker detection
mocked. It confirmed RSC encryption-key cache write and reuse,
expired-key handling for runtime and builds, explicit encryption-key
precedence, and preview-key cache write and reuse.
- `pnpm --filter=next types`
- Not run: full Next.js integration tests.

<!-- NEXT_JS_LLM -->


<!-- fleet 3a3e1bf2-4e32-499c-8f71-ae9dd2d64f16 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent b0b2e89e
......@@ -13,8 +13,6 @@ const EXPIRATION = 1000 * 60 * 60 * 24 * 14 // 14 days
async function writeCache(distDir: string, config: __ApiPreviewProps) {
const cacheBaseDir = getStorageDirectory(distDir)
if (!cacheBaseDir) return
const configPath = path.join(cacheBaseDir, CONFIG_FILE)
if (!fs.existsSync(cacheBaseDir)) {
await fs.promises.mkdir(cacheBaseDir, { recursive: true })
......@@ -51,13 +49,6 @@ export async function generatePreviewKeys({
isBuild: boolean
}): Promise<__ApiPreviewProps> {
const cacheBaseDir = getStorageDirectory(distDir)
if (!cacheBaseDir) {
// There's no persistent storage available. We generate a new config.
// This also covers development time.
return generateConfig()
}
const configPath = path.join(cacheBaseDir, CONFIG_FILE)
async function tryReadCachedConfig(): Promise<false | __ApiPreviewProps> {
if (!fs.existsSync(configPath)) return false
......
......@@ -15,8 +15,6 @@ const EXPIRATION = 1000 * 60 * 60 * 24 * 14 // 14 days
async function writeCache(distDir: string, configValue: string) {
const cacheBaseDir = getStorageDirectory(distDir)
if (!cacheBaseDir) return
const configPath = path.join(cacheBaseDir, CONFIG_FILE)
if (!fs.existsSync(cacheBaseDir)) {
await fs.promises.mkdir(cacheBaseDir, { recursive: true })
......@@ -42,13 +40,6 @@ async function loadOrGenerateKey(
generateKey: () => Promise<string>
): Promise<string> {
const cacheBaseDir = getStorageDirectory(distDir)
if (!cacheBaseDir) {
// There's no persistent storage available. We generate a new key.
// This also covers development time.
return await generateKey()
}
const configPath = path.join(cacheBaseDir, CONFIG_FILE)
async function hasCachedKey(): Promise<false | string> {
if (!fs.existsSync(configPath)) return false
......
import path from 'path'
import isDockerFunction from 'next/dist/compiled/is-docker'
export function getStorageDirectory(distDir: string): string | undefined {
const isLikelyEphemeral = isDockerFunction()
if (isLikelyEphemeral) {
return undefined
}
export function getStorageDirectory(distDir: string): string {
return path.join(distDir, 'cache')
}
......@@ -51,31 +51,27 @@ async function getChromeDevtoolsWorkspace(
const distDir = path.join(root, configDistDir)
const cacheBaseDir = getStorageDirectory(distDir)
if (cacheBaseDir === undefined) {
const cachedUUIDPath = path.join(
cacheBaseDir,
'chrome-devtools-workspace-uuid'
)
try {
workspaceUUID = await fs.promises.readFile(cachedUUIDPath, 'utf8')
} catch {
// TODO: Why does this need to be v4 and not v5?
// With v5 we could base it off of the `distDir` and `root` which would
// allow us to persist the workspace across .next wipes.
workspaceUUID = randomUUID()
} else {
const cachedUUIDPath = path.join(
cacheBaseDir,
'chrome-devtools-workspace-uuid'
)
try {
workspaceUUID = await fs.promises.readFile(cachedUUIDPath, 'utf8')
} catch {
// TODO: Why does this need to be v4 and not v5?
// With v5 we could base it off of the `distDir` and `root` which would
// allow us to persist the workspace across .next wipes.
workspaceUUID = randomUUID()
try {
await fs.promises.writeFile(cachedUUIDPath, workspaceUUID, 'utf8')
} catch (cause) {
console.warn(
new Error(
'Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload.',
{ cause }
)
try {
await fs.promises.writeFile(cachedUUIDPath, workspaceUUID, 'utf8')
} catch (cause) {
console.warn(
new Error(
'Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload.',
{ cause }
)
}
)
}
}
}
......
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