Skip to content

Conversation

@ematipico
Copy link
Member

@ematipico ematipico commented Oct 24, 2025

Changes

This PR fixes the middleware usage when calling the getApp function in dev. The manifest didn't have the middleware getter function, which is in charge of resolving the middleware module.

This is now done by exposing the middleware virtual module in userland.

Testing

Added a new middleware and used some basic features:

  • redirect
  • rewrite
  • locals

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Oct 24, 2025

⚠️ No Changeset found

Latest commit: 615ebfe

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Oct 24, 2025
constructor(manifest: SSRManifest, streaming = true, logger: Logger) {
super(manifest, streaming, logger);
this.logger = logger;
this.manifestData = routesList;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed because it's done in base.ts

export function getApp(dev = import.meta.env.DEV): BaseApp {
if (dev) {
const logger = createConsoleLogger('debug');
return new DevApp(manifest, true, logger, { routes: routes.map((r) => r.routeData) });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewp
Copy link
Contributor

I'm a little confused by the description here. This is about getApp? I thought this was making it so that middleware works in the new environment API path in dev, no?

contents.push(`const pageMap = new Map([\n ${pageMap.join(',\n ')}\n]);`);
exports.push(`export { pageMap }`);
const middleware = await this.resolve(MIDDLEWARE_MODULE_ID);
const middleware = await this.resolve(MIDDLEWARE_RESOLVED_MODULE_ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Copy link
Member Author

@ematipico ematipico Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because up until now the middleware virtual module was never imported via import. Now we do that, so we need a simple name, and the one with \0

Copy link
Member

@florian-lefebvre florian-lefebvre Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I don't understand, why do we need to import the module with the null byte? Asking because my understanding is that Vite is supposed to be able to resolve the id without the null byte (which is kinda like an implementation detail of the virtual module)

@ematipico
Copy link
Member Author

I'm a little confused by the description here. This is about getApp? I thought this was making it so that middleware works in the new environment API path in dev, no?

Yes, and that's done by updating the getApp function

@matthewp
Copy link
Contributor

@ematipico I don't see that from the code, it looks like this adds middleware to the manifest and that's what makes it work. Don't mean to be nit-picky, but getApp is an API for creating a new App instance so seeing it discussed regarding middleware was confusing to me.

@ematipico
Copy link
Member Author

@matthewp what do I need to address? My description? My code? If so, how?

@matthewp
Copy link
Contributor

@ematipico yeah just the description, to specify that this fixes middleware support in the environment branch, and that the way that it fixes it is by adding them to them to the manifest.

Separately, the tests are currently failing.

export async function loadMiddleware(moduleLoader: ModuleLoader) {
try {
return await moduleLoader.import(MIDDLEWARE_MODULE_ID);
return await moduleLoader.import(MIDDLEWARE_RESOLVED_MODULE_ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should still import MIDDLEWARE_MODULE_ID to let Vite handle the module resolution. I'd be happy to be proved wrong tho

@ematipico ematipico merged commit 783daf6 into feat/environment-api Oct 28, 2025
8 of 18 checks passed
@ematipico ematipico deleted the env-api/middleware branch October 28, 2025 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants