Skip to content

Commit

Permalink
fix(composables): extract host getter
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Oct 5, 2022
1 parent 9cf84fa commit 7c3b402
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions nuxt/composables/useHost.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getHost } from '~/plugins/util/util'

export const useHost = () => {
if (process.server) {
const event = useRequestEvent()

if (!event.req.headers.host) throw new Error('Host header is not given!')

return event.req.headers.host
return getHost(event)
} else {
return location.host
}
Expand Down
7 changes: 7 additions & 0 deletions nuxt/plugins/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CombinedError } from '@urql/core'
import Clipboard from 'clipboard'
import { CompatibilityEvent } from 'h3'
import { computed, Ref } from 'vue'

export function append(path: string, pathToAppend: string): string {
Expand Down Expand Up @@ -54,6 +55,12 @@ export function copyText(text: string) {
// return promise
// }

export function getHost(event: CompatibilityEvent) {
if (!event.req.headers.host) throw new Error('Host header is not given!')

return event.req.headers.host
}

export function getApiDataDefault() {
return {
api: computed(() => {
Expand Down
4 changes: 3 additions & 1 deletion nuxt/server/middleware/headers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { appendHeader, defineEventHandler } from 'h3'

import { getHost } from '~/plugins/util/util'

function getCsp(host: string): Record<string, Array<string>> {
const hostName = host.replace(/:[0-9]+$/, '')
const config = useRuntimeConfig()
Expand Down Expand Up @@ -62,7 +64,7 @@ function getCspAsString(host: string): string {
}

export default defineEventHandler((event) => {
const host = useHost()
const host = getHost(event)

appendHeader(event, 'Content-Security-Policy', getCspAsString(host))
// appendHeader(event, 'Cross-Origin-Embedder-Policy', 'require-corp') // https://stackoverflow.com/questions/71904052/getting-notsameoriginafterdefaultedtosameoriginbycoep-error-with-helmet
Expand Down

0 comments on commit 7c3b402

Please sign in to comment.