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

Add support for Next.js app router exports #34

Merged
merged 6 commits into from
Aug 16, 2023
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
11 changes: 10 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ async function load(source) {
}

this.addContextDependency(schemaDir);

const nextjsExports = [
'metadata',
'revalidate',
]
const nextjsExportsCode = nextjsExports
.map((name) => `export const ${name} = frontmatter.nextjs?.${name};`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rpaul-stripe WDYT about using the nextjs frontmatter key to allow users to specify Next.js specific features (like metadata and revalidate)

Alternative keys:

  • next.js
  • config / options (higher chance for collisions)

I think I like next.js best (personally)

.join('\n')

const result = `import React from 'react';
import yaml from 'js-yaml';
// renderers is imported separately so Markdoc isn't sent to the client
Expand Down Expand Up @@ -206,7 +215,7 @@ ${
};
}`
}

${appDir ? nextjsExportsCode : ''}
export default${appDir ? ' async' : ''} function MarkdocComponent(props) {
const markdoc = ${appDir ? 'await getMarkdocData()' : 'props.markdoc'};
// Only execute HMR code in development
Expand Down
9 changes: 8 additions & 1 deletion tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ async function getMarkdocData(context = {}) {
}



export const dynamic = frontmatter.nextjs?.dynamic;
export const dynamicParams = frontmatter.nextjs?.dynamicParams;
export const fetchCache = frontmatter.nextjs?.fetchCache;
export const maxDuration = frontmatter.nextjs?.maxDuration;
export const metadata = frontmatter.nextjs?.metadata;
export const preferredRegion = frontmatter.nextjs?.preferredRegion;
export const revalidate = frontmatter.nextjs?.revalidate;
export const runtime = frontmatter.nextjs?.runtime;
export default async function MarkdocComponent(props) {
const markdoc = await getMarkdocData();
// Only execute HMR code in development
Expand Down
9 changes: 9 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ test('app router', async () => {
);
});

test('app router metadata', async () => {
const output = await callLoader(
options({ appDir: true }),
source.replace('---', '---\nmetadata:\n title: Metadata title')
);

expect(output).toContain('export const metadata = frontmatter.nextjs?.metadata;')
});

test.each([
[undefined, undefined],
['./schemas/folders', 'markdoc1'],
Expand Down