Replies: 7 comments 10 replies
-
|
By coincidence, I noticed this commit: Which made me think if those checks would be reversed, e.g.: if (app.match(request)) {
return app.render(request);
}
if (manifest.assets.has(url.pathname)) {
return;
}That would potentially give us a chance to react to an asset request, e.g.:
export async function get(_, request) {
const auth = isLoggedIn();
const url = new URL(request.url);
const protectedRoutes = new URLPattern({pathname: '/protected/images/:image'});
const match = protectedRoutes.exec(url);
if(match) {
if(auth) {
return;
} else {
return new Response(null, {status: 403});
}
}
return;
} |
Beta Was this translation helpful? Give feedback.
-
|
I've got a scenario where I have an SSR site with multilingual support where you can visit the site with either a But if you visit the site with a prefix that isn't support, it should redirect to the default language, const redir = localization.initLang(Astro);
if (redir) {
return redir;
}My It would be great if I could add a middleware that would allow me to add this logic to every page automatically. |
Beta Was this translation helpful? Give feedback.
-
|
Middleware would be also a solution for my current issue: I want to set my own |
Beta Was this translation helpful? Give feedback.
-
|
I would love something like SvelteKit's hooks.
A simple hooks look something like this: export const handle = async ({ event, resolve }) => {
const response = await resolve(event);
return response;
}The One thing super useful is export const handle = async ({ event, resolve }) => {
event.locals.session = await getSessionFromRequest(event.request);
const response = await resolve(event); // event.locals.session available in route handler
return response;
}You can even add functions to it, allowing to create functions that read and modify the request and response without passing on the Though the simplicity kinda falls apart when you have multiple This is might be a deal breaker if you want have community supported middleware, like Express. tldr; It'd be great if you can set headers and cookies in middleware, as well as share data between middleware and .astro files. |
Beta Was this translation helpful? Give feedback.
-
|
I think it would be great to tackle middleware. I think what we really need is a good list of goals / non-goals. One particular goal I think we should shoot for is having the middleware work in both dev and prod, no matter which adapter you are using. Some past proposals have not done this, they were prod only. Another thing I think we need here before we start picking a specific API is a comparison between other frameworks and how they solve this issue. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for presenting this one back in the day @thepassle! The Astro team recently moved rfcs to this new roadmap repo to better track pre-RFC discussions and proposals. You can find our new public roadmap here. I'm curious if you'd want to lead a proposal on this idea with more details fleshed out? There's definitely interest in the idea, but we'd love to have a community champion to start shaping goals and updated usage examples (see our new proposal template here). If you're interested, feel free to update the discussion based on the new template. You can share around the discord for feedback or input as well! |
Beta Was this translation helpful? Give feedback.
-
|
Middleware has been accepted and has moved to stage 2 (based on @pilcrowonpaper's new proposal): #531 |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was trying to figure out if there was any way to have route handlers act as 'middleware' of sorts, but I couldn't find anything.
Usecase
Maybe I have some static assets that I'd like to only make available for logged in users, e.g.:
/protected/[...a]/index.js:request (user is logged in):
/protected/my-img.png-> 200request (user is not logged in):
/protected/my-img.png-> 403Like for example in a service worker you would be able to just forward the request like:
Most of all it would be really nice to just be able to forward a request by either calling a
next()function, or just returningfetch(request), e.g.:Alternative syntax for middleware could be instead of exporting a
getfunction, exporting agetarray:/my-route.js:or even returning an array from the function:
Beta Was this translation helpful? Give feedback.
All reactions