-
Hi all, I have a question if is it planned to be able to rewrite request in middleware? Or something like that. This pattern is used for example in ParaglideJS. NextJS import { NextRequest, NextResponse } from "next/server";
import { paraglideMiddleware } from "./paraglide/server";
export function middleware(request: NextRequest) {
return paraglideMiddleware(request, ({ request, locale }) => {
request.headers.set("x-paraglide-locale", locale);
request.headers.set("x-paraglide-request-url", request.url);
return NextResponse.rewrite(request.url, request);
});
} HonoJS export default await createHonoServer({
beforeAll(app) {
app.use(async (c, next) => {
return await paraglideMiddleware(c.req.raw, ({ request }) => {
c.req.raw = request;
return next();
});
});
},
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
What's the use case? If you want to add things for the route loaders/actions to use you can use the router context to set values there and read them somewhere else without modifying the actual request object. If the idea is to make |
Beta Was this translation helpful? Give feedback.
-
For example if you have the language detection strategy set to prioritize the locale value from the cookie over the locale value from the url, the middleware can automatically change (rewrite) the url. Base language: de
Without this feature actually it is not possible to use a new ParaglideJS 2.0 middleware. :( |
Beta Was this translation helpful? Give feedback.
-
I'm trying to create an admin-panel, that needs authorization. The page already has an error boundary that deals with 403 grcefully. So far I was doing the authorization in the loader and throwing a 403 from there - and the error boundary was triggering correctly. Now, I'm trying to throw a Response from the middleware - but that directly gets sent to client, without actually going the the page. Returning a 403 Response is not helping either. What's the right way to handle such scenarios? I basically want to short-circuit everything and directly throw a 403, but I also want a good-looking error boundary. (Without redirecting the user to a new /403 page, preferably) |
Beta Was this translation helpful? Give feedback.
@vampcat Yeah You are right. This is working code: