|
1 | 1 | import { callWithNuxt } from '#app' |
2 | 2 | import { defu } from 'defu' |
| 3 | +import { appendResponseHeader } from 'h3' |
3 | 4 | import { useRuntimeConfig, useRequestURL, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports' |
4 | 5 |
|
5 | 6 | export const useDrupalCe = () => { |
@@ -58,6 +59,13 @@ export const useDrupalCe = () => { |
58 | 59 | useFetchOptions = processFetchOptions(useFetchOptions) |
59 | 60 | useFetchOptions.query = useFetchOptions.query ?? {} |
60 | 61 |
|
| 62 | + useFetchOptions.onResponse = (context) => { |
| 63 | + if (config.passThroughHeaders && import.meta.server) { |
| 64 | + const headersObject = Object.fromEntries([...context.response.headers.entries()]) |
| 65 | + passThroughHeaders(nuxtApp, headersObject) |
| 66 | + } |
| 67 | + } |
| 68 | + |
61 | 69 | if (config.addRequestContentFormat) { |
62 | 70 | useFetchOptions.query._content_format = config.addRequestContentFormat |
63 | 71 | } |
@@ -148,12 +156,32 @@ export const useDrupalCe = () => { |
148 | 156 | : h(resolveComponent(customElements.element), customElements) |
149 | 157 | } |
150 | 158 |
|
| 159 | + /** |
| 160 | + * Pass through headers from Drupal to the client |
| 161 | + * @param pageHeaders The headers from the Drupal response |
| 162 | + */ |
| 163 | + const passThroughHeaders = (nuxtApp, pageHeaders) => { |
| 164 | + // Only run when SSR context is available. |
| 165 | + if (!nuxtApp.ssrContext) { |
| 166 | + return |
| 167 | + } |
| 168 | + const event = nuxtApp.ssrContext.event |
| 169 | + if (pageHeaders) { |
| 170 | + Object.keys(pageHeaders).forEach((key) => { |
| 171 | + if (config.passThroughHeaders.includes(key)) { |
| 172 | + appendResponseHeader(event, key, pageHeaders[key]) |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | + } |
| 177 | + |
151 | 178 | return { |
152 | 179 | fetchPage, |
153 | 180 | fetchMenu, |
154 | 181 | getMessages, |
155 | 182 | getPage, |
156 | 183 | renderCustomElements, |
| 184 | + passThroughHeaders |
157 | 185 | } |
158 | 186 | } |
159 | 187 |
|
|
0 commit comments