Skip to content

Commit

Permalink
[v4] use Date.now() for last edit time on development and git last …
Browse files Browse the repository at this point in the history
…commit time on production (#3684)

* aa

* move to bottom
  • Loading branch information
dimaMachina authored Nov 10, 2024
1 parent a398adb commit 3edc6d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changeset/fifty-apricots-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nextra-theme-blog': patch
'nextra-theme-docs': patch
'nextra': patch
---

use `Date.now()` for last edit time on development and git last commit time on production
46 changes: 23 additions & 23 deletions packages/nextra/src/server/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { convertToPageMap } from './page-map/to-page-map.js'
import { twoslashRenderer } from './twoslash.js'
import { logger } from './utils.js'

const NOW = Date.now()
const CONTENT_DIR = getContentDirectory()
const APP_DIR = findPagesDir(CWD).appDir!

if (!APP_DIR) {
throw new Error('Unable to find `app` directory')
}
Expand Down Expand Up @@ -51,12 +54,8 @@ const repository = await (async () => {
}
})()

const CONTENT_DIR = getContentDirectory()
// repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it
const GIT_ROOT = repository ? path.join(repository.path(), '..') : ''
// Cache the result of `repository.getFileLatestModifiedDateAsync` because it can slow down
// Fast Refresh for uncommitted files
const LastCommitTimeMap = new Map<string, number>()

export async function loader(
this: LoaderContext<LoaderOptions>,
Expand Down Expand Up @@ -147,27 +146,11 @@ export async function loader(
whiteListTagsStyling
})

let timestamp: number | undefined = LastCommitTimeMap.get(filePath)
if (IS_PRODUCTION && timestamp === undefined) {
try {
if (!repository) {
throw new Error('Init git repository failed')
}
const relativePath = path.relative(GIT_ROOT, filePath)
timestamp = await repository.getFileLatestModifiedDateAsync(relativePath)
LastCommitTimeMap.set(filePath, timestamp)
} catch {
logger.warn(
'Failed to get the last modified timestamp from Git for the file',
filePath
)
LastCommitTimeMap.set(filePath, 0)
}
}

const restProps: PageOpts['metadata'] = {
filePath: slash(path.relative(CWD, filePath)),
timestamp,
// Run only on production because it can slow down Fast Refresh for uncommitted files
// https://github.com/shuding/nextra/issues/3675#issuecomment-2466416366
timestamp: IS_PRODUCTION ? await getLastCommitTime(filePath) : NOW,
readingTime
}
const enhancedMetadata = `Object.assign(metadata, ${JSON.stringify(restProps)})`
Expand Down Expand Up @@ -225,3 +208,20 @@ ${locales

return rawJs.replace(rawImport, replaced)
}

async function getLastCommitTime(
filePath: string
): Promise<number | undefined> {
try {
if (!repository) {
throw new Error('Init git repository failed')
}
const relativePath = path.relative(GIT_ROOT, filePath)
return await repository.getFileLatestModifiedDateAsync(relativePath)
} catch {
logger.warn(
'Failed to get the last modified timestamp from Git for the file',
filePath
)
}
}

0 comments on commit 3edc6d7

Please sign in to comment.