1 | "use strict";exports.__esModule=true;exports.IncrementalCache=void 0;var _fs=require("fs");var _lruCache=_interopRequireDefault(require("next/dist/compiled/lru-cache"));var _path=_interopRequireDefault(require("path"));var _constants=require("../lib/constants");var _normalizePagePath=require("./normalize-page-path");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function toRoute(pathname){return pathname.replace(/\/$/,'').replace(/\/index$/,'')||'/';}class IncrementalCache{constructor({max,dev,distDir,pagesDir,flushToDisk,locales}){this.incrementalOptions=void 0;this.prerenderManifest=void 0;this.cache=void 0;this.locales=void 0;this.incrementalOptions={dev,distDir,pagesDir,flushToDisk:!dev&&(typeof flushToDisk!=='undefined'?flushToDisk:true)};this.locales=locales;if(dev){this.prerenderManifest={version:-1,
|
2 | routes:{},dynamicRoutes:{},notFoundRoutes:[],preview:null
|
3 | };}else{this.prerenderManifest=JSON.parse((0,_fs.readFileSync)(_path.default.join(distDir,_constants.PRERENDER_MANIFEST),'utf8'));}this.cache=new _lruCache.default({
|
4 | max:max||50*1024*1024,length(val){if(val.isNotFound||val.isRedirect)return 25;
|
5 | return val.html.length+JSON.stringify(val.pageData).length;}});}getSeedPath(pathname,ext){return _path.default.join(this.incrementalOptions.pagesDir,`${pathname}.${ext}`);}calculateRevalidate(pathname){pathname=toRoute(pathname);
|
6 |
|
7 | const curTime=new Date().getTime();if(this.incrementalOptions.dev)return curTime-1000;const{initialRevalidateSeconds}=this.prerenderManifest.routes[pathname]||{initialRevalidateSeconds:1};const revalidateAfter=typeof initialRevalidateSeconds==='number'?initialRevalidateSeconds*1000+curTime:initialRevalidateSeconds;return revalidateAfter;}getFallback(page){page=(0,_normalizePagePath.normalizePagePath)(page);return _fs.promises.readFile(this.getSeedPath(page,'html'),'utf8');}
|
8 | async get(pathname){if(this.incrementalOptions.dev)return;pathname=(0,_normalizePagePath.normalizePagePath)(pathname);let data=this.cache.get(pathname);
|
9 | if(!data){if(this.prerenderManifest.notFoundRoutes.includes(pathname)){return{isNotFound:true,revalidateAfter:false};}try{const html=await _fs.promises.readFile(this.getSeedPath(pathname,'html'),'utf8');const pageData=JSON.parse(await _fs.promises.readFile(this.getSeedPath(pathname,'json'),'utf8'));data={html,pageData,revalidateAfter:this.calculateRevalidate(pathname)};this.cache.set(pathname,data);}catch(_){
|
10 | }}if(data&&data.revalidateAfter!==false&&data.revalidateAfter<new Date().getTime()){data.isStale=true;}const manifestPath=toRoute(pathname);const manifestEntry=this.prerenderManifest.routes[manifestPath];if(data&&manifestEntry){data.curRevalidate=manifestEntry.initialRevalidateSeconds;}return data;}
|
11 | async set(pathname,data,revalidateSeconds){if(this.incrementalOptions.dev)return;if(typeof revalidateSeconds!=='undefined'){
|
12 |
|
13 | this.prerenderManifest.routes[pathname]={dataRoute:_path.default.posix.join('/_next/data',`${(0,_normalizePagePath.normalizePagePath)(pathname)}.json`),srcRoute:null,
|
14 | initialRevalidateSeconds:revalidateSeconds};}pathname=(0,_normalizePagePath.normalizePagePath)(pathname);this.cache.set(pathname,{...data,revalidateAfter:this.calculateRevalidate(pathname)});
|
15 |
|
16 | if(this.incrementalOptions.flushToDisk&&!data.isNotFound){try{const seedPath=this.getSeedPath(pathname,'html');await _fs.promises.mkdir(_path.default.dirname(seedPath),{recursive:true});await _fs.promises.writeFile(seedPath,data.html,'utf8');await _fs.promises.writeFile(this.getSeedPath(pathname,'json'),JSON.stringify(data.pageData),'utf8');}catch(error){
|
17 | console.warn('Failed to update prerender files for',pathname,error);}}}}exports.IncrementalCache=IncrementalCache;
|
18 |
|
\ | No newline at end of file |