-
-
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(astro): experimental middleware #6721
Conversation
🦋 Changeset detectedLatest commit: e8e5007 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 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This PR is blocked because it contains a |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
e05bfe0
to
033173a
Compare
This PR is blocked because it contains a |
!preview middleware |
033173a
to
35c8652
Compare
This PR is blocked because it contains a |
35c8652
to
ce9013d
Compare
This PR is blocked because it contains a |
|
ce9013d
to
6804f5c
Compare
This PR is blocked because it contains a |
6804f5c
to
c4acb57
Compare
This PR is blocked because it contains a |
!preview middleware |
4e5a017
to
f844c7c
Compare
|
!preview middleware |
|
fb79199
to
a724a92
Compare
@sarah11918 could you double-check again if the docs are correct, please? |
Co-authored-by: Sarah Rainsberger <[email protected]>
* Thrown in development mode, when `locals` are overridden with something that is not an object | ||
* | ||
* For example: | ||
* ```ts | ||
* import {defineMiddleware} from "astro/middleware"; | ||
* export const onRequest = defineMiddleware((context, next) => { | ||
* context.locals = 1541; | ||
* return next(); | ||
* }); | ||
* ``` | ||
*/ | ||
LocalsNotAnObject: { | ||
title: 'Value assigned to `locals` is not accepted.', | ||
code: 3033, | ||
message: `The \`locals\` can only be assigned to an object. Other values like numbers, strings, etc. are not accepted.`, | ||
hint: 'If you tried to remove some information from the `locals` object, try to use `delete` or set the property to `undefined`.', | ||
}, |
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.
* Thrown in development mode, when `locals` are overridden with something that is not an object | |
* | |
* For example: | |
* ```ts | |
* import {defineMiddleware} from "astro/middleware"; | |
* export const onRequest = defineMiddleware((context, next) => { | |
* context.locals = 1541; | |
* return next(); | |
* }); | |
* ``` | |
*/ | |
LocalsNotAnObject: { | |
title: 'Value assigned to `locals` is not accepted.', | |
code: 3033, | |
message: `The \`locals\` can only be assigned to an object. Other values like numbers, strings, etc. are not accepted.`, | |
hint: 'If you tried to remove some information from the `locals` object, try to use `delete` or set the property to `undefined`.', | |
}, | |
* Thrown in development mode when `locals` is overwritten with something that is not an object | |
* | |
* For example: | |
* ```ts | |
* import {defineMiddleware} from "astro/middleware"; | |
* export const onRequest = defineMiddleware((context, next) => { | |
* context.locals = 1541; | |
* return next(); | |
* }); | |
* ``` | |
*/ | |
LocalsNotAnObject: { | |
title: 'Value assigned to `locals` is not accepted', | |
code: 3033, | |
message: '`\locals\` can only be assigned to an object. Other values like numbers, strings, etc. are not accepted.', | |
hint: 'If you tried to remove some information from the `locals` object, try to use `delete` or set the property to `undefined`.', | |
}, |
Lots of things I'm unsure of! 😅 So these are just things I'll point out that might need a closer look to make sure it's what you mean/want:
- I might consider
locals
singular, even though the word is plural? If it's onelocals
object, then I think singular fits? So I'd say "locals is..." locals
could be "overriden" or could be "overwritten" depending on what's actually happening. I just want to make sure that "override" is what you're going for, and you didn't actually prefer or mean "overWRITE". (I think of override in the sense of controls, or what's going to take precedence. But, I would probably think "overwrite" if the object contents are in fact being changed/rewritten.)- I'm not sure about the backtick symbols being used in the
message
above. I changed the outer ones to single quotes, but I'm not sure exactly what you want within code ticks around locals. So just check on that and make it wahtever it should be. 😄 - the other
title
s did not have periods at the end, so I removed this one, but whatever you want for titles is fine. Just pick a style!
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.
might consider locals singular, even though the word is plural? If it's one locals object, then I think singular fits? So I'd say "locals is..."
Yes, I would consider locals
like a name, other than something we quantify.
locals could be "overriden" or could be "overwritten" depending on what's actually happening. I just want to make sure that "override" is what you're going for, and you didn't actually prefer or mean "overWRITE". (I think of override in the sense of controls, or what's going to take precedence. But, I would probably think "overwrite" if the object contents are in fact being changed/rewritten.)
I mean "override". The user can overwrite the contents of locals
as they want. The user is free. But they can't do something like locals = "new string"
(override). That is forbidden and will throw this very error.
Co-authored-by: Sarah Rainsberger <[email protected]>
title: '`Astro.locals` are not serializable.', | ||
code: 3034, | ||
message: (href: string) => { | ||
return `The information stored in \`Astro.locals\` are not serializable when visiting "${href}" path.\nMake sure you store only data that are serializable.`; |
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.
return `The information stored in \`Astro.locals\` are not serializable when visiting "${href}" path.\nMake sure you store only data that are serializable.`; | |
return `The information stored in \`Astro.locals\` is not serializable when visiting "${href}" path.\nMake sure you store only serializable data.`; |
Information is singular, and I'm not sure about "when visiting... path" If that renders to e.g. "when visiting /about/ path" then that sentence wouldn't read very well in English.
What exactly is the signifiance of including the part about the path? Would the warning work without it, or is the message depending on knowing which path is causing the error? In that case, instead of "when visiting the path" what is the actual problem? The route can't be created? The path doesn't exist?
The information stored in Astro.locals for the path "${href} is not serializable. Make sure...
Would something closer to that be possible?
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.
What exactly is the signifiance of including the part about the path?
The logic of the middleware can change based on the page the user is visiting. So having a path can help the developer understand the issue.
The information stored in Astro.locals for the path "${href} is not serializable. Make sure...
Would something closer to that be possible?
That's perfect!
@ematipico you have a typo in description also it looks like MiddlewareHandler requires generic type passed to it |
* See withastro/astro#4986 for `site`, `generator`, `url`, `clientAddress`, `props` and `redirect` * See withastro/astro#6721 for `locals` * See withastro/astro#9021 for `preferredLocale` and `preferredLocaleList` * See withastro/astro#9101 for `currentLocale`
* See withastro/astro#4986 for `site`, `generator`, `url`, `clientAddress`, `props` and `redirect` * See withastro/astro#6721 for `locals` * See withastro/astro#9021 for `preferredLocale` and `preferredLocaleList` * See withastro/astro#9101 for `currentLocale`
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
Changes
"astro"
package to use the preview release:src/
, createmiddleware/index.js
or a file calledsrc/middleware.js
. Or.ts
; it's up to you.onRequest
You can manipulate the object to your heart content.
.astro
file (any file), access tolocals
sequence
API provided by theastro/middleware
module:resolve
function to retrieve and act on the response. When usingresolve
, the middleware must return the response.context
object (APIContext
type). It can becontext.request.url
,context.params
, etc.This PR implements the Astro middleware under an experimental flag. Please refer to the RFC for more information.
Testing
I created multiple test cases for
development
andproduction
modes. The production tests use the preview feature and don't use an SSR adapter.Docs
cc
cc @withastro/maintainers-docs for feedback! Here's an issue where we can collect feedback: withastro/docs#3069