file-system-cache.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _lruCache = _interopRequireDefault(require("next/dist/compiled/lru-cache"));
  7. var _path = _interopRequireDefault(require("../../../shared/lib/isomorphic/path"));
  8. class FileSystemCache {
  9. constructor(ctx){
  10. this.fs = ctx.fs;
  11. this.flushToDisk = ctx.flushToDisk;
  12. this.serverDistDir = ctx.serverDistDir;
  13. this.appDir = !!ctx._appDir;
  14. if (ctx.maxMemoryCacheSize) {
  15. this.memoryCache = new _lruCache.default({
  16. max: ctx.maxMemoryCacheSize,
  17. length ({ value }) {
  18. var ref;
  19. if (!value) {
  20. return 25;
  21. } else if (value.kind === "REDIRECT") {
  22. return JSON.stringify(value.props).length;
  23. } else if (value.kind === "IMAGE") {
  24. throw new Error("invariant image should not be incremental-cache");
  25. }
  26. // rough estimate of size of cache value
  27. return value.html.length + (((ref = JSON.stringify(value.pageData)) == null ? void 0 : ref.length) || 0);
  28. }
  29. });
  30. }
  31. }
  32. async get(key) {
  33. var ref;
  34. let data = (ref = this.memoryCache) == null ? void 0 : ref.get(key);
  35. // let's check the disk for seed data
  36. if (!data) {
  37. try {
  38. var ref1;
  39. const { filePath: htmlPath , isAppPath } = await this.getFsPath(`${key}.html`);
  40. const html = await this.fs.readFile(htmlPath);
  41. const pageData = isAppPath ? await this.fs.readFile((await this.getFsPath(`${key}.rsc`, true)).filePath) : JSON.parse(await this.fs.readFile(await (await this.getFsPath(`${key}.json`, false)).filePath));
  42. const { mtime } = await this.fs.stat(htmlPath);
  43. data = {
  44. lastModified: mtime.getTime(),
  45. value: {
  46. kind: "PAGE",
  47. html,
  48. pageData
  49. }
  50. };
  51. (ref1 = this.memoryCache) == null ? void 0 : ref1.set(key, data);
  52. } catch (_) {
  53. // unable to get data from disk
  54. }
  55. }
  56. return data || null;
  57. }
  58. async set(key, data) {
  59. var ref;
  60. if (!this.flushToDisk) return;
  61. (ref = this.memoryCache) == null ? void 0 : ref.set(key, {
  62. value: data,
  63. lastModified: Date.now()
  64. });
  65. if ((data == null ? void 0 : data.kind) === "PAGE") {
  66. const isAppPath = typeof data.pageData === "string";
  67. const { filePath: htmlPath } = await this.getFsPath(`${key}.html`, isAppPath);
  68. await this.fs.mkdir(_path.default.dirname(htmlPath));
  69. await this.fs.writeFile(htmlPath, data.html);
  70. await this.fs.writeFile((await this.getFsPath(`${key}.${isAppPath ? "rsc" : "json"}`, isAppPath)).filePath, isAppPath ? data.pageData : JSON.stringify(data.pageData));
  71. }
  72. }
  73. async getFsPath(pathname, appDir) {
  74. let isAppPath = false;
  75. let filePath = _path.default.join(this.serverDistDir, "pages", pathname);
  76. if (!this.appDir || appDir === false) return {
  77. filePath,
  78. isAppPath
  79. };
  80. try {
  81. await this.fs.readFile(filePath);
  82. return {
  83. filePath,
  84. isAppPath
  85. };
  86. } catch (err) {
  87. return {
  88. filePath: _path.default.join(this.serverDistDir, "app", pathname),
  89. isAppPath: true
  90. };
  91. }
  92. }
  93. }
  94. exports.default = FileSystemCache;
  95. function _interopRequireDefault(obj) {
  96. return obj && obj.__esModule ? obj : {
  97. default: obj
  98. };
  99. }
  100. //# sourceMappingURL=file-system-cache.js.map