nuxt / nitro-demo

nuxt nitro preview
https://nitro-demo.nuxt.workers.dev/
187 stars 24 forks source link

Missing query params in request url for Netlify and Cloudflare Worker #20

Closed KABBOUCHI closed 3 years ago

KABBOUCHI commented 3 years ago

https://github.com/KABBOUCHI/nuxt-nitro-demo/blob/bc8fd5f0bd767598d1939eeb7e89c295a3d37596/server/api/hello.ts#L1-L13

// ./server/api/hello.ts
import { useQuery } from "h3";

export default (req) => {
  return {
    message: "hi",

    request: {
      url: req.url,
      method: req.method,
      query: useQuery(req),
    },
  };
};

Vercel: https://nuxt-nitro-demo.vercel.app/api/hello?test1=1&test2=2

{
   "message":"hi",
   "request":{
      "url":"?test1=1&test2=2",
      "method":"GET",
      "query":{
         "test1":"1",
         "test2":"2"
      }
   }
}

Netlify: https://nuxt-nitro-demo.netlify.app/api/hello?test1=1&test2=2

{
   "message":"hi",
   "request":{
      "url":"/",
      "method":"GET",
      "query":{

      }
   }
}

url should be event.path + event.queryStringParameters

// .output/server/chunks/nitro/lambda.js

async function handler(event, context) {
  const r = await localCall({
    event,
    url: event.path,
    context,
    headers: event.headers,
    method: event.httpMethod,
    query: event.queryStringParameters,
    body: event.body
  });
  return {
    statusCode: r.status,
    headers: r.headers,
    body: r.body.toString()
  };
}

possible solution:

const { withQuery } = require('ufo')

const r = await localCall({
    event,
    url: withQuery(event.path, event.queryStringParameters),
    ....

Cloudflare: https://nuxt-nitro-demo.kabbouchi.workers.dev/api/hello?test1=1&test2=2

{
   "message":"hi",
   "request":{
      "url":"/",
      "method":"GET",
      "query":{

      }
   }
}
danielroe commented 3 years ago

I appreciate the report - thanks @KABBOUCHI โค๏ธ

manniL commented 3 years ago

Resolved upstream - will be fixed with the next release ๐Ÿ˜‹

alexcroox commented 3 years ago

@manniL just tried v0.2.1, still getting req undefined in nuxtServerInit FYI

export const actions = {
  nuxtServerInit({ commit }, { req }) {
    // req is undefined here

However I am using Nuxt 2.15.4 and not nuxt-edge so apologies if that's the reason.

manniL commented 3 years ago

@manniL just tried v0.2.1, still getting req undefined in nuxtServerInit FYI

export const actions = {
  nuxtServerInit({ commit }, { req }) {
    // req is undefined here

However I am using Nuxt 2.15.4 and not nuxt-edge so apologies if that's the reason.

UPDATE Could you provide a repro for this scenario? The missing req/res object should've been fixed with Nitro v0.2.1

Old

Having an undefined req object is not part of the original issue reported here. However, I've experienced a similar behavior a couple of days ago and reported the bug :)

So - WIP again ๐Ÿ˜‹ -

alexcroox commented 3 years ago

Apologies, I reported the missing req in CF via PM a few weeks back and mistook this to be similar. I'll create a repro and create a new issue to avoid polluting this one ๐Ÿ‘

manniL commented 3 years ago

Apologies, I reported the missing req in CF via PM a few weeks back and mistook this to be similar. I'll create a repro and create a new issue to avoid polluting this one ๐Ÿ‘

Just double checked and can still repro missing req when using nitro, even in dev mode. Maybe squashing this bug could resolve the issue in prod too.

Released in v0.2.2

KABBOUCHI commented 3 years ago

@manniL I was trying the new update but there a new issue:

Absolute path is throwing an error (full URL is working fine)

$fetch('/api/quotes')

image

this repo/demo has this error too https://nitro-demo.vercel.app/api

probably from this: https://github.com/nuxt/nitro-demo/blob/a469eb6eaaed301b79eaa841b64e3b2588fe9f4e/pages/api.vue#L37-L39

pi0 commented 3 years ago

Thanks for report @KABBOUCHI. API error was due to a regression in h3. All should be fine in the latest releases. https://nitro-demo.vercel.app/api