Skip to content

Commit

Permalink
Avoid GraphQL errors when Algolia is not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-cummins committed Sep 13, 2023
1 parent 723e830 commit 3a23ee6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
9 changes: 5 additions & 4 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ module.exports = {
description: config.siteDescription,
siteUrl: config.siteUrl,
algolia: {
appId: process.env.ALGOLIA_APP_ID,
searchOnlyApiKey: process.env.ALGOLIA_SEARCH_ONLY_API_KEY,
indexName: process.env.ALGOLIA_INDEX_NAME
appId: process.env.ALGOLIA_APP_ID || "none",
searchOnlyApiKey: process.env.ALGOLIA_SEARCH_ONLY_API_KEY || "none",
indexName: process.env.ALGOLIA_INDEX_NAME || "none",
available: !algoliaMissing
}
},
plugins: [
{
resolve: `gatsby-plugin-beam-analytics`,
options: {
dataToken: process.env.BEAM_ID
dataToken: process.env.BEAM_ID || "none"
}
},
`gatsby-plugin-styled-jsx`, // the plugin's code is inserted directly to gatsby-node.js and gatsby-ssr.js files
Expand Down
6 changes: 5 additions & 1 deletion plugins/gatsby-plugin-beam-analytics/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const React = require("react");

exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
if (pluginOptions.dataToken && process.env.NODE_ENV === "production") {
if (
pluginOptions.dataToken &&
pluginOptions.dataToken !== "none" &&
process.env.NODE_ENV === "production"
) {
setHeadComponents([
<script
key="beam"
Expand Down
1 change: 1 addition & 0 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class PureHeader extends React.Component {
fontLoaded={loaded}
pages={pages}
theme={theme}
searchAvailable={this.props.searchAvailable}
/>
)}
</ScreenWidthContext.Consumer>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Menu extends React.Component {
{ to: "/type/podcast", label: "Podcasts", icon: icon("podcast") },
{ to: "/type/book", label: "Books", icon: icon("book") },
{ to: "/category/", label: "Topics", icon: Tag },
{ to: "/search/", label: "Search", icon: Search },
this.props.searchAvailable && { to: "/search/", label: "Search", icon: Search },
...pages,
{ to: "/contact/", label: "Contact", icon: Envelope }
];
].filter(item => item); // Lazily filter out undefined items

this.renderedItems = []; // will contain references to rendered DOM elements of menu
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Menu/Menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ describe("Menu", () => {
fontLoaded={loaded}
pages={pages}
theme={theme}
searchAvailable={true}
/>
);
});

it("includes a search item", () => {
expect(screen.getByText("Search")).toBeTruthy();
expect(screen.queryByText("Search")).toBeTruthy();
});
});
23 changes: 22 additions & 1 deletion src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,25 @@ class Layout extends React.Component {
id
htmlAst
}
site {
siteMetadata {
algolia {
available
}
}
}
}
`}
render={data => {
const { children } = this.props;
const {
footnote: { htmlAst: footnoteHTMLAst, html: footnoteHTML },
pages: { edges: pages }
pages: { edges: pages },
site: {
siteMetadata: {
algolia: { available: searchAvailable }
}
}
} = data;

return (
Expand All @@ -119,6 +131,7 @@ class Layout extends React.Component {
path={this.props.location.pathname}
pages={pages}
theme={this.state.theme}
searchAvailable={searchAvailable}
/>
<main>{children}</main>
<Footer
Expand All @@ -137,18 +150,21 @@ class Layout extends React.Component {
html {
box-sizing: border-box;
}
*,
*:after,
*:before {
box-sizing: inherit;
margin: 0;
padding: 0;
}
body {
font-family: ${this.state.font400loaded
? "'Open Sans', sans-serif;"
: "Arial, sans-serif;"};
}
h1,
h2,
h3 {
Expand All @@ -157,19 +173,24 @@ class Layout extends React.Component {
letter-spacing: -0.03em;
margin: 0;
}
h1 {
letter-spacing: -0.04em;
}
p {
margin: 0;
}
strong {
font-weight: ${this.state.font600loaded ? 600 : 400};
}
a {
text-decoration: none;
color: #666;
}
main {
width: auto;
display: block;
Expand Down
17 changes: 13 additions & 4 deletions test-integration/frontpage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const { authorName } = require("../src/utils/configger");

const siteRoot = `http://localhost:${port}`;

// eslint-disable-next-line jest/expect-expect,jest/valid-title,jest/no-disabled-tests
const itSometimes = (condition, ...args) => (condition ? test(...args) : test.skip(...args));

describe("main site", () => {
beforeAll(async () => {
await page.goto(siteRoot);
Expand All @@ -21,9 +24,15 @@ describe("main site", () => {
});

describe("header navigation bar", () => {
it("should have a Search option", async () => {
await expect(page.waitForXPath('//*[text()="Search"]')).resolves.toBeTruthy();
});
itSometimes(
process.env.ALGOLIA_APP_ID && process.env.ALGOLIA_APP_ID !== "none",
"should have a Search option",
async () => {
// We should only have a search bar if we have algolia configured
// eslint-disable-next-line jest/no-standalone-expect
await expect(page.waitForXPath('//*[text()="Search"]')).resolves.toBeTruthy();
}
);

it("should have a Contact option", async () => {
await expect(page.waitForXPath('//*[text()="Contact"]')).resolves.toBeTruthy();
Expand All @@ -47,7 +56,7 @@ describe("main site", () => {
await expect(scrollButton.isIntersectingViewport()).resolves.toBeTruthy();

// Click, which should scroll
await scrollButton.evaluate(scrollButton => scrollButton.click());
await scrollButton.evaluate(but => but.click());
// Nasty hack; wait for some scrolling to happen
await page.waitFor(2000);
// Now the button should be out of view
Expand Down

0 comments on commit 3a23ee6

Please sign in to comment.