Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite-plugin-nitro): use proxy for API requests instead of custom middleware #1378

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/platform/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export interface Options {
* Additional files to include in compilation
*/
include?: string[];
/**
* Toggles internal API middleware.
* If disabled, a proxy request is used to route /api
* requests to / in the production server build.
*/
useAPIMiddleware?: boolean;
}

export interface PrerenderContentDir {
Expand Down
12 changes: 0 additions & 12 deletions packages/platform/src/lib/platform-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,4 @@ describe('platformPlugin', () => {
expect(viteNitroPluginSpy).toHaveBeenCalledWith({ ssr: false }, undefined);
expect(ssrBuildPluginSpy).not.toHaveBeenCalled();
});

it('should pass the custom endpoint as part of the nitro runtimeConfig if options.apiPrefix is set to false', async () => {
const customPrefix = 'custom-endpoint';
const { viteNitroPluginSpy, platformPlugin } = await setup();
platformPlugin({ apiPrefix: customPrefix });

expect(viteNitroPluginSpy).toHaveBeenCalledWith(expect.anything(), {
runtimeConfig: {
apiPrefix: customPrefix,
},
});
});
});
10 changes: 1 addition & 9 deletions packages/platform/src/lib/platform-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ import { depsPlugin } from './deps-plugin.js';
import { injectHTMLPlugin } from './ssr/inject-html-plugin.js';

export function platformPlugin(opts: Options = {}): Plugin[] {
const { apiPrefix, ...platformOptions } = {
const { ...platformOptions } = {
ssr: true,
...opts,
};

let nitroOptions = platformOptions?.nitro;
if (apiPrefix) {
nitroOptions = {
...nitroOptions,
runtimeConfig: {
apiPrefix,
},
};
}

return [
...viteNitroPlugin(platformOptions, nitroOptions),
Expand Down
8 changes: 8 additions & 0 deletions packages/vite-plugin-nitro/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export interface Options {
* Additional API paths to include
*/
additionalAPIDirs?: string[];
apiPrefix?: string;

/**
* Toggles internal API middleware.
* If disabled, a proxy request is used to route /api
* requests to / in the production server build.
*/
useAPIMiddleware?: boolean;
}

export interface PrerenderOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const mockNitroConfig: NitroConfig = {
},
],
},
routeRules: undefined,
};

export async function mockBuildFunctions() {
Expand Down
35 changes: 26 additions & 9 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const __dirname = dirname(__filename);
export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
const workspaceRoot = options?.workspaceRoot ?? process.cwd();
const isTest = process.env['NODE_ENV'] === 'test' || !!process.env['VITEST'];
const apiPrefix = `/${nitroOptions?.runtimeConfig?.['apiPrefix'] ?? 'api'}`;
const apiPrefix = `/${options?.apiPrefix || 'api'}`;
const useAPIMiddleware =
typeof options?.useAPIMiddleware !== 'undefined'
? options?.useAPIMiddleware
: true;

let isBuild = false;
let isServe = false;
Expand Down Expand Up @@ -120,12 +124,21 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
plugins: [pageEndpointsPlugin()],
},
handlers: [
{
handler: apiMiddlewareHandler,
middleware: true,
},
...(useAPIMiddleware
? [
{
handler: apiMiddlewareHandler,
middleware: true,
},
]
: []),
...pageHandlers,
],
routeRules: useAPIMiddleware
? undefined
: {
[`${apiPrefix}/**`]: { proxy: { to: '/**' } },
},
};

if (isVercelPreset(buildPreset)) {
Expand Down Expand Up @@ -260,10 +273,14 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
},
moduleSideEffects: ['zone.js/node', 'zone.js/fesm2015/zone-node'],
handlers: [
{
handler: apiMiddlewareHandler,
middleware: true,
},
...(useAPIMiddleware
? [
{
handler: apiMiddlewareHandler,
middleware: true,
},
]
: []),
...pageHandlers,
],
};
Expand Down
Loading