Skip to content

Commit 1cf59a7

Browse files
authored
fix(vite-plugin-nitro): build sitemap before server with Environment API (#2762)
1 parent 769b5cf commit 1cf59a7

2 files changed

Lines changed: 53 additions & 16 deletions

File tree

‎packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NitroConfig } from 'nitropack';
2+
import { ConfigEnv, UserConfig, ViteBuilder } from 'vite';
13
import { describe, expect, it, vi } from 'vitest';
24

35
import { PrerenderContentFile } from './options';
@@ -34,6 +36,48 @@ describe('nitro', () => {
3436
expect(spy).not.toHaveBeenCalledWith('/api', expect.anything());
3537
});
3638

39+
describe('when building with the Vite Environment API', () => {
40+
it('builds the sitemap into the public assets before the server', async () => {
41+
// Arrange
42+
const { buildServerImportSpy, buildSitemapImportSpy } =
43+
await mockBuildFunctions();
44+
const plugin = nitro({
45+
ssr: true,
46+
prerender: {
47+
routes: ['/', '/about'],
48+
sitemap: { host: 'https://example.com' },
49+
},
50+
});
51+
const config = (await (
52+
plugin[1].config as (
53+
config: UserConfig,
54+
env: ConfigEnv,
55+
) => Promise<UserConfig>
56+
)({}, { command: 'build' } as ConfigEnv)) as UserConfig;
57+
const builder = {
58+
environments: { client: {}, ssr: {} },
59+
build: vi.fn().mockResolvedValue(undefined),
60+
} as unknown as ViteBuilder;
61+
62+
// Act
63+
await config.builder?.buildApp?.(builder);
64+
65+
// Assert
66+
expect(buildSitemapImportSpy).toHaveBeenCalledTimes(1);
67+
expect(buildServerImportSpy).toHaveBeenCalledTimes(1);
68+
expect(buildSitemapImportSpy.mock.invocationCallOrder[0]).toBeLessThan(
69+
buildServerImportSpy.mock.invocationCallOrder[0],
70+
);
71+
72+
const nitroConfig = buildServerImportSpy.mock.calls[0][1] as NitroConfig;
73+
const sitemapDir = buildSitemapImportSpy.mock.calls[0][3];
74+
expect(sitemapDir).toBe(
75+
(nitroConfig.publicAssets as { dir: string }[])[0].dir,
76+
);
77+
expect(sitemapDir).not.toBe(nitroConfig.output?.publicDir);
78+
});
79+
});
80+
3781
describe.skip('when prerendering is configured...', () => {
3882
it('should build the server with prerender route "/" if nothing was provided', async () => {
3983
// Arrange

‎packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -559,36 +559,29 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
559559
'#analog/ssr': ssrEntry,
560560
};
561561

562-
await buildServer(
563-
options,
564-
nitroConfig,
565-
routeSourceFiles,
566-
i18nWorkers,
567-
);
568-
569562
if (
570563
nitroConfig.prerender?.routes?.length &&
571564
options?.prerender?.sitemap
572565
) {
573-
const publicDir = nitroConfig.output?.publicDir;
574-
if (!publicDir) {
575-
throw new Error(
576-
'Nitro public output directory is required to build the sitemap.',
577-
);
578-
}
579-
580566
console.log('Building Sitemap...');
581-
// sitemap needs to be built after all directories are built
567+
// build before the server so Nitro includes it in its public assets
582568
await buildSitemap(
583569
config,
584570
options.prerender.sitemap,
585571
nitroConfig.prerender.routes,
586-
publicDir,
572+
clientOutputPath,
587573
routeSitemaps,
588574
options.i18n,
589575
);
590576
}
591577

578+
await buildServer(
579+
options,
580+
nitroConfig,
581+
routeSourceFiles,
582+
i18nWorkers,
583+
);
584+
592585
console.log(
593586
`\n\nThe '@analogjs/platform' server has been successfully built.`,
594587
);

0 commit comments

Comments
 (0)