Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LDP-2335: pass through response headers on the server #190

Merged
merged 8 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ is added automatically to requests. Defaults to `false`.

- `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).

- `passThroughHeaders`: Response headers to pass through from Drupal to the client. Defaults to `cache-control`.
fago marked this conversation as resolved.
Show resolved Hide resolved

## Overriding options with environment variables

Runtime config values can be overridden with environment variables via `NUXT_PUBLIC_` prefix. Supported runtime overrides:
Expand Down
7 changes: 6 additions & 1 deletion playground/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</template>

<script lang="ts" setup>
const { fetchPage, renderCustomElements } = useDrupalCe()
const { fetchPage, renderCustomElements, passThroughHeaders } = useDrupalCe()
const page = await fetchPage(useRoute().path, { query: useRoute().query })
// Set to false to support custom layouts, using <NuxtLayout> instead.
definePageMeta({
Expand All @@ -31,4 +31,9 @@ useHead({
},
],
})

if (import.meta.server) {
const event = useRequestEvent()
passThroughHeaders(event, page.value.headers, ['cache-control'])
}
</script>
4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ModuleOptions {
fetchProxyHeaders: string[],
useLocalizedMenuEndpoint: boolean,
exposeAPIRouteRules: boolean,
passThroughHeaders?: string[],
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -38,7 +39,8 @@ export default defineNuxtModule<ModuleOptions>({
fetchProxyHeaders: ['cookie'],
useLocalizedMenuEndpoint: true,
addRequestFormat: false,
exposeAPIRouteRules: true
exposeAPIRouteRules: true,
passThroughHeaders: ['cache-control']
},
setup (options, nuxt) {
// Keep backwards compatibility for baseURL(deprecated).
Expand Down
35 changes: 34 additions & 1 deletion src/runtime/composables/useDrupalCe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { callWithNuxt } from '#app'
import { defu } from 'defu'
import { appendResponseHeader, H3Event } from 'h3'
import { useRuntimeConfig, useRequestURL, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports'

export const useDrupalCe = () => {
Expand Down Expand Up @@ -54,10 +55,20 @@ export const useDrupalCe = () => {
page_layout: 'default',
title: ''
}))

const headers = ref({})

useFetchOptions.key = `page-${path}`
useFetchOptions = processFetchOptions(useFetchOptions)
useFetchOptions.query = useFetchOptions.query ?? {}

if (import.meta.server) {
useFetchOptions.onResponse = (context) => {
const headersObject = Object.fromEntries([...context.response.headers.entries()])
headers.value = headersObject
}
}

if (config.addRequestContentFormat) {
useFetchOptions.query._content_format = config.addRequestContentFormat
}
Expand All @@ -83,7 +94,10 @@ export const useDrupalCe = () => {

page.value?.messages && pushMessagesToState(page.value.messages)

pageState.value = page
pageState.value = page.value
if (import.meta.server) {
page.value.headers = headers.value
fago marked this conversation as resolved.
Show resolved Hide resolved
}
return page
}

Expand Down Expand Up @@ -148,12 +162,31 @@ export const useDrupalCe = () => {
: h(resolveComponent(customElements.element), customElements)
}

/**
* Pass through headers from Drupal to the client
* @param event H3Event
* @param pageHeaders The headers from the Drupal response
* @param overridePassThroughHeaders Override/unset the current passThroughHeaders
fago marked this conversation as resolved.
Show resolved Hide resolved
*/
const passThroughHeaders = (event: H3Event, pageHeaders: Object, overridePassThroughHeaders?: Array<String>) => {
const { passThroughHeaders } = useRuntimeConfig().public.drupalCe
const passThroughHeadersArray = overridePassThroughHeaders || passThroughHeaders
if (pageHeaders) {
Object.keys(pageHeaders).forEach((key) => {
if (passThroughHeadersArray.includes(key)) {
appendResponseHeader(event, key, pageHeaders[key])
}
})
}
}

return {
fetchPage,
fetchMenu,
getMessages,
getPage,
renderCustomElements,
passThroughHeaders
fago marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down