Skip to content

Commit

Permalink
feat(vite-plugin-nitro): add support for running hooks during pre-ren…
Browse files Browse the repository at this point in the history
…dering (#548)

Co-authored-by: Q <q.ray@jbhunt.com>
  • Loading branch information
QuantariusRay and Q authored Jul 14, 2023
1 parent 0fe63ba commit 46af10e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/platform/src/lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PluginOptions } from '@analogjs/vite-plugin-angular';
import { NitroConfig } from 'nitropack';
import { NitroConfig, PrerenderRoute } from 'nitropack';
import { SitemapConfig } from '@analogjs/vite-plugin-nitro';

export interface PrerenderOptions {
Expand All @@ -13,6 +13,8 @@ export interface PrerenderOptions {
*/
routes?: string[] | (() => Promise<(string | undefined)[]>);
sitemap?: SitemapConfig;
/** List of functions that run for each route after pre-rendering is complete. */
postRenderingHooks?: ((routes: PrerenderRoute) => Promise<void>)[];
}

export interface Options {
Expand Down
6 changes: 6 additions & 0 deletions packages/vite-plugin-nitro/src/lib/build-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadEsmModule } from '@angular-devkit/build-angular/src/utils/load-esm';
import { NitroConfig } from 'nitropack';
import { Options } from './options';
import { addPostRenderingHooks } from './hooks/post-rendering-hook';

export async function buildServer(
options?: Options,
Expand All @@ -14,6 +15,11 @@ export async function buildServer(
preset: process.env['BUILD_PRESET'],
...nitroConfig,
});

if (options?.prerender?.postRenderingHooks) {
addPostRenderingHooks(nitro, options.prerender.postRenderingHooks);
}

await prepare(nitro);
await copyPublicAssets(nitro);

Expand Down
12 changes: 12 additions & 0 deletions packages/vite-plugin-nitro/src/lib/hooks/post-rendering-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Nitro, PrerenderRoute } from 'nitropack';

export function addPostRenderingHooks(
nitro: Nitro,
hooks: ((pr: PrerenderRoute) => Promise<void>)[]
): void {
hooks.forEach((hook: (preRoute: PrerenderRoute) => void) => {
nitro.hooks.hook('prerender:generate', (route: PrerenderRoute) => {
hook(route);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { addPostRenderingHooks } from './post-rendering-hook';
import { vi } from 'vitest';
import { Nitro } from 'nitropack';

describe('postRenderingHook', () => {
const genRoute = {
route: 'test/testRoute',
contents: 'This is a test.',
};

const nitroMock = {
hooks: {
hook: vi.fn((name: string, callback: (route) => void) =>
callback(genRoute)
),
},
} as Nitro;

const mockFunc1 = vi.fn();
const mockFunc2 = vi.fn();

it('should not attempt to call nitro mocks if no callbacks provided', () => {
addPostRenderingHooks(nitroMock, []);
expect(nitroMock.hooks.hook).not.toHaveBeenCalled();
});

it('should call provided hooks', () => {
addPostRenderingHooks(nitroMock, [mockFunc1, mockFunc2]);
expect(mockFunc1).toHaveBeenCalledWith(genRoute);
expect(mockFunc2).toHaveBeenCalled(genRoute);
});
});
4 changes: 4 additions & 0 deletions packages/vite-plugin-nitro/src/lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PrerenderRoute } from 'nitropack';

export interface Options {
ssr?: boolean;
ssrBuildDir?: string;
Expand All @@ -20,6 +22,8 @@ export interface PrerenderOptions {
*/
routes?: string[] | (() => Promise<(string | undefined)[]>);
sitemap?: SitemapConfig;
/** List of functions that run for each route after pre-rendering is complete. */
postRenderingHooks?: ((routes: PrerenderRoute) => Promise<void>)[];
}

export interface SitemapConfig {
Expand Down

0 comments on commit 46af10e

Please sign in to comment.