Skip to content
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

Put GTM first so it gets loaded regardless of the ; problem #249

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
38 changes: 8 additions & 30 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

const math = require('remark-math');
const katex = require('rehype-katex');
const fs = require('fs').promises;

const metaTagStringToReplace = 'contentOfContentSecurityPolicyGoesHere';
// script-src causes development builds to fail
// But unsafe-eval should NOT be in production builds
// Also, put GTM first because sometimes the ';' in the escaped single quotes causes the browser to think it's the end
const scriptSrc = process.env.NODE_ENV === 'development' ?
`https://*.googletagmanager.com 'self' 'unsafe-inline' 'unsafe-eval'`
: `https://*.googletagmanager.com 'self' 'unsafe-inline'`;

const contentSecurityPolicy = `
default-src 'none';
base-uri 'self';
manifest-src 'self';
script-src 'self' 'unsafe-inline' https://*.googletagmanager.com;
script-src ${scriptSrc};
style-src 'self' 'unsafe-inline';
font-src 'self';
img-src 'self' https://*.googletagmanager.com https://*.google-analytics.com data:;
Expand All @@ -20,25 +24,6 @@ form-action 'self';
connect-src 'self' https://*.algolia.net https://*.algolianet.com https://*.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com;
frame-src https://tezosbot.vercel.app https://calendly.com/ lucid.app;`;

// Update the CSP tsg after builds
// because docusaurus always escapes the quotes
// https://github.com/facebook/docusaurus/issues/9686
const updateMetaTag = async (outDir, route) => {
const filePath = route.endsWith('.html')
? outDir + route
: outDir + route + '/index.html';
const fileContent = await fs.readFile(filePath,
'utf8');
const updatedFileContent = fileContent.replace(metaTagStringToReplace, contentSecurityPolicy);
await fs.writeFile(filePath, updatedFileContent, 'utf8');
}

// script-src causes development builds to fail
// But unsafe-eval should NOT be in production builds
const scriptSrc = process.env.NODE_ENV === 'development' ?
`'self' 'unsafe-inline' 'unsafe-eval' https://*.googletagmanager.com;`
: `'self' 'unsafe-inline' https://*.googletagmanager.com;`;

/** @type {import('@docusaurus/types').Config} */
const config = {
title: 'Tezos Developer Documentation',
Expand All @@ -63,7 +48,7 @@ const config = {
tagName: 'meta',
attributes: {
'http-equiv': 'Content-Security-Policy',
content: metaTagStringToReplace,
content: contentSecurityPolicy,
},
},
],
Expand Down Expand Up @@ -95,13 +80,6 @@ const config = {

plugins: [
'plugin-image-zoom',
() => ({
async postBuild({ routesPaths, outDir }) {
await Promise.all(routesPaths.map((oneRoute) =>
updateMetaTag(outDir, oneRoute)
));
},
}),
],

themeConfig:
Expand Down