Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
128 lines
4.6 KiB
TypeScript
128 lines
4.6 KiB
TypeScript
import { NextRequest } from "next/server"
|
|
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
import { env } from "@/env/server"
|
|
|
|
export const langInput = z.object({
|
|
lang: z.nativeEnum(Lang),
|
|
})
|
|
|
|
/**
|
|
* Helper function to convert Lang enum to API lang enum.
|
|
*/
|
|
export const toApiLang = (lang: Lang): string => {
|
|
const result = toApiLangMap[lang]
|
|
if (!result) {
|
|
throw new Error("Invalid language")
|
|
}
|
|
return result
|
|
}
|
|
|
|
const toApiLangMap: { [key in Lang]: string } = {
|
|
[Lang.en]: "En",
|
|
[Lang.sv]: "Sv",
|
|
[Lang.no]: "No",
|
|
[Lang.fi]: "Fi",
|
|
[Lang.da]: "Da",
|
|
[Lang.de]: "De",
|
|
}
|
|
|
|
/**
|
|
* Helper function to convert lang string to Lang enum.
|
|
*/
|
|
export function toLang(lang: string): Lang | undefined {
|
|
const lowerCaseLang = lang.toLowerCase()
|
|
return Object.values(Lang).find((l) => l === lowerCaseLang)
|
|
}
|
|
|
|
/**
|
|
* Use this function when you want to create URLs that are public facing, for
|
|
* example for redirects or redirectTo query parameters.
|
|
* Dedicated environments are behind Akamai (test, stage, production). They have
|
|
* env.PUBLIC_URL set.
|
|
* All other environment like deploy previews and branch deployments are not
|
|
* behind Akamai and therefore do not have env.PUBLIC_URL set.
|
|
* We need this approach because Netlify uses x-forwarded-host internally and
|
|
* strips it from ever reaching our code.
|
|
* TODO: Replace this approach with custom header in Akamai that mirrors the
|
|
* value in x-forwarded-host which would not get stripped by Netlify.
|
|
* @param request The incoming request.
|
|
* @returns NextURL The public facing URL instance for the given request.
|
|
*/
|
|
export function getPublicNextURL(request: NextRequest) {
|
|
if (env.PUBLIC_URL) {
|
|
const publicNextURL = request.nextUrl.clone()
|
|
// Akamai in front of Netlify for dedicated environments
|
|
// require us to rewrite the incoming host and hostname
|
|
// to match the public URL used to visit Akamai.
|
|
const url = new URL(env.PUBLIC_URL)
|
|
publicNextURL.host = url.host
|
|
publicNextURL.hostname = url.hostname
|
|
return publicNextURL
|
|
}
|
|
return request.nextUrl
|
|
}
|
|
|
|
/**
|
|
* Use this function when you want the public facing URL for the given request.
|
|
* Read about the motivation in getPublicNextURL above.
|
|
* @see getPublicNextURL
|
|
* @param request The incoming request.
|
|
* @returns string The public facing origin for the given request.
|
|
*/
|
|
export function getPublicURL(request: NextRequest) {
|
|
if (env.PUBLIC_URL) {
|
|
return env.PUBLIC_URL
|
|
}
|
|
return request.nextUrl.origin
|
|
}
|
|
|
|
/**
|
|
* Use this function when you want to create URLs that are internal (behind Akamai),
|
|
* for example for rewrites. Mainly used for middleware wrapped in auth().
|
|
* The auth() function overrides the origin of the incoming request. It sets it
|
|
* to the origin of AUTH_URL/NEXTAUTH_URL. This means we cannot use the augmented
|
|
* request from auth() for rewrites, as those will point to auth url origin
|
|
* (in front of Akamai) and not the origin of the incoming request (behind Akamai).
|
|
* This results in rewrites going over the internet instead of going through the
|
|
* internal routing at Netlify.
|
|
* For dedicated environments (test, stage and production) we are behind Akamai.
|
|
* For those we have set a value for AUTH_URL/NEXTAUTH_URL, they point to the
|
|
* PUBLIC_URL. For rewrites we need the internal origin inside Netlify.
|
|
* In middleware.ts we copy the incoming origin to a header 'x-sh-origin'. We try
|
|
* and use that first, if not present we assume the internal origin is the value
|
|
* of the host header, as that is what Netlify used for routing to this deployment.
|
|
* @param request The incoming request.
|
|
* @returns NextURL The internal request, in Netlify behind Akamai.
|
|
*/
|
|
export function getInternalNextURL(request: NextRequest) {
|
|
const { href, origin } = request.nextUrl
|
|
|
|
const originHeader = request.headers.get("x-sh-origin")
|
|
if (originHeader) {
|
|
console.log(`[internalNextUrl] using x-sh-origin header`, {
|
|
origin,
|
|
originHeader,
|
|
newOrigin: href.replace(origin, originHeader),
|
|
})
|
|
return new NextRequest(href.replace(origin, originHeader), request).nextUrl
|
|
}
|
|
|
|
const hostHeader = request.headers.get("host")
|
|
if (hostHeader) {
|
|
const inputHostOrigin = `${request.nextUrl.protocol}//${hostHeader}`
|
|
console.log(`[internalNextUrl] using host header`, {
|
|
origin,
|
|
hostHeader,
|
|
hostOrigin: inputHostOrigin,
|
|
newOrigin: href.replace(origin, inputHostOrigin),
|
|
})
|
|
const { origin: hostOrigin } = new URL(inputHostOrigin)
|
|
return new NextRequest(href.replace(origin, hostOrigin), request).nextUrl
|
|
}
|
|
|
|
console.log(`[internalNextUrl] falling back to incoming request`)
|
|
return request.nextUrl
|
|
}
|