Skip to content
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

Middleware works locally, doesn't run at all when deployed to Vercel #9357

Closed
1 task
bstro opened this issue Dec 7, 2023 · 5 comments · Fixed by #9585
Closed
1 task

Middleware works locally, doesn't run at all when deployed to Vercel #9357

bstro opened this issue Dec 7, 2023 · 5 comments · Fixed by #9585
Labels
ecosystem: upstream Upstream package has issue feat: middleware Related to middleware (scope) pkg: vercel Related to Vercel adapter (scope)

Comments

@bstro
Copy link

bstro commented Dec 7, 2023

Astro Info

Astro                    v4.0.3
Node                     v21.1.0
System                   macOS (x64)
Package Manager          yarn
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Trying to deploy my Astro app to vercel. I wrote a middleware that implements basic HTTP authentication.

When I run my app locally on the astro dev server, everything works as expected—the user/password prompt appears and if I enter the correct credentials, I am forwarded to the correct page.

When I deploy to Vercel, there is no authentication at all, as if the middleware isn't being run.

What's the expected result?

Middleware should behave identically whether I am running the project locally or when deploying to Vercel.
Stackblitz: https://stackblitz.com/~/github.com/bstro/github-pw6m1w (note the Auth required response)
Deployment: https://github-pw6m1w.vercel.app/ (note that the middleware doesn't seem to run at all)

Link to Minimal Reproducible Example

https://stackblitz.com/~/github.com/bstro/github-pw6m1w

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 7, 2023
@ematipico
Copy link
Member

ematipico commented Dec 7, 2023

You're right, the middleware gets generated, but I can't see the edge middleware file being generated. This is very weird.

@ematipico ematipico added - P4: important Violate documented behavior or significantly impacts performance (priority) pkg: vercel Related to Vercel adapter (scope) feat: middleware Related to middleware (scope) and removed needs triage Issue needs to be triaged labels Dec 7, 2023
@ematipico
Copy link
Member

ematipico commented Dec 7, 2023

@bstro I further checked, and it seems that we are emitting the edge middleware correctly, and it seems to be a Vercel issue where it doesn't run the file middleware.mjs. (although middleware.js works)

I'll keep you posted.

My advice for now it to not use the edge middleware feature for now, although not sure sure it that would work for you.

@Mokto
Copy link

Mokto commented Dec 10, 2023

What if we run something like : babel --plugins @babel/plugin-transform-modules-commonjs .vercel/output/functions/render.func/dist/middleware.mjs > .vercel/output/functions/render.func/dist/middleware.js ? To transform the file back into something vercel can understand.

@ematipico
Copy link
Member

What if we run something like : babel --plugins @babel/plugin-transform-modules-commonjs .vercel/output/functions/render.func/dist/middleware.mjs > .vercel/output/functions/render.func/dist/middleware.js ? To transform the file back into something vercel can understand.

You could try it on your local environment, but I think Vercel must support this format. After all they support it for their normal functions.

@ematipico ematipico added ecosystem: upstream Upstream package has issue and removed - P4: important Violate documented behavior or significantly impacts performance (priority) labels Dec 19, 2023
@lilnasy
Copy link
Contributor

lilnasy commented Dec 29, 2023

Switching middleware.mjs to middleware.js made no difference. It doesn't seem to be part of Vercel's build output API at all.

A comment in the relevant part of the adapter points to the convention of middleware.js in vercel docs. However, the docs talk about the middleware file in root, next to astro.config.mjs. However, the code puts that file inside the compiled function instead.

Sure enough, putting the middleware in project root works.

image

...but not before it complained about being bundled incorrectly...

image

...which makes me wonder how edge middleware ever worked, if it did - vercel expects the edge middleware to be its own function in this format which we never created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ecosystem: upstream Upstream package has issue feat: middleware Related to middleware (scope) pkg: vercel Related to Vercel adapter (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants