+
Skip to content

fix(vite-plugin-nitro): handle Nitro Base URL for API routes in development #1653

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

Merged
merged 1 commit into from
Mar 18, 2025
Merged
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
14 changes: 9 additions & 5 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const __dirname = dirname(__filename);
export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
const workspaceRoot = options?.workspaceRoot ?? process.cwd();
let isTest = process.env['NODE_ENV'] === 'test' || !!process.env['VITEST'];
const baseURL = process.env['NITRO_APP_BASE_URL'] || '';
const prefix = baseURL ? baseURL.substring(0, baseURL.length - 1) : '';
const apiPrefix = `/${options?.apiPrefix || 'api'}`;
const useAPIMiddleware =
typeof options?.useAPIMiddleware !== 'undefined'
Expand Down Expand Up @@ -122,6 +124,7 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
},
runtimeConfig: {
apiPrefix: apiPrefix.substring(1),
prefix,
},
rollupConfig: {
onwarn(warning) {
Expand Down Expand Up @@ -152,7 +155,7 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
: useAPIMiddleware
? undefined
: {
[`${apiPrefix}/**`]: {
[`${prefix}${apiPrefix}/**`]: {
proxy: { to: '/**' },
},
},
Expand All @@ -164,7 +167,8 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
import { useRuntimeConfig } from '#imports';

export default eventHandler(async (event) => {
const apiPrefix = \`/\${useRuntimeConfig().apiPrefix}\`;
const prefix = useRuntimeConfig().prefix;
const apiPrefix = \`\${prefix}/\${useRuntimeConfig().apiPrefix}\`;

if (event.node.req.url?.startsWith(apiPrefix)) {
const reqUrl = event.node.req.url?.replace(apiPrefix, '');
Expand Down Expand Up @@ -411,7 +415,7 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
if (hasAPIDir) {
viteServer.middlewares.use(
(req: IncomingMessage, res: ServerResponse, next: Function) => {
if (req.url?.startsWith(apiPrefix)) {
if (req.url?.startsWith(`${prefix}${apiPrefix}`)) {
apiHandler(req, res);
return;
}
Expand All @@ -436,7 +440,7 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
}

console.log(
`\n\nThe server endpoints are accessible under the "${apiPrefix}" path.`,
`\n\nThe server endpoints are accessible under the "${prefix}${apiPrefix}" path.`,
);
}
},
Expand Down Expand Up @@ -484,7 +488,7 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
config() {
return {
define: {
ANALOG_API_PREFIX: `"${apiPrefix.substring(1)}"`,
ANALOG_API_PREFIX: `"${baseURL.substring(1)}${apiPrefix.substring(1)}"`,
},
};
},
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载