Files
web/apps/scandic-web/utils/metadata/title.ts
Erik Tiekstra ea4ef9a048 fix: Added translated city name to meta title
Approved-by: Linus Flood
Approved-by: Matilda Landström
2025-07-01 09:48:18 +00:00

213 lines
6.3 KiB
TypeScript

import { getIntl } from "@/i18n"
import type { RawMetadataSchema } from "@scandic-hotels/trpc/routers/contentstack/metadata/output"
function getTitleSuffix(contentType: string) {
switch (contentType) {
case "content_page":
case "collection_page":
case "destination_overview_page":
case "destination_city_page":
case "destination_country_page":
return " | Scandic Hotels"
default:
return ""
}
}
export async function getTitle(data: RawMetadataSchema) {
const intl = await getIntl()
const suffix = getTitleSuffix(data.system.content_type_uid)
const metadata = data.web?.seo_metadata
if (metadata?.title) {
return `${metadata.title}${suffix}`
}
if (data.system.content_type_uid === "hotel_page" && data.hotelData) {
if (data.subpageUrl) {
const restaurantSubPage = data.hotelRestaurants?.find(
(restaurant) => restaurant.nameInUrl === data.subpageUrl
)
if (restaurantSubPage) {
const restaurantTitleLong = intl.formatMessage(
{
defaultMessage:
"Explore {restaurantName} at {hotelName} in {destination}",
},
{
restaurantName: restaurantSubPage.name,
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
const restaurantTitleShort = intl.formatMessage(
{
defaultMessage: "Explore {restaurantName} at {hotelName}",
},
{
restaurantName: restaurantSubPage.name,
hotelName: data.hotelData.name,
}
)
if (restaurantTitleLong.length > 60) {
return restaurantTitleShort
}
return restaurantTitleLong
}
switch (data.subpageUrl) {
case data.additionalHotelData?.hotelParking.nameInUrl:
const parkingTitleLong = intl.formatMessage(
{
defaultMessage:
"Parking information for {hotelName} in {destination}",
},
{
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
const parkingTitleShort = intl.formatMessage(
{ defaultMessage: "Parking information for {hotelName}" },
{ hotelName: data.hotelData.name }
)
if (parkingTitleLong.length > 60) {
return parkingTitleShort
}
return parkingTitleLong
case data.additionalHotelData?.healthAndFitness.nameInUrl:
const wellnessTitleLong = intl.formatMessage(
{
defaultMessage:
"Gym & health facilities at {hotelName} in {destination}",
},
{
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
const wellnessTitleShort = intl.formatMessage(
{
defaultMessage: "Gym & health facilities at {hotelName}",
},
{
hotelName: data.hotelData.name,
}
)
if (wellnessTitleLong.length > 60) {
return wellnessTitleShort
}
return wellnessTitleLong
case data.additionalHotelData?.hotelSpecialNeeds.nameInUrl:
const accessibilityTitleLong = intl.formatMessage(
{
defaultMessage:
"Accessibility information for {hotelName} in {destination}",
},
{
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
const accessibilityTitleShort = intl.formatMessage(
{
defaultMessage: "Accessibility information for {hotelName}",
},
{
hotelName: data.hotelData.name,
}
)
if (accessibilityTitleLong.length > 60) {
return accessibilityTitleShort
}
return accessibilityTitleLong
case data.additionalHotelData?.meetingRooms.nameInUrl:
const meetingsTitleLong = intl.formatMessage(
{
defaultMessage:
"Meetings & conferences at {hotelName} in {destination}",
},
{
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
const meetingsTitleShort = intl.formatMessage(
{
defaultMessage: "Meetings & conferences at {hotelName}",
},
{
hotelName: data.hotelData.name,
}
)
if (meetingsTitleLong.length > 60) {
return meetingsTitleShort
}
return meetingsTitleLong
default:
break
}
}
return intl.formatMessage(
{
defaultMessage: "Stay at {hotelName} | Hotel in {destination}",
},
{
hotelName: data.hotelData.name,
destination: data.hotelData.translatedCityName,
}
)
}
if (
data.system.content_type_uid === "destination_city_page" ||
data.system.content_type_uid === "destination_country_page"
) {
if (data.destinationData) {
const { location, filter, filterType } = data.destinationData
if (location) {
if (filter) {
if (filterType === "facility") {
return intl.formatMessage(
{
defaultMessage: "Hotels with {filter} in {location}",
},
{ location, filter }
)
} else if (filterType === "surroundings") {
return intl.formatMessage(
{
defaultMessage: "Hotels near {filter} in {location}",
},
{ location, filter }
)
}
}
const destinationTitle = intl.formatMessage(
{
defaultMessage: "Hotels in {location}",
},
{ location }
)
return `${destinationTitle}${suffix}`
}
}
}
if (data.web?.breadcrumbs?.title) {
return `${data.web.breadcrumbs.title}${suffix}`
}
if (data.heading) {
return `${data.heading}${suffix}`
}
if (data.header?.heading) {
return `${data.header.heading}${suffix}`
}
return ""
}