Skip to content

Commit 020bef2

Browse files
committed
feat(dev-overlay): Add a query parameter to restart endpoint to invalidate the persistent cache
1 parent 8d25e84 commit 020bef2

File tree

17 files changed

+182
-21
lines changed

17 files changed

+182
-21
lines changed

crates/napi/src/next_api/project.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,18 @@ pub async fn project_update(
567567
Ok(())
568568
}
569569

570+
/// Invalidates the persistent cache so that it will be deleted next time that a turbopack project
571+
/// is created with persistent caching enabled.
572+
#[napi]
573+
pub async fn project_invalidate_persistent_cache(
574+
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
575+
) -> napi::Result<()> {
576+
tokio::task::spawn_blocking(move || project.turbo_tasks.invalidate_persistent_cache())
577+
.await
578+
.context("panicked while invalidating persistent cache")??;
579+
Ok(())
580+
}
581+
570582
/// Runs exit handlers for the project registered using the [`ExitHandler`] API.
571583
///
572584
/// This is called by `project_shutdown`, so if you're calling that API, you shouldn't call this

crates/napi/src/next_api/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ impl NextTurboTasks {
149149
}
150150
}
151151
}
152+
153+
pub fn invalidate_persistent_cache(&self) -> Result<()> {
154+
match self {
155+
NextTurboTasks::Memory(_) => {}
156+
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.backend().invalidate()?,
157+
}
158+
Ok(())
159+
}
152160
}
153161

154162
pub fn create_turbo_tasks(

packages/next/src/build/swc/generated-native.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ export declare function projectUpdate(
201201
project: { __napiType: 'Project' },
202202
options: NapiPartialProjectOptions
203203
): Promise<void>
204+
/**
205+
* Invalidates the persistent cache so that it will be deleted next time that a turbopack project
206+
* is created with persistent caching enabled.
207+
*/
208+
export declare function projectInvalidatePersistentCache(project: {
209+
__napiType: 'Project'
210+
}): Promise<void>
204211
/**
205212
* Runs exit handlers for the project registered using the [`ExitHandler`] API.
206213
*

packages/next/src/build/swc/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ function bindingToApi(
723723
)
724724
}
725725

726+
invalidatePersistentCache(): Promise<void> {
727+
return binding.projectInvalidatePersistentCache(this._nativeProject)
728+
}
729+
726730
shutdown(): Promise<void> {
727731
return binding.projectShutdown(this._nativeProject)
728732
}

packages/next/src/build/swc/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ export interface Project {
227227
TurbopackResult<CompilationEvent>
228228
>
229229

230+
invalidatePersistentCache(): Promise<void>
231+
230232
shutdown(): Promise<void>
231233

232234
onExit(): Promise<void>

packages/next/src/client/components/react-dev-overlay/server/restart-dev-server-middleware.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@ import type { ServerResponse, IncomingMessage } from 'http'
22
import type { Telemetry } from '../../../../telemetry/storage'
33
import { RESTART_EXIT_CODE } from '../../../../server/lib/utils'
44
import { middlewareResponse } from './middleware-response'
5+
import type { Project } from '../../../../build/swc/types'
56

67
const EVENT_DEV_OVERLAY_RESTART_SERVER = 'DEV_OVERLAY_RESTART_SERVER'
78

8-
export function getRestartDevServerMiddleware(telemetry: Telemetry) {
9+
interface RestartDevServerMiddlewareConfig {
10+
telemetry: Telemetry
11+
turbopackProject?: Project
12+
}
13+
14+
export function getRestartDevServerMiddleware({
15+
telemetry,
16+
turbopackProject,
17+
}: RestartDevServerMiddlewareConfig) {
918
return async function (
1019
req: IncomingMessage,
1120
res: ServerResponse,
1221
next: () => void
1322
): Promise<void> {
14-
const { pathname } = new URL(`http://n${req.url}`)
23+
const { pathname, searchParams } = new URL(`http://n${req.url}`)
1524
if (pathname !== '/__nextjs_restart_dev' || req.method !== 'POST') {
1625
return next()
1726
}
1827

28+
if (searchParams.has('invalidatePersistentCache')) {
29+
await turbopackProject?.invalidatePersistentCache()
30+
}
31+
1932
telemetry.record({
2033
eventName: EVENT_DEV_OVERLAY_RESTART_SERVER,
2134
payload: {},

packages/next/src/server/dev/hot-reloader-turbopack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,10 @@ export async function createHotReloaderTurbopack(
650650
getNextErrorFeedbackMiddleware(opts.telemetry),
651651
getDevOverlayFontMiddleware(),
652652
getDisableDevIndicatorMiddleware(),
653-
getRestartDevServerMiddleware(opts.telemetry),
653+
getRestartDevServerMiddleware({
654+
telemetry: opts.telemetry,
655+
turbopackProject: project,
656+
}),
654657
]
655658

656659
const versionInfoPromise = getVersionInfo()

packages/next/src/server/dev/hot-reloader-webpack.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,9 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
15701570
getNextErrorFeedbackMiddleware(this.telemetry),
15711571
getDevOverlayFontMiddleware(),
15721572
getDisableDevIndicatorMiddleware(),
1573-
getRestartDevServerMiddleware(this.telemetry),
1573+
getRestartDevServerMiddleware({
1574+
telemetry: this.telemetry,
1575+
}),
15741576
]
15751577
}
15761578

turbopack/crates/turbo-persistence/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl TurboPersistence {
176176
Ok(db)
177177
}
178178

179-
/// Performas the initial check on the database directory.
179+
/// Performs the initial check on the database directory.
180180
fn open_directory(&mut self) -> Result<()> {
181181
match fs::read_dir(&self.path) {
182182
Ok(entries) => {

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ impl<B: BackingStorage> TurboTasksBackend<B> {
202202
backing_storage,
203203
)))
204204
}
205+
206+
pub fn invalidate(&self) -> Result<()> {
207+
self.0.backing_storage.invalidate()
208+
}
205209
}
206210

207211
impl<B: BackingStorage> TurboTasksBackendInner<B> {

0 commit comments

Comments
 (0)