-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b23aef
commit 44316c7
Showing
5 changed files
with
99 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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,9 +1,68 @@ | ||
exports.onCreateWebpackConfig = ({ actions }) => { | ||
actions.setWebpackConfig({ | ||
resolve: { | ||
fallback: { | ||
path: require.resolve("path-browserify"), | ||
}, | ||
}, | ||
}); | ||
}; | ||
const path = require('path'); | ||
|
||
exports.createPages = async ({ graphql, actions, reporter }) => { | ||
const { createPage } = actions; | ||
|
||
// Dynamically import the ES Module | ||
const { fetchById, detachExperienceStyles } = await import('@contentful/experiences-sdk-react'); | ||
|
||
const response = await graphql(` | ||
query { | ||
allContentfulExperience { | ||
nodes { | ||
contentful_id | ||
slug | ||
title | ||
node_locale | ||
sys { | ||
contentType { | ||
sys { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
if (response.errors) { | ||
reporter.panicOnBuild('Error while querying Contentful data', response.errors); | ||
return; | ||
} | ||
|
||
const { useContentfulClient } = require('./src/utils/useContentfulClient.ts'); | ||
const client = useContentfulClient(); | ||
const { nodes } = response.data.allContentfulExperience; | ||
|
||
for (const node of nodes) { | ||
const { slug, title, node_locale: localeCode, contentful_id, sys } = node; | ||
|
||
try { | ||
const experienceEntry = await fetchById({ | ||
client, | ||
experienceTypeId: sys.contentType.sys.id, | ||
localeCode, | ||
id: contentful_id, | ||
}); | ||
|
||
let experienceStyles = ''; | ||
if (experienceEntry) { | ||
experienceStyles = detachExperienceStyles(experienceEntry) ?? ''; | ||
} | ||
|
||
createPage({ | ||
path: `/experience/${slug}`, | ||
component: path.resolve('src/templates/experiencePage.js'), | ||
context: { | ||
title, | ||
experienceEntryJSON: JSON.stringify(experienceEntry), | ||
locale: localeCode, | ||
experienceStyles, | ||
}, | ||
}); | ||
} catch (err) { | ||
reporter.warn(`Error creating page for slug "${slug}": ${err.message}`); | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
74 changes: 19 additions & 55 deletions
74
examples/gatsby/gatsby-ssg/src/pages/ExperienceSlugExample.tsx
This file contains 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,61 +1,25 @@ | ||
import path from 'path'; | ||
import { fetchById, detachExperienceStyles } from '@contentful/experiences-sdk-react'; | ||
import React from 'react' | ||
import { ExperienceRoot, useFetchBySlug } from '@contentful/experiences-sdk-react'; | ||
import { useContentfulClient } from '../utils/useContentfulClient'; | ||
|
||
exports.createPages = async ({ graphql, actions, reporter }: any) => { | ||
const { client } = useContentfulClient(); | ||
const { createPage } = actions; | ||
|
||
// Assume that your experience type id is "experience" | ||
const response = await graphql(` | ||
allContentfulExperience { | ||
nodes { | ||
contentful_id | ||
slug | ||
title | ||
node_locale | ||
sys { | ||
contentType { | ||
sys { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
if (response.errors) { | ||
// handle errors | ||
} | ||
const slug = 'ExperienceSlugExample' | ||
const localeCode = 'en-US'; | ||
const experienceTypeId = process.env.GATSBY_CTFL_EXPERIENCE_TYPE || ''; | ||
|
||
const { nodes } = response.data.allContentfulExperience; | ||
|
||
for (const node of nodes) { | ||
const { slug, title, node_locale: localeCode, contentful_id, sys } = node; | ||
|
||
const experienceEntry = await fetchById({ | ||
client, | ||
experienceTypeId: sys.contentType.sys.id, | ||
localeCode, | ||
id: contentful_id | ||
}); | ||
export default function ExperienceSlugExample() { | ||
const { client } = useContentfulClient(); | ||
|
||
let experienceStyles = ''; | ||
const { experience, error, isLoading } = useFetchBySlug({ | ||
slug, | ||
localeCode, | ||
client, | ||
experienceTypeId, | ||
}); | ||
|
||
if (experienceEntry) { | ||
experienceStyles = detachExperienceStyles(experienceEntry) ?? ''; | ||
} | ||
if (isLoading) return <div>Loading...</div>; | ||
if (error) return <div>{error.message}</div>; | ||
|
||
createPage({ | ||
path: `/experience/${slug}`, | ||
component: path.resolve('src/templates/experiencePage.js'), | ||
context: { | ||
title, | ||
experienceEntryJSON: JSON.stringify(experienceEntry), | ||
locale: localeCode, | ||
experienceStyles | ||
} | ||
}); | ||
} | ||
}; | ||
return ( | ||
<ExperienceRoot experience={experience} locale={localeCode} /> | ||
) | ||
} |
This file contains 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