|
| 1 | +import {type LinksFunction, type LoaderArgs} from '@shopify/remix-oxygen'; |
| 2 | +import { |
| 3 | + Links, |
| 4 | + Meta, |
| 5 | + Outlet, |
| 6 | + Scripts, |
| 7 | + LiveReload, |
| 8 | + ScrollRestoration, |
| 9 | + useLoaderData, |
| 10 | + type ShouldRevalidateFunction, |
| 11 | + useRouteError, |
| 12 | +} from '@remix-run/react'; |
| 13 | +import type {Shop} from '@shopify/hydrogen/storefront-api-types'; |
| 14 | +import appStyles from './styles/app.css'; |
| 15 | +import favicon from '../public/favicon.svg'; |
| 16 | +import {useNonce} from '@shopify/hydrogen'; |
| 17 | +import * as Sentry from '@sentry/remix'; |
| 18 | + |
| 19 | +// This is important to avoid re-fetching root queries on sub-navigations |
| 20 | +export const shouldRevalidate: ShouldRevalidateFunction = ({ |
| 21 | + formMethod, |
| 22 | + currentUrl, |
| 23 | + nextUrl, |
| 24 | +}) => { |
| 25 | + // revalidate when a mutation is performed e.g add to cart, login... |
| 26 | + if (formMethod && formMethod !== 'GET') { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + // revalidate when manually revalidating via useRevalidator |
| 31 | + if (currentUrl.toString() === nextUrl.toString()) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + return false; |
| 36 | +}; |
| 37 | + |
| 38 | +export const links: LinksFunction = () => { |
| 39 | + return [ |
| 40 | + {rel: 'stylesheet', href: appStyles}, |
| 41 | + { |
| 42 | + rel: 'preconnect', |
| 43 | + href: 'https://cdn.shopify.com', |
| 44 | + }, |
| 45 | + { |
| 46 | + rel: 'preconnect', |
| 47 | + href: 'https://shop.app', |
| 48 | + }, |
| 49 | + {rel: 'icon', type: 'image/svg+xml', href: favicon}, |
| 50 | + ]; |
| 51 | +}; |
| 52 | + |
| 53 | +export async function loader({context}: LoaderArgs) { |
| 54 | + const layout = await context.storefront.query<{shop: Shop}>(LAYOUT_QUERY); |
| 55 | + return { |
| 56 | + layout, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export function ErrorBoundary() { |
| 61 | + const error = useRouteError(); |
| 62 | + const eventId = Sentry.captureRemixErrorBoundaryError(error); |
| 63 | + |
| 64 | + return ( |
| 65 | + <div> |
| 66 | + <span>ErrorBoundary Error</span> |
| 67 | + <span id="event-id">{eventId}</span> |
| 68 | + </div> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +function App() { |
| 73 | + const nonce = useNonce(); |
| 74 | + const data = useLoaderData<typeof loader>(); |
| 75 | + |
| 76 | + const {name} = data.layout.shop; |
| 77 | + |
| 78 | + return ( |
| 79 | + <html lang="en"> |
| 80 | + <head> |
| 81 | + <meta charSet="utf-8" /> |
| 82 | + <meta name="viewport" content="width=device-width,initial-scale=1" /> |
| 83 | + <Meta /> |
| 84 | + <Links /> |
| 85 | + </head> |
| 86 | + <body> |
| 87 | + <h1>Hello, {name}</h1> |
| 88 | + <p>This is a custom storefront powered by Hydrogen</p> |
| 89 | + <Outlet /> |
| 90 | + <ScrollRestoration nonce={nonce} /> |
| 91 | + <Scripts nonce={nonce} /> |
| 92 | + <LiveReload nonce={nonce} /> |
| 93 | + </body> |
| 94 | + </html> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export default Sentry.withSentry(App); |
| 99 | + |
| 100 | +const LAYOUT_QUERY = `#graphql |
| 101 | + query layout { |
| 102 | + shop { |
| 103 | + name |
| 104 | + description |
| 105 | + } |
| 106 | + } |
| 107 | +`; |
0 commit comments