index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _fileSystemCache = _interopRequireDefault(require("./file-system-cache"));
  6. var _path = _interopRequireDefault(require("../../../shared/lib/isomorphic/path"));
  7. var _normalizePagePath = require("../../../shared/lib/page-path/normalize-page-path");
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {
  10. default: obj
  11. };
  12. }
  13. function toRoute(pathname) {
  14. return pathname.replace(/\/$/, "").replace(/\/index$/, "") || "/";
  15. }
  16. class CacheHandler {
  17. // eslint-disable-next-line
  18. constructor(_ctx){}
  19. async get(_key) {
  20. return {};
  21. }
  22. async set(_key, _data) {}
  23. }
  24. exports.CacheHandler = CacheHandler;
  25. class IncrementalCache {
  26. constructor({ fs , dev , appDir , flushToDisk , serverDistDir , maxMemoryCacheSize , getPrerenderManifest , incrementalCacheHandlerPath }){
  27. let cacheHandlerMod = _fileSystemCache.default;
  28. if (process.env.NEXT_RUNTIME !== "edge" && incrementalCacheHandlerPath) {
  29. cacheHandlerMod = require(incrementalCacheHandlerPath);
  30. cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod;
  31. }
  32. if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
  33. // Allow cache size to be overridden for testing purposes
  34. maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10);
  35. }
  36. this.dev = dev;
  37. this.prerenderManifest = getPrerenderManifest();
  38. this.cacheHandler = new cacheHandlerMod({
  39. dev,
  40. fs,
  41. flushToDisk,
  42. serverDistDir,
  43. maxMemoryCacheSize,
  44. _appDir: appDir
  45. });
  46. }
  47. calculateRevalidate(pathname, fromTime) {
  48. // in development we don't have a prerender-manifest
  49. // and default to always revalidating to allow easier debugging
  50. if (this.dev) return new Date().getTime() - 1000;
  51. // if an entry isn't present in routes we fallback to a default
  52. // of revalidating after 1 second
  53. const { initialRevalidateSeconds } = this.prerenderManifest.routes[toRoute(pathname)] || {
  54. initialRevalidateSeconds: 1
  55. };
  56. const revalidateAfter = typeof initialRevalidateSeconds === "number" ? initialRevalidateSeconds * 1000 + fromTime : initialRevalidateSeconds;
  57. return revalidateAfter;
  58. }
  59. _getPathname(pathname) {
  60. return (0, _normalizePagePath).normalizePagePath(pathname);
  61. }
  62. // get data from cache if available
  63. async get(pathname) {
  64. var ref;
  65. // we don't leverage the prerender cache in dev mode
  66. // so that getStaticProps is always called for easier debugging
  67. if (this.dev) return null;
  68. pathname = this._getPathname(pathname);
  69. let entry = null;
  70. const cacheData = await this.cacheHandler.get(pathname);
  71. const curRevalidate = (ref = this.prerenderManifest.routes[toRoute(pathname)]) == null ? void 0 : ref.initialRevalidateSeconds;
  72. const revalidateAfter = this.calculateRevalidate(pathname, (cacheData == null ? void 0 : cacheData.lastModified) || Date.now());
  73. const isStale = revalidateAfter !== false && revalidateAfter < Date.now() ? true : undefined;
  74. if (cacheData) {
  75. entry = {
  76. isStale,
  77. curRevalidate,
  78. revalidateAfter,
  79. value: cacheData.value
  80. };
  81. }
  82. if (!cacheData && this.prerenderManifest.notFoundRoutes.includes(pathname)) {
  83. // for the first hit after starting the server the cache
  84. // may not have a way to save notFound: true so if
  85. // the prerender-manifest marks this as notFound then we
  86. // return that entry and trigger a cache set to give it a
  87. // chance to update in-memory entries
  88. entry = {
  89. isStale,
  90. value: null,
  91. curRevalidate,
  92. revalidateAfter
  93. };
  94. this.set(pathname, entry.value, curRevalidate);
  95. }
  96. return entry;
  97. }
  98. // populate the incremental cache with new data
  99. async set(pathname, data, revalidateSeconds) {
  100. if (this.dev) return;
  101. pathname = this._getPathname(pathname);
  102. try {
  103. // we use the prerender manifest memory instance
  104. // to store revalidate timings for calculating
  105. // revalidateAfter values so we update this on set
  106. if (typeof revalidateSeconds !== "undefined") {
  107. this.prerenderManifest.routes[pathname] = {
  108. dataRoute: _path.default.posix.join("/_next/data", `${(0, _normalizePagePath).normalizePagePath(pathname)}.json`),
  109. srcRoute: null,
  110. initialRevalidateSeconds: revalidateSeconds
  111. };
  112. }
  113. await this.cacheHandler.set(pathname, data);
  114. } catch (error) {
  115. console.warn("Failed to update prerender cache for", pathname, error);
  116. }
  117. }
  118. }
  119. exports.IncrementalCache = IncrementalCache;
  120. //# sourceMappingURL=index.js.map