Skip to content

Commit 03acd89

Browse files
committed
Fix plausible script tags and make them configurable (#105)
* Add plausible through metadata * Use Gatsby SiteMetadata for plausible domain and src * Use automatic plausible inference and relative proxy source links * Disable plausible on example * v0.1.23 * Add plausibleAPI * Re-disable plausible in example * Fix conditional plausible disable * Re-add defaults for max compatibility * Use disablePlausible flag and change mechanism to signal with src * Use pluginOptions for src/api/domain and use src presence over extra boolean flag * Allow null on plausible options * Change src to null
1 parent 82652a0 commit 03acd89

File tree

7 files changed

+92
-38
lines changed

7 files changed

+92
-38
lines changed

packages/example/gatsby-config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ module.exports = {
99
title: 'Example website',
1010
description: 'Example website description',
1111
keywords: ['docs', 'test'],
12-
siteUrl: 'http://localhost:8000'
12+
siteUrl: 'http://gatsby-theme-iterative-example.herokuapp.com'
1313
},
1414
plugins: [
1515
{
1616
resolve: themePackageName,
1717
options: {
18-
simpleLinkerTerms: require('./content/linked-terms')
18+
simpleLinkerTerms: require('./content/linked-terms'),
19+
plausibleSrc: null
1920
}
2021
},
2122
{

packages/gatsby-theme-iterative/gatsby-config.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const defaults = require('./config-defaults')
2020

2121
module.exports = ({
2222
simpleLinkerTerms,
23+
plausibleSrc = '/pl/js/plausible.outbound-links.js',
24+
plausibleAPI = '/pl/api/event',
25+
plausibleDomain,
2326
postCssPlugins = [
2427
require('tailwindcss/nesting')(require('postcss-nested')),
2528
autoprefixer,
@@ -154,7 +157,9 @@ module.exports = ({
154157
].filter(Boolean),
155158
siteMetadata: {
156159
author: 'Iterative',
157-
siteUrl: 'https://example.com',
158-
titleTemplate: ''
160+
titleTemplate: '',
161+
plausibleSrc,
162+
plausibleAPI,
163+
plausibleDomain
159164
}
160165
})

packages/gatsby-theme-iterative/gatsby-node.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ exports.pluginOptionsSchema = ({ Joi }) => {
3232
postCssPlugins: Joi.array(),
3333
argsLinkerPath: Joi.alternatives()
3434
.try(Joi.string(), Joi.array().items(Joi.string()))
35-
.default(defaults.argsLinkerPath)
35+
.default(defaults.argsLinkerPath),
36+
plausibleSrc: [Joi.string().optional(), Joi.allow(null)],
37+
plausibleAPI: [Joi.string().optional(), Joi.allow(null)],
38+
plausibleDomain: [Joi.string().optional(), Joi.allow(null)]
3639
})
3740
}
3841

@@ -63,6 +66,17 @@ exports.createSchemaCustomization = async api => {
6366
name: 'String!',
6467
match: '[String]'
6568
}
69+
}),
70+
buildObjectType({
71+
name: 'SiteSiteMetadata',
72+
fields: {
73+
author: 'String',
74+
siteUrl: 'String',
75+
titleTemplate: 'String',
76+
plausibleSrc: 'String',
77+
plausibleDomain: 'String',
78+
plausibleAPI: 'String'
79+
}
6680
})
6781
])
6882
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-env node */
2+
const React = require('react')
3+
4+
const PageWrapper = require('./src/components/PageWrapper').default
5+
6+
exports.wrapPageElement = PageWrapper
7+
8+
const onRenderBody = ({ setHeadComponents }, { plausibleSrc }) => {
9+
if (plausibleSrc) {
10+
return setHeadComponents([
11+
<script
12+
key="plausible-custom-events"
13+
dangerouslySetInnerHTML={{
14+
__html:
15+
'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }'
16+
}}
17+
/>
18+
])
19+
}
20+
}
21+
22+
exports.onRenderBody = onRenderBody

packages/gatsby-theme-iterative/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dvcorg/gatsby-theme-iterative",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "",
55
"main": "index.js",
66
"types": "src/typings.d.ts",

packages/gatsby-theme-iterative/src/components/MainLayout/DefaultSEO/index.tsx

+38-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react'
2-
import { Script } from 'gatsby'
32
import Helmet from 'react-helmet'
43

54
import { MetaProps } from '../../SEO'
@@ -17,11 +16,16 @@ const metaImage = {
1716

1817
const DefaultSEO: React.FC<IDefaultSEOProps> = ({ pathname }) => {
1918
const siteMeta = getSiteMeta()
20-
const siteUrl = siteMeta.siteUrl
21-
const metaTitle = siteMeta.title
22-
const metaTitleTemplate = siteMeta.titleTemplate
23-
const metaDescription = siteMeta.description
24-
const metaKeywords = siteMeta.keywords
19+
const {
20+
siteUrl,
21+
titleTemplate,
22+
title: metaTitle,
23+
description: metaDescription,
24+
keywords: metaKeywords,
25+
plausibleDomain,
26+
plausibleSrc,
27+
plausibleAPI
28+
} = siteMeta
2529
const fullUrl = siteUrl + pathname
2630

2731
const meta: MetaProps[] = [
@@ -104,32 +108,34 @@ const DefaultSEO: React.FC<IDefaultSEOProps> = ({ pathname }) => {
104108
]
105109

106110
return (
107-
<>
108-
<Helmet
109-
htmlAttributes={{
110-
lang: 'en'
111-
}}
112-
defaultTitle={metaTitle}
113-
titleTemplate={metaTitleTemplate || `%s | ${metaTitle}`}
114-
meta={meta}
115-
link={[
116-
{
117-
rel: 'mask-icon',
118-
href: '/safari-pinned-tab.svg',
119-
color: '#13adc7'
120-
},
121-
{
122-
rel: 'canonical',
123-
href: fullUrl
124-
}
125-
]}
126-
/>
127-
<Script
128-
defer
129-
data-domain="dvc.org"
130-
src="https://plausible.io/js/plausible.outbound-links.js"
131-
/>
132-
</>
111+
<Helmet
112+
htmlAttributes={{
113+
lang: 'en'
114+
}}
115+
defaultTitle={metaTitle}
116+
titleTemplate={titleTemplate || `%s | ${metaTitle}`}
117+
meta={meta}
118+
link={[
119+
{
120+
rel: 'mask-icon',
121+
href: '/safari-pinned-tab.svg',
122+
color: '#13adc7'
123+
},
124+
{
125+
rel: 'canonical',
126+
href: fullUrl
127+
}
128+
]}
129+
>
130+
{plausibleSrc ? (
131+
<script
132+
defer
133+
data-domain={plausibleDomain || new URL(siteUrl).hostname}
134+
data-api={plausibleAPI || undefined}
135+
src={plausibleSrc}
136+
/>
137+
) : null}
138+
</Helmet>
133139
)
134140
}
135141

packages/gatsby-theme-iterative/src/queries/siteMeta.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ interface ISiteMeta {
66
keywords: string
77
siteUrl: string
88
titleTemplate: string
9+
plausibleDomain: string | null
10+
plausibleSrc: string | null
11+
plausibleAPI: string | null
912
}
1013

1114
export default function siteMeta(): ISiteMeta {
@@ -21,6 +24,9 @@ export default function siteMeta(): ISiteMeta {
2124
keywords
2225
siteUrl
2326
titleTemplate
27+
plausibleDomain
28+
plausibleSrc
29+
plausibleAPI
2430
}
2531
}
2632
}

0 commit comments

Comments
 (0)