2024-04-15 19:39:03 +02:00
|
|
|
import { customDomainUsed, getDomain, getHost } from "~/lib/utils.js"
|
2023-12-16 19:21:03 +01:00
|
|
|
|
2024-01-12 15:43:28 +01:00
|
|
|
/**
|
|
|
|
|
* Added by Caddy when proxying to the app
|
|
|
|
|
* @type {string}
|
|
|
|
|
*/
|
2024-04-15 19:39:03 +02:00
|
|
|
const customDomainHeaderName = "user-custom-domain"
|
2024-01-12 15:43:28 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List of routes that can be used with a custom domain
|
|
|
|
|
* @type {string[]}
|
|
|
|
|
*/
|
2024-04-15 19:39:03 +02:00
|
|
|
const customDomainAllowedRoutes = ["forms-slug"]
|
2024-01-12 15:43:28 +01:00
|
|
|
|
2024-01-16 16:51:38 +01:00
|
|
|
function redirectToMainDomain(details = {}) {
|
2024-04-15 19:39:03 +02:00
|
|
|
console.warn("Redirecting to main domain", { reason: "unknown", ...details })
|
|
|
|
|
return navigateTo(
|
|
|
|
|
useRuntimeConfig().public.appUrl +
|
|
|
|
|
"?utm_source=failed_custom_domain_redirect",
|
|
|
|
|
{ redirectCode: 301, external: true },
|
|
|
|
|
)
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export default defineNuxtRouteMiddleware((to) => {
|
2024-01-16 11:33:38 +01:00
|
|
|
if (!customDomainUsed()) return
|
2024-01-12 15:43:28 +01:00
|
|
|
|
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
|
|
|
|
const customDomainHeaderValue = useRequestHeaders()[customDomainHeaderName]
|
2024-04-15 19:39:03 +02:00
|
|
|
if (
|
|
|
|
|
import.meta.server &&
|
|
|
|
|
(!customDomainHeaderValue ||
|
|
|
|
|
customDomainHeaderValue !== getDomain(getHost()))
|
|
|
|
|
) {
|
|
|
|
|
return redirectToMainDomain({
|
|
|
|
|
reason: "header_mismatch",
|
2024-01-16 17:40:28 +01:00
|
|
|
customDomainHeaderValue: customDomainHeaderValue,
|
2024-01-16 16:51:38 +01:00
|
|
|
host: getDomain(getHost()),
|
|
|
|
|
})
|
2024-01-12 15:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!config.public.customDomainsEnabled) {
|
|
|
|
|
// If custom domain not allowed, redirect
|
2024-01-16 17:40:28 +01:00
|
|
|
return redirectToMainDomain({
|
2024-04-15 19:39:03 +02:00
|
|
|
reason: "custom_domains_disabled",
|
2024-01-16 17:40:28 +01:00
|
|
|
})
|
2024-01-12 15:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!customDomainAllowedRoutes.includes(to.name)) {
|
|
|
|
|
// Custom domain only allowed for form url
|
2024-01-16 17:40:28 +01:00
|
|
|
return redirectToMainDomain({
|
2024-04-15 19:39:03 +02:00
|
|
|
reason: "route_not_allowed",
|
2024-01-16 16:51:38 +01:00
|
|
|
})
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
|
|
|
|
})
|