Skip to content

Commit

Permalink
Fix Nextra not able to run on WebContainers (#843)
Browse files Browse the repository at this point in the history
* fix nextra on webcontainers

* add changeset
  • Loading branch information
shuding committed Sep 13, 2022
1 parent 21009c7 commit a9523c9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-poems-argue.md
@@ -0,0 +1,5 @@
---
'nextra': patch
---

fix nextra on webcontainers
2 changes: 1 addition & 1 deletion examples/swr-site/theme.config.tsx
Expand Up @@ -121,7 +121,7 @@ const config: DocsThemeConfig = {
);
},
},
gitTimestamp: "Last updated on",
gitTimestamp: ({ timestamp }) => <>Last updated on {timestamp.toString()}</>,
head() {
const config = useConfig();
const description =
Expand Down
52 changes: 28 additions & 24 deletions packages/nextra/src/loader.ts
Expand Up @@ -4,7 +4,6 @@ import path from 'node:path'
import grayMatter from 'gray-matter'
import slash from 'slash'
import { LoaderContext } from 'webpack'
import { Repository } from '@napi-rs/simple-git'
import { findPagesDir } from 'next/dist/lib/find-pages-dir.js'

import { addPage } from './content-dump'
Expand All @@ -25,32 +24,36 @@ const PAGES_DIR = findPagesDir(CWD).pages
// TODO: create this as a webpack plugin.
const indexContentEmitted = new Set<string>()

const [repository, gitRoot] = (function () {
try {
const repo = Repository.discover(CWD)
if (repo.isShallow()) {
if (process.env.VERCEL) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.'
)
} else if (process.env.GITHUB_ACTION) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.'
)
} else {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented.'
)
const IS_WEB_CONTAINER = !!process.versions.webcontainer

const initGitRepo = (async () => {
if (!IS_WEB_CONTAINER) {
const { Repository } = await import('@napi-rs/simple-git')
try {
const repository = Repository.discover(CWD)
if (repository.isShallow()) {
if (process.env.VERCEL) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.'
)
} else if (process.env.GITHUB_ACTION) {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.'
)
} else {
console.warn(
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented.'
)
}
}
// repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it
const gitRoot = path.join(repository.path(), '..')
return { repository, gitRoot }
} catch (e) {
console.warn('[nextra] Init git repository failed', e)
}
// repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it
const gitRoot = path.join(repo.path(), '..')

return [repo, gitRoot]
} catch (e) {
console.warn('[nextra] Init git repository failed', e)
return []
}
return {}
})()

async function loader(
Expand Down Expand Up @@ -160,6 +163,7 @@ export default MDXContent`.trimStart()
}

let timestamp: PageOpts['timestamp']
const { repository, gitRoot } = await initGitRepo
if (repository && gitRoot) {
try {
timestamp = await repository.getFileLatestModifiedDateAsync(
Expand Down
35 changes: 33 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9523c9

Please sign in to comment.