@@ -5,47 +5,83 @@ import path from 'path'
55
66import pkg from 'fs-extra'
77
8- import { BUILD_DIR , SERVER_DIR } from '../constants.js'
9-
8+ import { NEXT_DIR , SERVER_DIR } from '../constants.js'
109// readfile not available in esm version of fs-extra
1110const { readFile } = pkg
1211
13-
1412export const removeFileDir = ( file : string , num : number ) => {
1513 return file . split ( '/' ) . slice ( num ) . join ( '/' )
1614 }
1715
18-
19- export const formatPageData = async ( pathname : string , key : string , file : string ) => {
16+ export const formatBlobContent = async ( pathname : string , key : string , file : string ) => {
2017 const isPage = pathname . startsWith ( 'pages' )
2118 const isApp = pathname . startsWith ( 'app' )
22- const removedDir = removeFileDir ( pathname , 1 ) . replace ( path . extname ( file ) , '' )
23- // console.log({pathname, key, file, isPage, isApp, removedDir} )
19+ const isFetchCache = file . startsWith ( 'cache/fetch-cache' )
20+ const isRoute = file . endsWith ( 'body' )
21+
2422 let data : any = { }
25- if ( isApp || isPage ) {
26- console . log ( "WIHTIN APP + PAGE CHECK" )
27- const getDataFile = async ( files : string , appDir : boolean , ext : string ) =>
28- await readFile (
29- join ( SERVER_DIR , ( appDir ? 'app' : 'pages' ) , `${ files } .${ ext } ` ) ,
30- 'utf8' )
31-
32- const pageData = isPage ? JSON . parse ( await getDataFile ( removedDir , false , 'json' ) ) : await getDataFile ( removedDir , true , 'rsc' )
33- let meta : { status ?: number , headers ?: OutgoingHttpHeaders } = { }
34-
35- try {
36- meta = JSON . parse ( ( await getDataFile ( key , true , 'meta' ) ) )
37- } catch { }
38-
39- data = {
23+
24+ if ( ! isRoute && ( isApp || isPage ) ) {
25+ data = await formatPage ( isPage , file , pathname )
26+ }
27+
28+ if ( isFetchCache ) {
29+ data = await formatFetchCache ( file )
30+ }
31+
32+ if ( isRoute ) {
33+ data = await formatRoute ( file , key , pathname )
34+ }
35+ return data
36+ }
37+
38+ const readFileData = async ( files : string , appDir : boolean , ext : string ) =>
39+ await readFile (
40+ join ( SERVER_DIR , ( appDir ? 'app' : 'pages' ) , `${ files } .${ ext } ` ) ,
41+ 'utf8' )
42+
43+ const formatPage = async ( page : boolean , file : string , pathname : string ) => {
44+ const removedDir = removeFileDir ( pathname , 1 ) . replace ( path . extname ( file ) , '' )
45+ const pageData = page ? JSON . parse ( await readFileData ( removedDir , false , 'json' ) ) : await readFileData ( removedDir , true , 'rsc' )
46+ let meta : { status ?: number , headers ?: OutgoingHttpHeaders } = { }
47+
48+ try {
49+ meta = JSON . parse ( ( await readFileData ( removedDir , true , 'meta' ) ) )
50+ } catch { }
51+
52+ return {
4053 lastModified : Date . now ( ) ,
4154 value : {
42- kind : 'PAGE' ,
43- html : await readFile ( join ( BUILD_DIR , file ) , 'utf8' ) ,
44- pageData,
45- headers : meta . headers ,
46- status : meta . status ,
55+ kind : 'PAGE' ,
56+ html : await readFile ( join ( NEXT_DIR , file ) , 'utf8' ) ,
57+ pageData,
58+ headers : meta . headers ,
59+ status : meta . status ,
4760 }
48- }
4961 }
50- return data
62+ }
63+
64+ const formatFetchCache = async ( file : string ) => {
65+ const parsedData = JSON . parse ( await readFile ( join ( NEXT_DIR , file ) , 'utf8' ) )
66+ return {
67+ lastModified : Date . now ( ) ,
68+ value : parsedData ,
69+ }
70+ }
71+
72+ const formatRoute = async ( file : string , key : string , pathname : string ) => {
73+ const removedDir = removeFileDir ( pathname , 1 ) . replace ( path . extname ( file ) , '' )
74+ try {
75+ // const data = await readFileData(removedDir, true, 'body')
76+ const meta = JSON . parse ( await readFileData ( removedDir , true , 'meta' ) )
77+ return {
78+ lastModified : Date . now ( ) ,
79+ value : {
80+ kind : 'ROUTE' ,
81+ // body: data,
82+ headers : meta . headers ,
83+ status : meta . status ,
84+ }
85+ }
86+ } catch { }
5187}
0 commit comments