-
Notifications
You must be signed in to change notification settings - Fork 55
Make header/footer server side rendered for better SEO #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| "use client"; | ||
|
|
||
| import { ShoppingBag } from "lucide-react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useCart } from "@/contexts/CartContext"; | ||
|
|
||
| export function CartButton() { | ||
| const { itemCount, openCart } = useCart(); | ||
|
|
||
| return ( | ||
| <Button | ||
| variant="ghost" | ||
| size="icon-lg" | ||
| onClick={openCart} | ||
| aria-label="Open cart" | ||
| className="relative" | ||
| > | ||
| <ShoppingBag className="size-5" /> | ||
| {itemCount > 0 && ( | ||
| <span className="absolute top-0 right-0 bg-primary text-white text-xs font-medium rounded-full h-5 w-5 flex items-center justify-center"> | ||
| {itemCount} | ||
| </span> | ||
| )} | ||
| </Button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,160 +1,57 @@ | ||
| "use client"; | ||
|
|
||
| import type { Category } from "@spree/sdk"; | ||
| import { Search, ShoppingBag, User, X } from "lucide-react"; | ||
| import { User } from "lucide-react"; | ||
| import Image from "next/image"; | ||
| import Link from "next/link"; | ||
| import { usePathname } from "next/navigation"; | ||
| import { useCallback, useRef, useState } from "react"; | ||
| import { CartButton } from "@/components/layout/CartButton"; | ||
| import { CountrySwitcher } from "@/components/layout/CountrySwitcher"; | ||
| import { MobileMenu } from "@/components/layout/MobileMenu"; | ||
| import { SearchBar } from "@/components/search/SearchBar"; | ||
| import { SearchToggle } from "@/components/layout/SearchToggle"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useCart } from "@/contexts/CartContext"; | ||
| import { useStore } from "@/contexts/StoreContext"; | ||
| import { extractBasePath } from "@/lib/utils/path"; | ||
|
|
||
| const storeName = process.env.NEXT_PUBLIC_STORE_NAME || "Spree Store"; | ||
|
|
||
| interface HeaderProps { | ||
| rootCategories: Category[]; | ||
| basePath: string; | ||
| } | ||
|
|
||
| export function Header({ rootCategories }: HeaderProps) { | ||
| const { itemCount, openCart } = useCart(); | ||
| const { storeName } = useStore(); | ||
| const pathname = usePathname(); | ||
| const basePath = extractBasePath(pathname); | ||
| const [searchOpen, setSearchOpen] = useState(false); | ||
| const searchTriggerRef = useRef<HTMLButtonElement>(null); | ||
|
|
||
| const closeSearch = useCallback(() => { | ||
| setSearchOpen(false); | ||
| searchTriggerRef.current?.focus(); | ||
| }, []); | ||
|
|
||
| export function Header({ rootCategories, basePath }: HeaderProps) { | ||
| return ( | ||
| <header className="sticky top-0 z-50 bg-white border-b border-gray-200 h-16 relative"> | ||
| {/* Normal header content */} | ||
| <div | ||
| className={`absolute inset-0 transition-all duration-300 ease-in-out ${ | ||
| searchOpen | ||
| ? "translate-y-4 opacity-0 pointer-events-none" | ||
| : "translate-y-0 opacity-100" | ||
| }`} | ||
| > | ||
| <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-full"> | ||
| <div className="flex items-center h-full w-full"> | ||
| {/* Left section: hamburger */} | ||
| <div className="flex items-center flex-1"> | ||
| <MobileMenu rootCategories={rootCategories} basePath={basePath} /> | ||
| </div> | ||
|
|
||
| {/* Center section: logo (always centered via equal flex-1 siblings) */} | ||
| <div className="flex justify-center min-w-0"> | ||
| <Link | ||
| href={basePath || "/"} | ||
| className="flex items-center min-w-0" | ||
| > | ||
| <Image | ||
| src="/spree.png" | ||
| alt={storeName} | ||
| width={90} | ||
| height={32} | ||
| className="h-8 w-auto max-w-full object-contain" | ||
| priority | ||
| /> | ||
| <SearchToggle | ||
| basePath={basePath} | ||
| left={<MobileMenu rootCategories={rootCategories} basePath={basePath} />} | ||
| center={ | ||
| <Link href={basePath || "/"} className="flex items-center min-w-0"> | ||
| <Image | ||
| src="/spree.png" | ||
| alt={storeName} | ||
| width={90} | ||
| height={32} | ||
| className="h-8 w-auto max-w-full object-contain" | ||
| priority | ||
| /> | ||
| </Link> | ||
| } | ||
| rightStart={ | ||
| <div className="hidden lg:block"> | ||
| <CountrySwitcher /> | ||
| </div> | ||
| } | ||
| rightEnd={ | ||
| <> | ||
| {/* Account - desktop only */} | ||
| <div className="hidden md:block"> | ||
| <Button variant="ghost" size="icon-lg" asChild> | ||
| <Link href={`${basePath}/account`} aria-label="Account"> | ||
| <User className="size-5" /> | ||
| </Link> | ||
| </div> | ||
|
|
||
| {/* Right section: actions */} | ||
| <div className="flex items-center flex-1 justify-end space-x-2"> | ||
| {/* Country/Currency Switcher - desktop only (hidden on mobile+tablet) */} | ||
| <div className="hidden lg:block"> | ||
| <CountrySwitcher /> | ||
| </div> | ||
|
|
||
| {/* Search toggle */} | ||
| <Button | ||
| ref={searchTriggerRef} | ||
| variant="ghost" | ||
| size="icon-lg" | ||
| onClick={() => setSearchOpen(true)} | ||
| aria-label="Open search" | ||
| aria-expanded={searchOpen} | ||
| aria-controls="search-overlay" | ||
| > | ||
| <Search className="size-5" /> | ||
| </Button> | ||
|
|
||
| {/* Account - desktop only */} | ||
| <div className="hidden md:block"> | ||
| <Button variant="ghost" size="icon-lg" asChild> | ||
| <Link href={`${basePath}/account`} aria-label="Account"> | ||
| <User className="size-5" /> | ||
| </Link> | ||
| </Button> | ||
| </div> | ||
|
|
||
| {/* Cart */} | ||
| <Button | ||
| variant="ghost" | ||
| size="icon-lg" | ||
| onClick={openCart} | ||
| aria-label="Open cart" | ||
| className="relative" | ||
| > | ||
| <ShoppingBag className="size-5" /> | ||
| {itemCount > 0 && ( | ||
| <span className="absolute top-0 right-0 bg-primary text-white text-xs font-medium rounded-full h-5 w-5 flex items-center justify-center"> | ||
| {itemCount} | ||
| </span> | ||
| )} | ||
| </Button> | ||
| </div> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Click-outside overlay — closes search when clicking anywhere outside */} | ||
| {searchOpen && ( | ||
| <div | ||
| className="fixed inset-0 z-40" | ||
| onClick={closeSearch} | ||
| role="presentation" | ||
| /> | ||
| )} | ||
|
|
||
| {/* Search bar - replaces header content */} | ||
| <div | ||
| id="search-overlay" | ||
| inert={!searchOpen} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Escape") closeSearch(); | ||
| }} | ||
| className={`absolute inset-0 z-50 transition-all duration-300 ease-in-out ${ | ||
| searchOpen | ||
| ? "translate-y-0 opacity-100" | ||
| : "-translate-y-4 opacity-0 pointer-events-none" | ||
| }`} | ||
| > | ||
| <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-full flex items-center gap-3"> | ||
| <div className="flex-1"> | ||
| <SearchBar | ||
| key={String(searchOpen)} | ||
| basePath={basePath} | ||
| autoFocus={searchOpen} | ||
| onNavigate={closeSearch} | ||
| /> | ||
| </div> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon-lg" | ||
| onClick={closeSearch} | ||
| aria-label="Close search" | ||
| > | ||
| <X className="size-5" /> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </header> | ||
| {/* Cart */} | ||
| <CartButton /> | ||
| </> | ||
| } | ||
| /> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.