Giter Site home page Giter Site logo

Comments (5)

manniL avatar manniL commented on July 24, 2024 1

@danekslama this is probably #27660

from nuxt.js.

danielroe avatar danielroe commented on July 24, 2024

When using a custom fetch, Nuxt does not perform 'internal' fetches or pass on headers automatically - you will need to do this manually if that's the behaviour you want.

from nuxt.js.

danekslama avatar danekslama commented on July 24, 2024

@danielroe we have a similar issue. It seems that cookies are not somehow reactive in the newest version. It was working before the update to 3.12.2.

Tested on 3.11.2 and working correctly.

from nuxt.js.

MickL avatar MickL commented on July 24, 2024

@danielroe

How can I have a custom fetch but with all the behavior as before? The docs didnt say anything about the downsides of missing functionality when using custom fetch. Is there any example that has custom fetch but with everything as before?

I just want to have an interceptor that redirects if any response is 401 but this seems to be the most complicated thing ever :(

from nuxt.js.

Aareksio avatar Aareksio commented on July 24, 2024

@MickL The docs are not very clear, but you need to recreate the functionality you need:
https://nuxt.com/docs/getting-started/data-fetching#passing-headers-and-cookies

In practice, for cookies, you need something akin to:

export const myFetch = $fetch.create({
  async onRequest(ctx) {
    if (import.meta.server) {
      // Forward cookies to the target
      ctx.options.headers = useRequestHeaders(['cookie'])

      // Save event for later, we need it to forward response cookies
      ;(ctx as any).event = useRequestEvent()
    }
  },
  async onResponse(ctx) {
    if (import.meta.server && ctx.response.headers.get('set-cookie')) {
      // Forward response cookies to the client
      const cookieHeader = ctx.response.headers.get('set-cookie')!
      appendResponseHeader((ctx as any).event, 'set-cookie', cookieHeader)
    }
  },
})

After investigating your reproduction, it does not forward any cookie on SSR. You must have been setting it via client-side request in your tests:

Reload page 1: Cookie is not set
Navigate to page 2, back to page 1: Cookie is set

from nuxt.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.