Skip to content

Commit

Permalink
Add support for Next.js app router exports (#34)
Browse files Browse the repository at this point in the history
* Add support for Next.js `metadata` export

* Support all Next.js exports under `nextjs` key

* Limit to `metadata` and `revalidate` exports

Co-authored-by: Mike Fix <[email protected]>

* Use implicit return

Co-authored-by: Mike Fix <[email protected]>

---------

Co-authored-by: Mike Fix <[email protected]>
  • Loading branch information
bradlc and mfix-stripe authored Aug 16, 2023
1 parent f316acb commit 42f67e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
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};`)
.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

0 comments on commit 42f67e3

Please sign in to comment.