Skip to content

Commit

Permalink
fix(vite-plugin-nitro): fix prerender from content directory (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
remes2000 committed Feb 16, 2024
1 parent f444e18 commit 4434069
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getMatchingContentFilesWithFrontMatter(
const fm = require('front-matter');
const root = normalizePath(path.resolve(workspaceRoot, rootDir));

const resolvedDir = normalizePath(path.resolve(root, glob));
const resolvedDir = normalizePath(path.relative(root, path.join(root, glob)));
const contentFiles: string[] = fg.sync([`${root}/${resolvedDir}/*`], {
dot: true,
});
Expand Down
119 changes: 63 additions & 56 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,65 +184,72 @@ describe('nitro', () => {
);
});

it('should build the server with content dir routes', async () => {
// Arrange
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const prerenderRoutes = {
prerender: {
routes: [
'/blog',
'/about',
describe('should build the server with content dir routes', () => {
[
'/packages/vite-plugin-nitro/test-data/content',
'packages/vite-plugin-nitro/test-data/content',
].forEach((contentDir) => {
it(`contentDir: ${contentDir}`, async () => {
// Arrange
const {
buildSSRAppImportSpy,
buildServerImportSpy,
buildSitemapImportSpy,
} = await mockBuildFunctions();
const prerenderRoutes = {
prerender: {
routes: [
'/blog',
'/about',
{
contentDir,
transform: (file: PrerenderContentFile) => {
if (file.attributes['draft']) {
return false;
}
const slug = file.attributes['slug'] || file.name;
return `/blog/${slug}`;
},
},
],
sitemap: { host: 'example.com' },
},
};
const plugin = nitro({
ssr: true,
...prerenderRoutes,
});

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildSSRAppImportSpy).toHaveBeenCalledWith(
{},
{ ssr: true, ...prerenderRoutes }
);

expect(buildServerImportSpy).toHaveBeenCalledWith(
{ ssr: true, ...prerenderRoutes },
{
contentDir: '/packages/vite-plugin-nitro/test-data/content',
transform: (file: PrerenderContentFile) => {
if (file.attributes['draft']) {
return false;
}
const slug = file.attributes['slug'] || file.name;
return `/blog/${slug}`;
...mockNitroConfig,
prerender: {
routes: ['/blog', '/about', '/blog/first', '/blog/02-second'],
},
},
],
sitemap: { host: 'example.com' },
},
};
const plugin = nitro({
ssr: true,
...prerenderRoutes,
alias: expect.anything(),
rollupConfig: expect.anything(),
handlers: expect.anything(),
}
);

expect(buildSitemapImportSpy).toHaveBeenCalledWith(
{},
{ host: 'example.com' },
['/blog', '/about', '/blog/first', '/blog/02-second'],
expect.anything()
);
});
});

// Act
await runConfigAndCloseBundle(plugin);

// Assert
expect(buildSSRAppImportSpy).toHaveBeenCalledWith(
{},
{ ssr: true, ...prerenderRoutes }
);

expect(buildServerImportSpy).toHaveBeenCalledWith(
{ ssr: true, ...prerenderRoutes },
{
...mockNitroConfig,
prerender: {
routes: ['/blog', '/about', '/blog/first', '/blog/02-second'],
},
alias: expect.anything(),
rollupConfig: expect.anything(),
handlers: expect.anything(),
}
);

expect(buildSitemapImportSpy).toHaveBeenCalledWith(
{},
{ host: 'example.com' },
['/blog', '/about', '/blog/first', '/blog/02-second'],
expect.anything()
);
});
});

Expand Down

0 comments on commit 4434069

Please sign in to comment.