Skip to content

Commit

Permalink
fix: react-router import in gatsby spa to match the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryunsong-contentful committed Jan 16, 2025
1 parent b2a2c58 commit f6480bb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 132 deletions.
18 changes: 0 additions & 18 deletions examples/gatsby/gatsby-spa/gatsby-browser.tsx

This file was deleted.

82 changes: 1 addition & 81 deletions examples/gatsby/gatsby-spa/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions examples/gatsby/gatsby-spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"gatsby": "^5.14.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^9.0.3",
"react-router": "^7.1.1"
"react-markdown": "^9.0.3"
},
"devDependencies": {
"@types/node": "^20.11.19",
Expand Down
26 changes: 0 additions & 26 deletions examples/gatsby/gatsby-spa/src/pages/StudioExperiencePage.tsx

This file was deleted.

30 changes: 30 additions & 0 deletions examples/gatsby/gatsby-spa/src/pages/[locale]/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useFetchBySlug, ExperienceRoot } from '@contentful/experiences-sdk-react';
import { PageProps } from 'gatsby';
import React from 'react';
import { useContentfulClient } from '../../utils/useContentfulClient';

type PathParams = {
locale: string;
slug: string,
isPreviewString: string;
};

const experienceTypeId = process.env.GATSBY_CTFL_EXPERIENCE_TYPE || '';

export default function StudioExperiencePage(pageProps: PageProps) {
const { locale = 'en-US', slug = 'home-page', } = pageProps.params as PathParams;
const isPreview = new URL(pageProps.location.href).searchParams.get('isPreview') === 'true';
const { client } = useContentfulClient({ isPreview });

const { experience, error, isLoading } = useFetchBySlug({
slug,
localeCode: locale,
client,
experienceTypeId,
});

if (isLoading) return <div>Loading...</div>;
if (error) return <div>{error.message}</div>;

return <ExperienceRoot experience={experience} locale={locale} />;
}
9 changes: 4 additions & 5 deletions examples/gatsby/gatsby-spa/src/utils/useContentfulClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useMemo } from 'react';
import { createClient } from 'contentful';
import { useLocation } from 'react-router';
import '../studio-config'; // Update this file to set breakpoints, design tokens, and register components

export const useContentfulClient = () => {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const isPreview = queryParams.get('isPreview') === 'true';
type Props = {
isPreview?: boolean;
}

export const useContentfulClient = ({isPreview}: Props) => {
const client = useMemo(() => {
const clientConfig = {
space: process.env.GATSBY_CTFL_SPACE || '',
Expand Down

0 comments on commit f6480bb

Please sign in to comment.