-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor incremental cache to be extensible (#37258)
This refactors the `incremental-cache` and moves the file-system cache handling to it's own cache handler allowing it to be replaced by a custom cache handler (experimental). Closes: #22619 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
- Loading branch information
Showing
5 changed files
with
148 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/next/server/lib/incremental-cache/file-system-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { CacheFs } from '../../../shared/lib/utils' | ||
import path from '../../../shared/lib/isomorphic/path' | ||
import type { CacheHandler, CacheHandlerContext } from './' | ||
|
||
export default class FileSystemCache implements CacheHandler { | ||
private flushToDisk?: boolean | ||
private pagesDir: string | ||
private fs: CacheFs | ||
|
||
constructor(ctx: CacheHandlerContext) { | ||
this.flushToDisk = ctx.flushToDisk | ||
this.pagesDir = ctx.pagesDir | ||
this.fs = ctx.fs | ||
} | ||
|
||
public async get(key: string) { | ||
return this.fs.readFile(this.getSeedPath(key)) | ||
} | ||
public async getMeta(key: string) { | ||
const stat = await this.fs.stat(this.getSeedPath(key)) | ||
return { | ||
mtime: stat.mtime.getTime(), | ||
} | ||
} | ||
|
||
public async set(key: string, data: string) { | ||
if (!this.flushToDisk) return | ||
const pathname = this.getSeedPath(key) | ||
await this.fs.mkdir(path.dirname(pathname)) | ||
return this.fs.writeFile(pathname, data) | ||
} | ||
|
||
private getSeedPath(pathname: string): string { | ||
return path.join(this.pagesDir, pathname) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters