Skip to content

useCookie() does not set Set-Cookie header when navigateTo() triggers redirect during SSR #1737

@yckimura

Description

@yckimura

Environment

Nuxt project info:

Operating system Linux 6.17.11+deb14-cloud-arm64
CPU unknown (4 cores)
Node.js version v22.22.0
nuxt/cli version 3.32.0
Package manager pnpm@10.29.3
Nuxt version 2.18.1
Nitro version 2.8.0
Builder webpack@4.47.0
Config -
Modules -
Build modules -

Reproduction

https://github.com/yckimura/bridge/pull/1/changes

This reproduction includes:

  • Test page: playground/pages/cookie-with-redirect.vue
  • Test case (still failing): test/bridge.test.ts
<template>
  <p>cookie-with-redirect.vue</p>
</template>

<script setup>
definePageMeta({
  middleware: defineNuxtRouteMiddleware(async () => {
    useCookie('redirect-test').value = 'foo'

    // Immediately redirect during SSR
    await navigateTo('/')
  })
})
</script>
it('should respond with set-cookie header even when SSR redirect occurs', async () => {
  const res = await fetch('/cookie-with-redirect', { redirect: 'manual' })

  // Verify redirect occurred
  expect(res.status).toBe(302)
  expect(res.headers.get('location')).toEqual('/')

  // Verify cookies were set
  const cookies = res.headers.get('set-cookie')
  expect(cookies).toBeTruthy() // <= Actual: null
  expect(cookies).toContain('redirect-test=foo')
})

Describe the bug

When useCookie() is used to set a cookie and navigateTo() is called immediately during SSR, the cookie is not written to the response headers. The cookie value is lost and the Set-Cookie header field is not sent.

Expected

Cookies set via useCookie() should be persisted in the response headers even when an SSR redirect occurs via navigateTo().

Actual

The Set-Cookie header is missing from the redirect response.

Additional context

No response

Logs

https://github.com/yckimura/bridge/actions/runs/22142694863/job/64010989045?pr=1#step:7:238

 FAIL  test/bridge.test.ts > nuxt composables > should respond with set-cookie header even when SSR redirect occurs
AssertionError: expected null to be truthy

- Expected: 
true

+ Received: 
null

 ❯ test/bridge.test.ts:67:21
     65|     // Verify cookies were set
     66|     const cookies = res.headers.get('set-cookie')

     67|     expect(cookies).toBeTruthy()
 Test Files  1 failed (1)
       |                     ^
     68|     expect(cookies).toContain('redirect-test=foo')
     69|   })

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions