Commit fc7f07d
committed
Capture the SSR request cookie eagerly so nested fetches stay authenticated (#263)
`CwaFetch`'s ofetch `onRequest` called `useRequestHeaders(['cookie'])`,
which threw `[nuxt] instance unavailable` for every nested/batch resource
fetched during SSR.
The reported mechanism — "ofetch runs interceptors asynchronously" — is
wrong. `$fetchRaw`'s body runs synchronously up to its first `await`, and
`callHooks` is invoked before it suspends, so `onRequest` inherits its
caller's context.
The real cause is in our own code. `useRequestHeaders` resolves the Nuxt
instance via `useNuxtApp()`, which throws rather than degrading. Nuxt's
`asyncContext` defaults to false, so unctx keeps that instance in a plain
module variable and clears it at the first suspension, and its
`__restore()` hook only covers code rewritten by `unctx/transform` — a
closed list of `defineNuxtPlugin`, `defineNuxtRouteMiddleware` and
friends. `fetcher.ts` is an untransformed plain class, so every `await`
in it destroys the context for everything downstream: the primary fetch
reached the interceptor synchronously and worked, while everything after
`await result.response` -> `fetchBatch` threw. ofetch's retry path loses
the context too, re-entering `onRequest` after a `setTimeout`.
It hid well: the throw surfaces as a rejected promise, so each nested
resource was quietly marked errored instead of crashing the render. And
`credentials: 'include'` does nothing server-side — no cookie jar in
undici — so that header append is the ONLY thing forwarding auth cookies
during SSR, and authenticated SSR requests silently downgraded to
anonymous. Not a regression despite the "latest deps" framing:
`asyncContext: false` is unchanged and the interceptor has not changed
since dfcf340 (Jan 2025).
Capture the cookie in the constructor instead, where the plugin still has
a live Nuxt context, and close over it. `onRequest` stays synchronous and
needs no context, so nested resources and retries both work.
Safe because exactly one CwaFetch exists per Cwa per plugin invocation —
per SSR request; `new Cwa(` and `new CwaFetch(` each have exactly one
non-spec call site. The cookie must stay in that closure: hoisting it to
module scope would leak one user's auth cookie into another's request.
The constructor already requires Nuxt context for `useRuntimeConfig()`
and `useCookie()`, so this widens nothing, and the only cookie mutations
(sign-in/sign-out) are client-side actions.
Rejected `runWithContext()` in the interceptor: on the server it always
returns a Promise, forcing an async interceptor and changing request
timing. Rejected app-side `experimental.asyncContext: true`: a module
must not require an app-level experimental flag to forward auth cookies.
cwa-fetch.spec.ts moves to the nuxt environment with mockNuxtImport — the
existing `vi.mock('#imports', ...)` was not intercepting at all, which
went unnoticed because no test ever invoked `onRequest`.1 parent 9d717f5 commit fc7f07d
3 files changed
Lines changed: 154 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
455 | 475 | | |
456 | 476 | | |
457 | 477 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
9 | 14 | | |
10 | 15 | | |
11 | 16 | | |
| |||
42 | 47 | | |
43 | 48 | | |
44 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
16 | 39 | | |
17 | 40 | | |
18 | 41 | | |
| |||
37 | 60 | | |
38 | 61 | | |
39 | 62 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
44 | 66 | | |
45 | 67 | | |
46 | 68 | | |
| |||
0 commit comments