-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
refactor: singular middleware #9776
Conversation
🦋 Changeset detectedLatest commit: 59b97de 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.
Here's some feedback:
- let's revert the changes around i18n, they are intentional, and they aren't part of the middleware work
- I don't like that we create a middleware by default because this will always create a
middleware.mjs
file, which is weird is a user doesn't use a middleware at all. The fact thatonRequest
can beundefined
was a good compromise - I have some concerns on how this change affects edge middleware
.changeset/twenty-papayas-agree.md
Outdated
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.
I don't think we need a changeset if the code is just a refactor and it doesn't change/fix anything for the users
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.
I went back and forth on this. We can't say ever say for sure, there won't be regression and the changelog comes in handy. At the end, it's just another entry in the changelog.
@@ -263,7 +248,7 @@ function buildManifest( | |||
clientDirectives: Array.from(settings.clientDirectives), | |||
entryModules, | |||
assets: staticFiles.map(prefixAssetPath), | |||
i18n: i18nManifest, | |||
i18n: settings.config.i18n, |
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.
Let's revert this change. It's intentional to pass only a subset of information in the SSR manifest.
// The middleware should not be imported by the pages | ||
if (shouldBundleMiddleware(opts.settings)) { | ||
const middlewareModule = await this.resolve(MIDDLEWARE_MODULE_ID); | ||
if (middlewareModule) { | ||
imports.push(`import { onRequest } from "${middlewareModule.id}";`); | ||
exports.push(`export { onRequest };`); | ||
} | ||
} |
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.
Why was this logic removed? A middleware shouldn't be part of an Astro route/page when we are in the edge
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.
a middleware is now never part of an Astro route/page
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.
Curious why too. Related to edge?
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.
To detach middleware from routes. We had to import a route before importing middleware. Now, middleware lives in the manifest instead.
The snippet here serves the purpose of excluding middleware when it is supposed to go into edge middleware. That purpose is served by this line now.
@@ -139,6 +129,9 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest | |||
? new URL(settings.config.base, settings.config.site).toString() | |||
: settings.config.site, | |||
componentMetadata: new Map(), | |||
i18n: i18nManifest, | |||
i18n: settings.config.i18n, |
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.
To revert
This is a stop-gap change. Soon enough, it will not even be in the manifest.
There is a good reason for it. Middleware is a no-op in this PR but will soon contain internal rendering logic that goes in the "middle". As a bonus, we get to delete several nested if-else branches dealing with the undefined that are duplicated in multiple places.
Could you elaborate? |
I want to revert these changes:
In the adapters, we create an edge middleware if |
9459137
to
054460e
Compare
That's true, unfortunately. I'll take a look. |
@@ -145,10 +145,12 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn | |||
const baseDirectory = getOutputDirectory(opts.settings.config); | |||
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory); | |||
const renderers = await import(renderersEntryUrl.toString()); | |||
const { onRequest: middleware } = await import(new URL('middleware.mjs', baseDirectory).toString()); |
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 is new, can you explain why this is imported this way? I can see that it's passed into createBuildManifest
, how was the middleware previously imported in generation? I don't see an old import() that was removed.
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.
Previously, manifest was picked up from ssrEntry.
Here, it is being imported in the same way as renderers.
We could also cheat, and set |
That's my temptation as well! I want to sit on it for a day. If nothing else pops up in my head, I'll go ahead with that. |
054460e
to
2e6008a
Compare
Implemented a no-compromise solution - the middleware function will always exist, |
88cb7b3
to
59b97de
Compare
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.
LGTM! Nice refactors and I'm satisfied that you've addressed all feedback.
]; | ||
|
||
const contents = [ | ||
edgeMiddleware ? `const middleware = (_, next) => next()` : '', |
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.
Clever! I think this makes a lot of sense.
Changes
onRequest
function is internally re-exported from each page and endpoint, and loaded after the request matches a particular one.onRequest
from page chunks to the manifest, where it can be loaded sooner than route data.Testing
Does not affect behavior. Existing tests should pass.
Docs
Does not affect usage.