Skip to content

Commit 59dc583

Browse files
piehimjoshin
andauthored
fix: add more meaningful error message when nested slices are used (#36800)
* fix: add more meaningful error message when nested slices are used * add more contextual information if available * Update packages/gatsby/cache-dir/slice.js Co-authored-by: Josh Johnson <[email protected]> Co-authored-by: Josh Johnson <[email protected]>
1 parent 93e0257 commit 59dc583

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/gatsby/cache-dir/slice.js

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ export function Slice(props) {
3232
} else if (slicesContext.renderEnvironment === `engines`) {
3333
// if we're in SSR, we'll just render the component as is
3434
return <InlineSlice {...internalProps} />
35+
} else if (slicesContext.renderEnvironment === `slices`) {
36+
// we are not yet supporting nested slices
37+
38+
let additionalContextMessage = ``
39+
40+
// just in case generating additional contextual information fails, we still want the base message to show
41+
// and not show another cryptic error message
42+
try {
43+
additionalContextMessage = `\n\nSlice component "${slicesContext.sliceRoot.name}" (${slicesContext.sliceRoot.componentPath}) tried to render <Slice alias="${props.alias}"/>`
44+
} catch {
45+
// don't need to handle it, we will just skip the additional context message if we fail to generate it
46+
}
47+
48+
throw new Error(
49+
`Nested slices are not supported.${additionalContextMessage}\n\nSee https://v5.gatsbyjs.com/docs/reference/built-in-components/gatsby-slice#nested-slices`
50+
)
3551
} else {
3652
throw new Error(
3753
`Slice context "${slicesContext.renderEnvironment}" is not supported.`

packages/gatsby/cache-dir/static-entry.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,20 @@ export { StaticQueryContext, React }
553553
export async function renderSlice({ slice, staticQueryContext, props = {} }) {
554554
const { default: SliceComponent } = await getPageChunk(slice)
555555

556+
const slicesContext = {
557+
// we are not yet supporting using <Slice /> placeholders within slice components
558+
// setting this renderEnvironemnt to throw meaningful error on `<Slice />` usage
559+
// `slices` renderEnvironment should be removed once we support nested `<Slice />` placeholders
560+
renderEnvironment: `slices`,
561+
sliceRoot: slice,
562+
}
563+
556564
const sliceElement = (
557-
<StaticQueryContext.Provider value={staticQueryContext}>
558-
<SliceComponent sliceContext={slice.context} {...props} />
559-
</StaticQueryContext.Provider>
565+
<SlicesContext.Provider value={slicesContext}>
566+
<StaticQueryContext.Provider value={staticQueryContext}>
567+
<SliceComponent sliceContext={slice.context} {...props} />
568+
</StaticQueryContext.Provider>
569+
</SlicesContext.Provider>
560570
)
561571
const sliceWrappedWithWrapRootElement = apiRunner(
562572
`wrapRootElement`,

0 commit comments

Comments
 (0)