Skip to content

Commit

Permalink
Merge pull request #71 from nksaraf/req-url
Browse files Browse the repository at this point in the history
fix: reset req.url for vite middleware
  • Loading branch information
nksaraf authored Dec 30, 2023
2 parents 4630198 + aa08e9e commit d086609
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-emus-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

fix: reset req.url for vite middleware
43 changes: 41 additions & 2 deletions packages/vinxi/lib/router-modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,25 @@ const routerModes = {
},
handler: async (router, app, serveConfig) => {
const { createViteHandler } = await import("./dev-server.js");
const { joinURL } = await import("ufo");
const { fromNodeMiddleware, eventHandler } = await import("h3");
const viteDevServer = await createViteHandler(router, app, serveConfig);

viteDevServer.middlewares.stack.unshift({
route: "",
handle: async (req, res, next) => {
// console.log(req.url, req.originalURL)
req.__preViteUrl = req.url;
req.url = joinURL(
app.config.server.baseURL ?? "",
router.base,
req.url,
);
await next();
req.url = req.__preViteUrl;
},
});

return {
route: router.base,
handler: eventHandler({
Expand Down Expand Up @@ -203,7 +219,9 @@ const routerModes = {
return await ROUTER_MODE_DEV_PLUGINS.handler(router);
},
handler: async (router, app, serveConfig) => {
const { eventHandler } = await import("../runtime/server.js");
const { eventHandler, fromNodeMiddleware } = await import(
"../runtime/server.js"
);
if (router.mode === "handler" && router.worker && isMainThread) {
if (!router.internals.appWorker) {
const { AppWorkerClient } = await import("./app-worker-client.js");
Expand Down Expand Up @@ -237,7 +255,13 @@ const routerModes = {

const { createViteHandler } = await import("./dev-server.js");
const viteServer = await createViteHandler(router, app, serveConfig);
const viteHandler = fromNodeMiddleware(viteServer.middlewares);

const handler = eventHandler(async (event) => {
await viteHandler(event);
if (event.handled) {
return;
}
const { default: handler } = await viteServer.ssrLoadModule(
handlerModule(router),
);
Expand Down Expand Up @@ -304,9 +328,24 @@ const routerModes = {
fromNodeMiddleware,
getRequestURL,
} = await import("../runtime/server.js");

const { joinURL } = await import("ufo");
const viteDevServer = await createViteHandler(router, app, serveConfig);

viteDevServer.middlewares.stack.unshift({
route: "",
handle: async (req, res, next) => {
// console.log(req.url, req.originalURL)
req.__preViteUrl = req.url;
req.url = joinURL(
app.config.server.baseURL ?? "",
router.base,
req.url,
);
await next();
req.url = req.__preViteUrl;
},
});

if (router.handler.endsWith(".html")) {
return [
{
Expand Down

0 comments on commit d086609

Please sign in to comment.