import { notFound } from "next/navigation" import { loadImageMapping } from "@/lib/wordpress-content" import { generateSEOMetadata, generateStructuredData } from "@/lib/seo" import { getPageBySlug, getAllPageSlugs } from "@/lib/wordpress-data-loader" import { cleanWordPressContent } from "@/lib/clean-wordPress-content" import { getLocationBySlug, getAllLocationSlugs } from "@/lib/location-data" import type { Metadata } from "next" import { FAQSchema } from "@/components/faq-schema" import { FAQSection } from "@/components/faq-section" import { ContactPage } from "@/components/contact-page" import { AboutPage } from "@/components/about-page" import { WhoWeServePage } from "@/components/who-we-serve-page" import { generateLocationPageMetadata, LocationLandingPage, } from "@/components/location-landing-page" // Required for static export - ensures this route is statically generated export const dynamic = "force-static" export const dynamicParams = false interface PageProps { params: Promise<{ slug: string[] }> } // Route mapping: navigation URLs -> WordPress page slugs const routeMapping: Record = { // Services "services/repairs": "vending-machine-repairs", "services/moving": "vending-machine-repairs", // Placeholder - no moving page exists "services/parts": "parts-and-support", services: "vending-machine-repairs", // Default to repairs page // Vending Machines "vending-machines": "vending-machines", // Main vending machines page "vending-machines/machines-we-use": "vending-machines", // Use main page "vending-machines/machines-for-sale": "vending-machines-for-sale-in-utah", // Who We Serve warehouses: "streamlining-snack-and-beverage-access-in-warehouse-environments", "auto-repair": "enhancing-auto-repair-facilities-with-convenient-vending-solutions", gyms: "vending-machine-for-your-gym", "community-centers": "vending-for-your-community-centers", "dance-studios": "vending-machine-for-your-dance-studio", "car-washes": "vending-machines-for-your-car-wash", // Food & Beverage "food-and-beverage/healthy-options": "healthy-vending", "food-and-beverage/traditional-options": "traditional-vending", "food-and-beverage/suppliers": "diverse-vending-options-with-rocky-mountain-vendings-exclusive-wholesale-accounts", // About "about-us": "about-us", "about/faqs": "faqs", "contact-us": "contact-us", } // Helper function to resolve route to WordPress slug function resolveRouteToSlug(slugArray: string[]): string | null { const route = slugArray.join("/") // Check if this is a location page - if so, return null to let Next.js handle it // (location pages are handled by vending-machines-[location] route) if (isLocationRoute(slugArray)) { return null // Let the location route handle it } // Check direct mapping first if (routeMapping[route]) { return routeMapping[route] } // Check if it's a direct WordPress slug const directSlug = slugArray.join("-") if (getPageBySlug(directSlug)) { return directSlug } // Check last segment as fallback (for nested routes) if (slugArray.length > 1) { const lastSegment = slugArray[slugArray.length - 1] if (getPageBySlug(lastSegment)) { return lastSegment } } // Try the full route as-is if (getPageBySlug(route)) { return route } return null } // Helper function to check if a route is a location page function isLocationRoute(slugArray: string[]): boolean { // Location pages follow pattern: vending-machines-{location} // e.g., ["vending-machines-salt-lake-city-utah"] or ["vending-machines", "salt-lake-city-utah"] if (slugArray.length === 1) { const slug = slugArray[0] // Check if it starts with "vending-machines-" and the rest is a valid location slug if (slug.startsWith("vending-machines-")) { const locationSlug = slug.replace("vending-machines-", "") return !!getLocationBySlug(locationSlug) } } else if (slugArray.length === 2 && slugArray[0] === "vending-machines") { return !!getLocationBySlug(slugArray[1]) } return false } // Generate static params for all pages export async function generateStaticParams() { try { const slugs = getAllPageSlugs() const params: Array<{ slug: string[] }> = [] // Add all WordPress page slugs slugs.forEach((slug: string) => { params.push({ slug: [slug], // Catch-all routes need arrays }) }) // Add mapped routes (like /services, /services/repairs, etc.) Object.keys(routeMapping).forEach((route) => { const routeArray = route.split("/") // Only add if it's not already added as a WordPress slug if (!slugs.includes(route)) { params.push({ slug: routeArray, }) } }) // Add location routes (e.g., /vending-machines-salt-lake-city-utah) const locationSlugs = getAllLocationSlugs() locationSlugs.forEach((locationSlug: string) => { if (locationSlug) { params.push({ slug: [`vending-machines-${locationSlug}`], }) } }) return params } catch (error) { // Silently return empty array in production if (process.env.NODE_ENV === "development") { console.error("Error generating static params:", error) } // Return at least one valid param to prevent build failure return [{ slug: ["vending-machines-ogden-utah"] }] } } // Generate metadata for a page export async function generateMetadata({ params, }: PageProps): Promise { try { const { slug } = await params const slugArray = Array.isArray(slug) ? slug : [slug] // Handle location routes if (isLocationRoute(slugArray)) { let locationSlug: string if (slugArray.length === 1) { locationSlug = slugArray[0].replace("vending-machines-", "") } else { locationSlug = slugArray[1] } const locationData = getLocationBySlug(locationSlug) if (!locationData) { return { title: "Location Not Found | Rocky Mountain Vending", } } return generateLocationPageMetadata(locationData) } const pageSlug = resolveRouteToSlug(slugArray) if (!pageSlug) { return { title: "Page Not Found | Rocky Mountain Vending", } } const page = getPageBySlug(pageSlug) if (!page) { return { title: "Page Not Found | Rocky Mountain Vending", } } return generateSEOMetadata({ title: page.title || "Page", description: page.seoDescription || page.excerpt || "", excerpt: page.excerpt, date: page.date, modified: page.modified, image: page.images?.[0]?.localPath, path: `/${slugArray.join("/")}`, }) } catch (error) { // Silently return fallback metadata in production if (process.env.NODE_ENV === "development") { console.error("Error generating metadata:", error) } return { title: "Rocky Mountain Vending", description: "Rocky Mountain Vending provides quality vending machine services in Utah.", } } } export default async function WordPressPage({ params }: PageProps) { try { const { slug } = await params const slugArray = Array.isArray(slug) ? slug : [slug] // If this is a location route, render the location page if (isLocationRoute(slugArray)) { let locationSlug: string if (slugArray.length === 1) { locationSlug = slugArray[0].replace("vending-machines-", "") } else { locationSlug = slugArray[1] } const locationData = getLocationBySlug(locationSlug) if (!locationData) { notFound() } return } const pageSlug = resolveRouteToSlug(slugArray) if (!pageSlug) { notFound() } const page = getPageBySlug(pageSlug) if (!page) { notFound() } // Load image mapping (optional, won't break if it fails) let imageMapping: any = {} try { imageMapping = loadImageMapping() } catch (e) { // Silently fail - image mapping is optional } // Clean and render WordPress content as styled React components const content = page.content ? (
{cleanWordPressContent(String(page.content), { imageMapping, pageTitle: page.title, // Pass page title to avoid duplicate headings })}
) : (

No content available.

) // Generate structured data let structuredData try { structuredData = generateStructuredData({ title: page.title || "Page", description: page.seoDescription || page.excerpt || "", url: page.link || page.urlPath || `https://rockymountainvending.com/${pageSlug}/`, datePublished: page.date, dateModified: page.modified || page.date, type: "WebPage", }) } catch (e) { // Silently use fallback structured data in production if (process.env.NODE_ENV === "development") { console.error("Error generating structured data:", e) } structuredData = { "@context": "https://schema.org", "@type": "WebPage", headline: page.title || "Page", description: page.seoDescription || "", url: `https://rockymountainvending.com/${pageSlug}/`, } } // Extract FAQs from content if this is the FAQ page const faqs: Array<{ question: string; answer: string }> = [] if (pageSlug === "faqs" && page.content) { const contentStr = String(page.content) // Extract FAQ items from accordion structure const questionMatches = contentStr.matchAll( /([^<]+)<\/span>/g ) // Extract full answer content - match everything inside the card-body div until the closing div const answerMatches = contentStr.matchAll( /
([\s\S]*?)<\/div>\s*<\/div>\s*/g ) const questions = Array.from(questionMatches).map((m) => m[1].trim()) const answers = Array.from(answerMatches).map((m) => { // Keep HTML but clean up whitespace let answer = m[1].trim() // Remove the opening

and closing

if they wrap everything, but keep other HTML // Clean up excessive whitespace but preserve HTML structure answer = answer .replace(/\n\s*\n/g, "\n") .replace(/>\s+<") .trim() return answer }) // Match questions with answers questions.forEach((question, index) => { if (answers[index]) { faqs.push({ question, answer: answers[index] }) } }) } // Check if this is a "Who We Serve" page const whoWeServeSlugs = [ "streamlining-snack-and-beverage-access-in-warehouse-environments", "enhancing-auto-repair-facilities-with-convenient-vending-solutions", "vending-machine-for-your-gym", "vending-for-your-community-centers", "vending-machine-for-your-dance-studio", "vending-machines-for-your-car-wash", ] const isWhoWeServePage = whoWeServeSlugs.includes(pageSlug) return ( <>