Skip to content

Commit 97daebb

Browse files
committed
Merge branch 'master' into master+social-image
2 parents 8c149c5 + 5ab4a87 commit 97daebb

26 files changed

+450
-160
lines changed

.all-contributorsrc

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
"contributions": [
6060
"code"
6161
]
62+
},
63+
{
64+
"login": "thedavedavies",
65+
"name": "Dave",
66+
"avatar_url": "https://avatars.githubusercontent.com/u/2972436?v=4",
67+
"profile": "https://highaltitude.io/",
68+
"contributions": [
69+
"code"
70+
]
6271
}
6372
],
6473
"contributorsPerLine": 7,

.prettierignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ yarn.lock
4040
.prettierignore
4141
.gitignore
4242

43-
public
43+
public
44+
45+
.all-contributorsrc

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Next.js WordPress Starter
22

33
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4-
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
4+
[![All Contributors](https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square)](#contributors-)
55
<!-- ALL-CONTRIBUTORS-BADGE:END -->
66

77
Scaling WordPress with the power of [Next.js](https://nextjs.org/) and the static web!
@@ -20,7 +20,7 @@ npx create-next-app -e https://github.com/colbyfayock/next-wordpress-starter
2020

2121
Add an `.env.local` file to the root with the following:
2222
```
23-
WORDPRESS_HOST="http://wordpressite.com"
23+
WORDPRESS_GRAPHQL_ENDPOINT="http://wordpressite.com/graphql"
2424
```
2525

2626
## 🚀 Getting Started
@@ -57,7 +57,7 @@ This project makes use of WPGraphQL to query WordPress with GraphQL. In order to
5757
Create a new file locally called `.env.local` and add the following:
5858

5959
```bash
60-
WORDPRESS_HOST="[host]"
60+
WORDPRESS_GRAPHQL_ENDPOINT="[WPGraphQL Endpoint]"
6161
```
6262

6363
Replace `[host]` with your the home URL of your WordPress instance.
@@ -113,17 +113,19 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
113113
<!-- markdownlint-disable -->
114114
<table>
115115
<tr>
116-
<td align="center"><a href="https://colbyfayock.com/newsletter"><img src="https://avatars2.githubusercontent.com/u/1045274?v=4" width="100px;" alt=""/><br /><sub><b>Colby Fayock</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=colbyfayock" title="Code">💻</a> <a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=colbyfayock" title="Documentation">📖</a></td>
117-
<td align="center"><a href="http://www.kevincunningham.co.uk"><img src="https://avatars3.githubusercontent.com/u/8320213?v=4" width="100px;" alt=""/><br /><sub><b>Kevin Cunningham</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=doingandlearning" title="Code">💻</a></td>
118-
<td align="center"><a href="http://guilleangulo.me"><img src="https://avatars0.githubusercontent.com/u/50624358?v=4" width="100px;" alt=""/><br /><sub><b>Guillermo Angulo</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=GuilleAngulo" title="Code">💻</a></td>
119-
<td align="center"><a href="http://www.heinsnyman.co.za"><img src="https://avatars0.githubusercontent.com/u/22816814?v=4" width="100px;" alt=""/><br /><sub><b>Hein Snyman</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=HeinSnyman" title="Code">💻</a></td>
120-
<td align="center"><a href="https://github.com/grische"><img src="https://avatars0.githubusercontent.com/u/2787581?v=4" width="100px;" alt=""/><br /><sub><b>Grische</b></sub></a><br /><a href="#tool-grische" title="Tools">🔧</a></td>
121-
<td align="center"><a href="https://github.com/jatin-rathee"><img src="https://avatars0.githubusercontent.com/u/44899844?v=4" width="100px;" alt=""/><br /><sub><b>Jatin Rathee</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=jatin-rathee" title="Code">💻</a></td>
116+
<td align="center"><a href="https://colbyfayock.com/newsletter"><img src="https://avatars2.githubusercontent.com/u/1045274?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Colby Fayock</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=colbyfayock" title="Code">💻</a> <a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=colbyfayock" title="Documentation">📖</a></td>
117+
<td align="center"><a href="http://www.kevincunningham.co.uk"><img src="https://avatars3.githubusercontent.com/u/8320213?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kevin Cunningham</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=doingandlearning" title="Code">💻</a></td>
118+
<td align="center"><a href="http://guilleangulo.me"><img src="https://avatars0.githubusercontent.com/u/50624358?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Guillermo Angulo</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=GuilleAngulo" title="Code">💻</a></td>
119+
<td align="center"><a href="http://www.heinsnyman.co.za"><img src="https://avatars0.githubusercontent.com/u/22816814?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hein Snyman</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=HeinSnyman" title="Code">💻</a></td>
120+
<td align="center"><a href="https://github.com/grische"><img src="https://avatars0.githubusercontent.com/u/2787581?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Grische</b></sub></a><br /><a href="#tool-grische" title="Tools">🔧</a></td>
121+
<td align="center"><a href="https://github.com/jatin-rathee"><img src="https://avatars0.githubusercontent.com/u/44899844?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jatin Rathee</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=jatin-rathee" title="Code">💻</a></td>
122+
<td align="center"><a href="https://highaltitude.io/"><img src="https://avatars.githubusercontent.com/u/2972436?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dave</b></sub></a><br /><a href="https://github.com/colbyfayock/next-wordpress-starter/commits?author=thedavedavies" title="Code">💻</a></td>
122123
</tr>
123124
</table>
124125

125-
<!-- markdownlint-enable -->
126+
<!-- markdownlint-restore -->
126127
<!-- prettier-ignore-end -->
128+
127129
<!-- ALL-CONTRIBUTORS-LIST:END -->
128130

129131
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

next.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ module.exports = withPlugins([[indexSearch], [feed], [sitemap], [socialImages]],
1313

1414
trailingSlash: true,
1515

16+
// By enabling verbose logging, it will provide additional output details for
17+
// diagnostic purposes. By default is set to false.
18+
//verbose: true,
19+
1620
env: {
1721
WORDPRESS_HOST: removeLastTrailingSlash(process.env.WORDPRESS_HOST),
22+
WORDPRESS_GRAPHQL_ENDPOINT: removeLastTrailingSlash(process.env.WORDPRESS_GRAPHQL_ENDPOINT),
1823
},
1924
});

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
"*": "yarn format:nopath"
2222
},
2323
"dependencies": {
24-
"@apollo/client": "^3.2.5",
24+
"@apollo/client": "^3.3.7",
2525
"canvas": "^2.6.1",
2626
"date-fns": "^2.16.1",
2727
"dotenv": "^8.2.0",
28-
"fuse.js": "^6.4.2",
28+
"fuse.js": "^6.4.6",
2929
"graphql": "^15.4.0",
3030
"he": "^1.2.0",
3131
"loader-utils": "^2.0.0",
3232
"next": "9.5.5",
33-
"next-compose-plugins": "^2.2.0",
33+
"next-compose-plugins": "^2.2.1",
3434
"parameterize": "^1.0.0",
3535
"path": "^0.12.7",
3636
"react": "16.13.1",
3737
"react-dom": "16.13.1",
3838
"react-helmet": "^6.1.0",
3939
"react-icons": "^3.11.0",
4040
"rss": "^1.2.2",
41-
"sass": "^1.27.0",
41+
"sass": "^1.32.5",
4242
"style.css": "^1.0.0"
4343
},
4444
"devDependencies": {

plugins/feed.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const path = require('path');
22
const { getFeedData, generateFeed } = require('./util');
33

4-
const WebpackPlugin = require('./plugin-compiler');
4+
const WebpackPluginCompiler = require('./plugin-compiler');
55

66
module.exports = function feed(nextConfig = {}) {
7-
const { env, outputDirectory, outputName } = nextConfig;
7+
const { env, outputDirectory, outputName, verbose = false } = nextConfig;
88

99
const plugin = {
1010
name: 'Feed',
@@ -14,7 +14,7 @@ module.exports = function feed(nextConfig = {}) {
1414
generate: generateFeed,
1515
};
1616

17-
const { WORDPRESS_HOST } = env;
17+
const { WORDPRESS_GRAPHQL_ENDPOINT, WORDPRESS_HOST } = env;
1818

1919
return Object.assign({}, nextConfig, {
2020
webpack(config, options) {
@@ -23,9 +23,11 @@ module.exports = function feed(nextConfig = {}) {
2323
}
2424

2525
config.plugins.push(
26-
new WebpackPlugin({
26+
new WebpackPluginCompiler({
2727
host: WORDPRESS_HOST,
28+
url: WORDPRESS_GRAPHQL_ENDPOINT,
2829
plugin,
30+
verbose,
2931
})
3032
);
3133

plugins/plugin-compiler.js

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
const path = require('path');
22

3-
const { createApolloClient, createFile } = require('./util');
3+
const { createApolloClient, createFile, terminalColor } = require('./util');
4+
5+
const DEFAULT_GRAPHQL_PATH = '/graphql';
46

57
class WebpackPlugin {
68
constructor(options = {}) {
79
this.options = options;
810
}
911

1012
async index(compilation, options) {
11-
const { host, plugin } = options;
13+
const { url, host, plugin, verbose = false } = options;
14+
let endpoint = url;
1215

13-
plugin.outputLocation = path.join(plugin.outputDirectory, plugin.outputName);
16+
if (!endpoint && host) {
17+
endpoint = `${host}${DEFAULT_GRAPHQL_PATH}`;
18+
}
1419

15-
console.log(`[${plugin.name}] Compiling file ${plugin.outputLocation}`);
20+
try {
21+
plugin.outputLocation = path.join(plugin.outputDirectory, plugin.outputName);
1622

17-
if (typeof host !== 'string') {
18-
throw new Error(`[${plugin.name}] Failed to compile: invalid host type ${typeof host}`);
19-
}
23+
verbose && console.log(`[${plugin.name}] Compiling file ${plugin.outputLocation}`);
24+
25+
const hasUrl = typeof url === 'string';
26+
const hasHost = typeof host === 'string';
27+
28+
if (!hasUrl && !hasHost) {
29+
throw new Error(
30+
`[${plugin.name}] Failed to compile: Please check that either WORDPRESS_GRAPHQL_ENDPOINT or WORDPRESS_HOST is set and configured properly.`
31+
);
32+
}
33+
34+
const apolloClient = createApolloClient(endpoint);
2035

21-
const apolloClient = createApolloClient(host);
36+
const data = await plugin.getData(apolloClient, plugin.name, verbose);
2237

23-
const data = await plugin.getData(apolloClient, plugin.name);
38+
const file = plugin.generate(data);
2439

25-
const file = plugin.generate(data);
40+
await createFile(file, plugin.name, plugin.outputDirectory, plugin.outputLocation, verbose);
2641

27-
await createFile(file, plugin.name, plugin.outputDirectory, plugin.outputLocation);
42+
//If there is an aditional action to perform
43+
if (!!plugin.postcreate) {
44+
plugin.postcreate(plugin);
45+
}
2846

29-
//If there is an aditional action to perform
30-
if (!!plugin.postcreate) {
31-
plugin.postcreate(plugin);
47+
!verbose && console.log(`Successfully created: ${terminalColor(plugin.outputName, 'info')}`);
48+
} catch (e) {
49+
console.error(`${terminalColor(e.message, 'error')}`);
3250
}
3351
}
3452

plugins/search-index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const path = require('path');
22
const { getAllPosts, generateIndexSearch } = require('./util');
33

4-
const WebpackPlugin = require('./plugin-compiler');
4+
const WebpackPluginCompiler = require('./plugin-compiler');
55

66
module.exports = function indexSearch(nextConfig = {}) {
7-
const { env, outputDirectory, outputName } = nextConfig;
7+
const { env, outputDirectory, outputName, verbose = false } = nextConfig;
88

99
const plugin = {
1010
name: 'SearchIndex',
@@ -14,7 +14,7 @@ module.exports = function indexSearch(nextConfig = {}) {
1414
generate: generateIndexSearch,
1515
};
1616

17-
const { WORDPRESS_HOST } = env;
17+
const { WORDPRESS_GRAPHQL_ENDPOINT, WORDPRESS_HOST } = env;
1818

1919
return Object.assign({}, nextConfig, {
2020
webpack(config, options) {
@@ -23,9 +23,11 @@ module.exports = function indexSearch(nextConfig = {}) {
2323
}
2424

2525
config.plugins.push(
26-
new WebpackPlugin({
26+
new WebpackPluginCompiler({
2727
host: WORDPRESS_HOST,
28+
url: WORDPRESS_GRAPHQL_ENDPOINT,
2829
plugin,
30+
verbose,
2931
})
3032
);
3133

plugins/sitemap.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const path = require('path');
22
const { getSitemapData, generateSitemap, generateRobotsTxt } = require('./util');
33

4-
const WebpackPlugin = require('./plugin-compiler');
4+
const WebpackPluginCompiler = require('./plugin-compiler');
55

66
module.exports = function sitemap(nextConfig = {}) {
7-
const { env, outputDirectory, outputName } = nextConfig;
7+
const { env, outputDirectory, outputName, verbose = false } = nextConfig;
88

99
const plugin = {
1010
name: 'Sitemap',
@@ -15,7 +15,7 @@ module.exports = function sitemap(nextConfig = {}) {
1515
postcreate: generateRobotsTxt,
1616
};
1717

18-
const { WORDPRESS_HOST } = env;
18+
const { WORDPRESS_GRAPHQL_ENDPOINT, WORDPRESS_HOST } = env;
1919

2020
return Object.assign({}, nextConfig, {
2121
webpack(config, options) {
@@ -24,9 +24,11 @@ module.exports = function sitemap(nextConfig = {}) {
2424
}
2525

2626
config.plugins.push(
27-
new WebpackPlugin({
27+
new WebpackPluginCompiler({
2828
host: WORDPRESS_HOST,
29+
url: WORDPRESS_GRAPHQL_ENDPOINT,
2930
plugin,
31+
verbose,
3032
})
3133
);
3234

0 commit comments

Comments
 (0)