Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add with-mdx-remote example#16613

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kodiakhq merged 17 commits intovercel:canaryfromdaneden:with-mdx-remote-example
Aug 29, 2020
Merged
Changes from1 commit
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
fa05082
Add with-mdx-remote example
danedenAug 26, 2020
ed2892a
Add note about conditional custom components
danedenAug 26, 2020
4679aa0
Add clarifying comment to components object
danedenAug 26, 2020
466f862
Add example React component
danedenAug 27, 2020
e44d1d2
Consistently use .jsx extension
danedenAug 27, 2020
6c9e335
Use `as` prop for Link to dynamic routes
danedenAug 27, 2020
944c596
Update readme and add next-remote-watch example
danedenAug 27, 2020
919e469
Remove next.config.js
danedenAug 28, 2020
3a61cb0
Update readme to use `getStaticProps` instead of `getInitialProps`
danedenAug 28, 2020
2f6e129
Remove empty devDependencies
danedenAug 28, 2020
2ae64ff
Improve design for example
danedenAug 28, 2020
0e031ac
Use .js instead of .jsx
danedenAug 28, 2020
bcb948c
Add nicer styles and example content
danedenAug 28, 2020
3d2eaaa
Merge branch 'canary' into with-mdx-remote-example
danedenAug 28, 2020
5e03d7f
Updated readme
lfadesAug 28, 2020
7e80a84
Updated package.json
lfadesAug 29, 2020
ce8ecad
Merge branch 'canary' into with-mdx-remote-example
kodiakhq[bot]Aug 29, 2020
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
PrevPrevious commit
NextNext commit
Updated readme
  • Loading branch information
@lfades
lfades committedAug 28, 2020
commit5e03d7f2fbc9c4c790d644930c7feb8b6a7f5e30
47 changes: 14 additions & 33 deletionsexamples/with-mdx-remote/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
# MDX Remote Example

This example shows how a simple blog might be built using the [next-mdx-remote](https://github.com/hashicorp/next-mdx-remote) library, which allows mdx content to be loaded via `getStaticProps` or `getServerSideProps`.In this example, themdx content is loaded from a local folder, but it could be loaded from a database or anywhere else. The demo also showcases [next-remote-watch](https://github.com/hashicorp/next-remote-watch), a library that allows next.js to watch files outside the `pages` folder that are not explicitly imported, which enables the mdx content here to trigger a live reload on change.
This example shows how a simple blog might be built using the [next-mdx-remote](https://github.com/hashicorp/next-mdx-remote) library, which allows mdx content to be loaded via `getStaticProps` or `getServerSideProps`.Themdx content is loaded from a local folder, but it could be loaded from a database or anywhere else.

Since `next-remote-watch` uses undocumented Next.js APIs, it doesn't replace the default `dev` script for this example. To use it, run `yarn dev:watch` or `npm run dev:watch`.
The example also showcases [next-remote-watch](https://github.com/hashicorp/next-remote-watch), a library that allows next.js to watch files outside the `pages` folder that are not explicitly imported, which enables the mdx content here to trigger a live reload on change.

Since `next-remote-watch` uses undocumented Next.js APIs, it doesn't replace the default `dev` script for this example. To use it, run `npm run dev:watch` or `yarn dev:watch`.

## Deploy your own

Expand All@@ -12,33 +14,12 @@ Deploy the example using [Vercel](https://vercel.com):

## How to use

### Using `create-next-app`

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example with-mdx with-mdx-remote-app
npx create-next-app --example with-mdx-remote with-mdx-remote-app
# or
yarn create next-app --example with-mdx with-mdx-remote-app
```

### Download manually

Download the example:

```bash
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-mdx-remote
cd with-mdx-remote
```

Install it and run:

```bash
npm install
npm run dev
# or
yarn
yarn dev
yarn create next-app --example with-mdx-remote with-mdx-remote-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
Expand All@@ -52,22 +33,22 @@ When using `next-mdx-remote`, you can pass custom components to the MDX renderer
For example, here's how you can change `getStaticProps` to conditionally add certain components:

```js
import dynamic from "next/dynamic"
...
import dynamic from 'next/dynamic'

// ...

export async function getStaticProps() {
const { content, data } = matter(source)

const components = {
...defaultComponents,
SomeHeavyComponent: /<SomeHeavyComponent/.test(content) ? dynamic(() => import("SomeHeavyComponent")) : null,
SomeHeavyComponent: /<SomeHeavyComponent/.test(content)
? dynamic(() => import('SomeHeavyComponent'))
: null,
}

const mdxSource = await renderToString(content, {
components,
...otherOptions,
})
const mdxSource = await renderToString(content, { components })
}
```

If you do this, you'll need toalsocheck in the page render function which components need to be dynamically loaded. You can pass a list ofcomponents names via `getStaticProps` to accomplish this.
If you do this, you'llalsoneed to check in the page render function which components need to be dynamically loaded. You can pass a list ofcomponent names via `getStaticProps` to accomplish this.

[8]ページ先頭

©2009-2025 Movatter.jp