Skip to content

Commit 76b4560

Browse files
authored
feat: LDP-2335: pass through response headers on the server (#190)
* feat: LDP-2335: Allow passing through response headers to client * LDP-2335: Replace the plugin with a SSR composable * LDP-2335: Headers should not be sent to client * LDP-2335: Page is now an object * LDP-2335: Remove config references * LDP-2335: Call passThroughHeaders from fetchPage * LDP-2335: Remove typos * LDP-2335: Improve docs and add SSR context check
1 parent 90a2ffb commit 76b4560

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ is added automatically to requests. Defaults to `false`.
9595

9696
- `exposeAPIRouteRules`: If enabled, the module will create a Nitro server handler that proxies API requests to Drupal backend. Defaults to `true` for SSR (it's disabled for SSG).
9797

98+
- `passThroughHeaders`: Response headers to pass through from Drupal to the client. Defaults to ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache']. Note: This is only available in SSR mode.
99+
98100
## Overriding options with environment variables
99101

100102
Runtime config values can be overridden with environment variables via `NUXT_PUBLIC_` prefix. Supported runtime overrides:

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ModuleOptions {
1717
fetchProxyHeaders: string[],
1818
useLocalizedMenuEndpoint: boolean,
1919
exposeAPIRouteRules: boolean,
20+
passThroughHeaders?: string[],
2021
}
2122

2223
export default defineNuxtModule<ModuleOptions>({
@@ -38,7 +39,8 @@ export default defineNuxtModule<ModuleOptions>({
3839
fetchProxyHeaders: ['cookie'],
3940
useLocalizedMenuEndpoint: true,
4041
addRequestFormat: false,
41-
exposeAPIRouteRules: true
42+
exposeAPIRouteRules: true,
43+
passThroughHeaders: ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache'],
4244
},
4345
setup (options, nuxt) {
4446
// Keep backwards compatibility for baseURL(deprecated).

src/runtime/composables/useDrupalCe.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { callWithNuxt } from '#app'
22
import { defu } from 'defu'
3+
import { appendResponseHeader } from 'h3'
34
import { useRuntimeConfig, useRequestURL, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports'
45

56
export const useDrupalCe = () => {
@@ -58,6 +59,13 @@ export const useDrupalCe = () => {
5859
useFetchOptions = processFetchOptions(useFetchOptions)
5960
useFetchOptions.query = useFetchOptions.query ?? {}
6061

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+
6169
if (config.addRequestContentFormat) {
6270
useFetchOptions.query._content_format = config.addRequestContentFormat
6371
}
@@ -148,12 +156,32 @@ export const useDrupalCe = () => {
148156
: h(resolveComponent(customElements.element), customElements)
149157
}
150158

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+
151178
return {
152179
fetchPage,
153180
fetchMenu,
154181
getMessages,
155182
getPage,
156183
renderCustomElements,
184+
passThroughHeaders
157185
}
158186
}
159187

0 commit comments

Comments
 (0)