Skip to content

Commit 42f67e3

Browse files
bradlcmfix-stripe
andauthored
Add support for Next.js app router exports (#34)
* 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]>
1 parent f316acb commit 42f67e3

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/loader.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ async function load(source) {
119119
}
120120

121121
this.addContextDependency(schemaDir);
122+
123+
const nextjsExports = [
124+
'metadata',
125+
'revalidate',
126+
]
127+
const nextjsExportsCode = nextjsExports
128+
.map((name) => `export const ${name} = frontmatter.nextjs?.${name};`)
129+
.join('\n')
130+
122131
const result = `import React from 'react';
123132
import yaml from 'js-yaml';
124133
// renderers is imported separately so Markdoc isn't sent to the client
@@ -206,7 +215,7 @@ ${
206215
};
207216
}`
208217
}
209-
218+
${appDir ? nextjsExportsCode : ''}
210219
export default${appDir ? ' async' : ''} function MarkdocComponent(props) {
211220
const markdoc = ${appDir ? 'await getMarkdocData()' : 'props.markdoc'};
212221
// Only execute HMR code in development

tests/__snapshots__/index.test.js.snap

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ async function getMarkdocData(context = {}) {
169169
}
170170
171171
172-
172+
export const dynamic = frontmatter.nextjs?.dynamic;
173+
export const dynamicParams = frontmatter.nextjs?.dynamicParams;
174+
export const fetchCache = frontmatter.nextjs?.fetchCache;
175+
export const maxDuration = frontmatter.nextjs?.maxDuration;
176+
export const metadata = frontmatter.nextjs?.metadata;
177+
export const preferredRegion = frontmatter.nextjs?.preferredRegion;
178+
export const revalidate = frontmatter.nextjs?.revalidate;
179+
export const runtime = frontmatter.nextjs?.runtime;
173180
export default async function MarkdocComponent(props) {
174181
const markdoc = await getMarkdocData();
175182
// Only execute HMR code in development

tests/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ test('app router', async () => {
177177
);
178178
});
179179

180+
test('app router metadata', async () => {
181+
const output = await callLoader(
182+
options({ appDir: true }),
183+
source.replace('---', '---\nmetadata:\n title: Metadata title')
184+
);
185+
186+
expect(output).toContain('export const metadata = frontmatter.nextjs?.metadata;')
187+
});
188+
180189
test.each([
181190
[undefined, undefined],
182191
['./schemas/folders', 'markdoc1'],

0 commit comments

Comments
 (0)