-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat: Add isRedirect to context #13143
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: daae1fe The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
CodSpeed Performance ReportMerging #13143 will not alter performanceComparing Summary
|
Unfortunately, it seems that the discussion in the linked issue has a lot of known knowledge, so I miss some context. Before adding new runtime APIs, we usually want to make sure they are needed. Can you please do a TLDR of why you need it? Also, can't do something like the following snippet const response = await next();
if (response.status >= 300 && response.status < 400) {} |
To sum up my discovery process, the fundamental issue is that I think there needs to be some flag (or combination of flags) on the runtime API that indicates if I'm open to alternatives to
I thought about this, but Arcjet is a unique case here. It is a library for request protection (such as running a WAF, validating emails, detecting bots), so you wouldn't want to run every request through the middleware before protecting, since that would execute each other middleware before users would have a chance to validate the request. The current recommendation is to not run Arcjet protections on prerendered routes because they are just static files, so we recommend using a middleware like: import { defineMiddleware } from "astro:middleware";
import aj from "arcjet:client";
export const onRequest = defineMiddleware(async (ctx, next) => {
if (!ctx.isPrerendered) {
const decision = await aj.protect(ctx.request);
if (decision.isDenied()) {
return new Response(null, { status: 403, statusText: "Forbidden" });
}
}
return next();
}); We additionally don't need to run the protections on redirects, since the redirect should point to either a static page where no protections will need to be run or a dynamic page where the protections will be run. With the import { defineMiddleware } from "astro:middleware";
import aj from "arcjet:client";
export const onRequest = defineMiddleware(async (ctx, next) => {
if (!ctx.isPrerendered && !ctx.isRedirect) {
const decision = await aj.protect(ctx.request);
if (decision.isDenied()) {
return new Response(null, { status: 403, statusText: "Forbidden" });
}
}
return next();
}); But as I said above, I would be happy with any API that indicated if |
Changes
I added the
isRedirect
field to the Render context. This can help users avoid running middleware on routes that might just be a redirect.For example, the Arcjet integration fails if run on a redirect because
isPrerendered
is false, butclientAddress
throws if accessed; however, it would be generally recommended not to run Arcjet on a redirect because it'll run on the final endpoint. With this change, we can recommend users skip Arcjet whenisRedirect
is true.Testing
I added a test to the redirects fixture that checks a header that is set by the middleware.
Docs
Yep! I'll add this to the Astro docs for Render context if this is something y'all want to include. 🎉
/cc @withastro/maintainers-docs for feedback!