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: directly pass localFetch to route rules handler #1611

Merged
merged 1 commit into from
Aug 20, 2023
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
5 changes: 3 additions & 2 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ function createNitroApp(): NitroApp {
preemptive: true,
});

h3App.use(createRouteRulesHandler());

// Create local fetch callers
const localCall = createCall(toNodeListener(h3App) as any);
const _localFetch = createLocalFetch(localCall, globalThis.fetch);
Expand All @@ -110,6 +108,9 @@ function createNitroApp(): NitroApp {
// @ts-ignore
globalThis.$fetch = $fetch;

// Register route rule handlers
h3App.use(createRouteRulesHandler({ localFetch }));

// A generic event handler give nitro access to the requests
h3App.use(
eventHandler((event) => {
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import defu from "defu";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import { joinURL, withQuery, getQuery, withoutBase } from "ufo";
import { useRuntimeConfig } from "./config";
import { nitroApp } from "./app";
import type { NitroRouteRules } from "nitropack";

const config = useRuntimeConfig();
const _routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: config.nitro.routeRules })
);

export function createRouteRulesHandler() {
export function createRouteRulesHandler(ctx: {
localFetch: typeof globalThis.fetch;
}) {
return eventHandler((event) => {
// Match route options against path
const routeRules = getRouteRules(event);
Expand Down Expand Up @@ -48,7 +49,7 @@ export function createRouteRulesHandler() {
target = withQuery(target, query);
}
return proxyRequest(event, target, {
fetch: nitroApp.localFetch,
fetch: ctx.localFetch,
...routeRules.proxy,
});
}
Expand Down
Loading