使用gatsby下载的项目,在develop环境正常运行但Gatsby build时总是报错
failed Building static HTML for pages - 1.539s
ERROR #95313 HTML.COMPILATION
Building static HTML failed for path "/404/"
See our docs page for more info on this error: https://gatsby.dev/debug-html
37 | var setComponents = pluginConfig.head ? setHeadComponents : setPostBodyComponents;
38 | var renderHtml = function renderHtml() {
> 39 | return "\n " + (excludeGtagPaths.length ? "window.excludeGtagPaths=[" + excludeGtagPaths.join(",") + "];" : "") + "\n " + (typeof
gtagConfig.anonymize_ip !== "undefined" && gtagConfig.anonymize_ip === true ? "function gaOptout(){document.cookie=disableStr+'=true; expires=Thu, 31 Dec 2099
23:59:59 UTC;path=/',window[disableStr]=!0}var gaProperty='" + firstTrackingId +
"',disableStr='ga-disable-'+gaProperty;document.cookie.indexOf(disableStr+'=true')>-1&&(window[disableStr]=!0);" : "") + "\n if(" + (pluginConfig.respectDNT ?
"!(navigator.doNotTrack == \"1\" || window.doNotTrack == \"1\")" : "true") + ") {\n window.dataLayer = window.dataLayer || [];\n function
gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n\n " + pluginOptions.trackingIds.map(function (trackingId) {
|
^
40 | return "gtag('config', '" + trackingId + "', " + JSON.stringify(gtagConfig) + ");";
41 | }).join("") + "\n }\n ";
42 | };
WebpackError: TypeError: Cannot read properties of undefined (reading 'map') (from plugin: gatsby-plugin-google-gtag)
- gatsby-ssr.js:39
[my-gatsby-site]/[gatsby-plugin-google-gtag]/gatsby-ssr.js:39:810
- gatsby-ssr.js:50
[my-gatsby-site]/[gatsby-plugin-google-gtag]/gatsby-ssr.js:50:15
- api-runner-ssr.js:48
my-gatsby-site/.cache/api-runner-ssr.js:48:22
- api-runner-ssr.js:41
my-gatsby-site/.cache/api-runner-ssr.js:41:11
- static-entry.js:356
my-gatsby-site/.cache/static-entry.js:356:5
- index.js:57
[my-gatsby-site]/[balanced-match]/index.js:57:1
- attributes.js:128
[my-gatsby-site]/[css-select]/lib/attributes.js:128:1
- attributes.js:93
[my-gatsby-site]/[css-select]/lib/attributes.js:93:1
解决方案
// Gatsby.config.js
plugins: [
"gatsby-plugin-sass",
// "gatsby-plugin-google-gtag" 这样写会报错
{
resolve: "gatsby-plugin-google-gtag",
options: {
trackingIds: [process.env.GATSBY_GA_MEASUREMENT_ID],
gtagConfig: {
optimize_id: [process.env.GATSBY_GTM_ID],
anonymize_ip: true,
cookie_expires: 0,
},
pluginConfig: {
head: true,
},
},
},
]
持续更新中....