Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Nextra not able to run on WebContainers #843

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-poems-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra': patch
---

fix nextra on webcontainers
2 changes: 1 addition & 1 deletion examples/swr-site/theme.config.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.