Skip to content

Fix plausible script tags and make them configurable (maintenance -> main) #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/example/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ module.exports = {
title: 'Example website',
description: 'Example website description',
keywords: ['docs', 'test'],
siteUrl: 'http://localhost:8000'
siteUrl: 'http://gatsby-theme-iterative-example.herokuapp.com'
},
plugins: [
{
resolve: themePackageName,
options: {
simpleLinkerTerms: require('./content/linked-terms')
simpleLinkerTerms: require('./content/linked-terms'),
plausibleSrc: null
}
},
{
Expand Down
9 changes: 7 additions & 2 deletions packages/gatsby-theme-iterative/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const defaults = require('./config-defaults')

module.exports = ({
simpleLinkerTerms,
plausibleSrc = '/pl/js/plausible.outbound-links.js',
plausibleAPI = '/pl/api/event',
plausibleDomain,
postCssPlugins = [
require('tailwindcss/nesting')(require('postcss-nested')),
autoprefixer,
Expand Down Expand Up @@ -154,7 +157,9 @@ module.exports = ({
].filter(Boolean),
siteMetadata: {
author: 'Iterative',
siteUrl: 'https://example.com',
titleTemplate: ''
titleTemplate: '',
plausibleSrc,
plausibleAPI,
plausibleDomain
}
})
16 changes: 15 additions & 1 deletion packages/gatsby-theme-iterative/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ exports.pluginOptionsSchema = ({ Joi }) => {
postCssPlugins: Joi.array(),
argsLinkerPath: Joi.alternatives()
.try(Joi.string(), Joi.array().items(Joi.string()))
.default(defaults.argsLinkerPath)
.default(defaults.argsLinkerPath),
plausibleSrc: [Joi.string().optional(), Joi.allow(null)],
plausibleAPI: [Joi.string().optional(), Joi.allow(null)],
plausibleDomain: [Joi.string().optional(), Joi.allow(null)]
})
}

Expand Down Expand Up @@ -63,6 +66,17 @@ exports.createSchemaCustomization = async api => {
name: 'String!',
match: '[String]'
}
}),
buildObjectType({
name: 'SiteSiteMetadata',
fields: {
author: 'String',
siteUrl: 'String',
titleTemplate: 'String',
plausibleSrc: 'String',
plausibleDomain: 'String',
plausibleAPI: 'String'
}
})
])
}
Expand Down
18 changes: 18 additions & 0 deletions packages/gatsby-theme-iterative/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-env node */
const React = require('react')

const onRenderBody = ({ setHeadComponents }, { plausibleSrc }) => {
if (plausibleSrc) {
return setHeadComponents([
<script
key="plausible-custom-events"
dangerouslySetInnerHTML={{
__html:
'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }'
}}
/>
])
}
}

exports.onRenderBody = onRenderBody
2 changes: 1 addition & 1 deletion packages/gatsby-theme-iterative/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dvcorg/gatsby-theme-iterative",
"version": "0.2.0",
"version": "0.2.1",
"description": "",
"main": "index.js",
"types": "src/typings.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import { Script } from 'gatsby'
import Helmet from 'react-helmet'

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

const DefaultSEO: React.FC<IDefaultSEOProps> = ({ pathname }) => {
const siteMeta = getSiteMeta()
const siteUrl = siteMeta.siteUrl
const metaTitle = siteMeta.title
const metaTitleTemplate = siteMeta.titleTemplate
const metaDescription = siteMeta.description
const metaKeywords = siteMeta.keywords
const {
siteUrl,
titleTemplate,
title: metaTitle,
description: metaDescription,
keywords: metaKeywords,
plausibleDomain,
plausibleSrc,
plausibleAPI
} = siteMeta
const fullUrl = siteUrl + pathname

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

return (
<>
<Helmet
htmlAttributes={{
lang: 'en'
}}
defaultTitle={metaTitle}
titleTemplate={metaTitleTemplate || `%s | ${metaTitle}`}
meta={meta}
link={[
{
rel: 'mask-icon',
href: '/safari-pinned-tab.svg',
color: '#13adc7'
},
{
rel: 'canonical',
href: fullUrl
}
]}
/>
<Script
defer
data-domain="dvc.org"
src="https://plausible.io/js/plausible.outbound-links.js"
/>
</>
<Helmet
htmlAttributes={{
lang: 'en'
}}
defaultTitle={metaTitle}
titleTemplate={titleTemplate || `%s | ${metaTitle}`}
meta={meta}
link={[
{
rel: 'mask-icon',
href: '/safari-pinned-tab.svg',
color: '#13adc7'
},
{
rel: 'canonical',
href: fullUrl
}
]}
>
{plausibleSrc ? (
<script
defer
data-domain={plausibleDomain || new URL(siteUrl).hostname}
data-api={plausibleAPI || undefined}
src={plausibleSrc}
/>
) : null}
</Helmet>
)
}

Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby-theme-iterative/src/queries/siteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ interface ISiteMeta {
keywords: string
siteUrl: string
titleTemplate: string
plausibleDomain: string | null
plausibleSrc: string | null
plausibleAPI: string | null
}

export default function siteMeta(): ISiteMeta {
Expand All @@ -21,6 +24,9 @@ export default function siteMeta(): ISiteMeta {
keywords
siteUrl
titleTemplate
plausibleDomain
plausibleSrc
plausibleAPI
}
}
}
Expand Down