Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
fix: get layouts working with quickstart pages
Browse files Browse the repository at this point in the history
* The layout keyed off of a value on the mdx pages that we don't have
  access to.
* This updates the layout logic to check for the `layout` field on a
  page to decide which layout to use.
* This also adds an `onCreatePage` lifecycle method to add the `layout`
  field to the index page.
  • Loading branch information
aswanson-nr authored and tabathadelane committed Jan 28, 2022
1 parent 7eda836 commit 01979e4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
edges {
node {
fields {
fileRelativePath
slug
}
id
Expand Down Expand Up @@ -39,12 +38,22 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
context: {
id,
layout: 'QuickStartLayout',
fileRelativePath,
},
});
});
};

exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions;
const oldPage = { ...page };

if (page.path === '/') {
page.context.layout = 'QuickStartLayout';
}
deletePage(oldPage);
createPage(page);
};

exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"develop": "GATSBY_NEWRELIC_ENV=development gatsby develop",
"start": "gatsby develop",
"start": "yarn develop",
"build": "GATSBY_NEWRELIC_ENV=production gatsby build",
"build:production": "yarn build --prefix-paths",
"serve": "gatsby serve",
Expand Down
6 changes: 3 additions & 3 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import QuickStartLayout from './QuickStartLayout';
import PropTypes from 'prop-types';

const Layout = ({ children, pageContext }) => {
if (pageContext.fileRelativePath.match(/404/)) {
return children;
if (pageContext.layout === 'QuickStartLayout') {
return <QuickStartLayout>{children}</QuickStartLayout>;
}

return <QuickStartLayout>{children}</QuickStartLayout>;
return <>{children}</>;
};

Layout.propTypes = {
Expand Down

0 comments on commit 01979e4

Please sign in to comment.