withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.41k stars 2.37k forks source link

Astro Actions .safe() function required empty object argument for Vercel deployment #11515

Closed Grahf0085 closed 1 month ago

Grahf0085 commented 1 month ago

Astro Info

Astro                    v4.12.2
Node                     v18.20.3
System                   Linux (x64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/solid-js

Describe the Bug

I have this logout action: const { data, error } = await actions.logoutUser.safe()

This woks fine on local host when running dev server and when using the node adapter and running the app using entry.mjs.

But when deployed to vercel I get this error when the logout function runs:

17:03:11 [ERROR] SyntaxError: Unexpected end of JSON input at JSON.parse () at parseJSONFromBytes (node:internal/deps/undici/undici:5584:19) at successSteps (node:internal/deps/undici/undici:5555:27) at fullyReadBody (node:internal/deps/undici/undici:1665:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:5564:7) at async Module.POST (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/route_Yu3iUWSv.mjs:17:12) at async renderEndpoint (file:///var/task/vercel/path0/.vercel/output/_functions/chunks/astro/server_CBvJsTK-.mjs:45:20) at async lastNext (file:///var/task/vercel/path0/.vercel/output/_functions/entry.mjs:1031:23) at async callMiddleware (file:///var/task/vercel/path0/.vercel/output/_functions/entry.mjs:480:10)

It's fixed by adding an empty object as a argument to the .safe() function: const { data, error } = await actions.logoutUser.safe({})

Not sure if it's document somewhere that an empty object needs to be passed to .safe() when it has no other arguments or if this is a bug.

To reproduce you will need to go to my site (https://sharper-than-time-c1ti0fvty-grahf0085s-projects.vercel.app/), make an account, and click the logout button. I can only produce this on vercel so.....

This is my logout action if it matters:

logoutUser: defineAction({
    handler: (_, context) => {
      const TOKEN = import.meta.env.TOKEN

      try {
        context.cookies.set(TOKEN, '', {
          httpOnly: true,
          maxAge: 0,
          path: '/',
          secure: true,
          sameSite: 'lax',
        })

        return { cookieDeleted: true }
      } catch (error) {
        console.error('Error logging out: ', error)
        throw new Error(error.message || error)
      }
    },
  }),

What's the expected result?

The logout function runs without producing an error

Link to Minimal Reproducible Example

https://sharper-than-time-c1ti0fvty-grahf0085s-projects.vercel.app/

Participation

ynhhoJ commented 1 month ago

Possible associated with #11423

bholmesdev commented 1 month ago

@ynhhoJ Hm, that issue is specific to Stackblitz and some edge environments. @Grahf0085 are you using Vercel edge middleware in your project?

bholmesdev commented 1 month ago

Update: I've built an example project using Vercel serverless with the SolidJS integration installed. I've confirmed that the following work as expected:

Code repository

Test deployment

I managed to get a build error by enabling edgeMiddleware. This is the same error that we receive from Netlify, and we have a fix planned. I unfortunately could not replicate the exact issue log you're seeing though.

I believe I've seen your issue log when an action returns undefined or a falsy value like 0. This was fixed and will be released in the next patch!

Would mind installing astro@latest and trying again? Note: there was a change to make .safe() the default behavior in actions as well. Please check the changelog for necessary refactors to use this patch.

Grahf0085 commented 1 month ago

@bholmesdev thanks for the follow up. As far as I know I am not using vercel edge middleware. I updated to Astro 4.13.0. Changed my logout action from: const { data, error } = await actions.logoutUser.safe({}) (this works) To: const { data, error } = await actions.logoutUser()

I got the same error message as when I was using const { data, error } = await actions.logoutUser.safe()

I'll keep an eye out for the next patch!

bholmesdev commented 1 month ago

Got it. Thanks for verifying @Grahf0085! We uncovered an issue with using response.clone() in https://github.com/withastro/astro/issues/11423 that I'm PR-ing a fix for shortly. We'll see if that affects anything in your environment. To be sure, you are not using bun, correct?