mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-06 04:22:01 +00:00
migrate gatsby-plugin-vercel to @vercel/gatsby-plugin-vercel-analytics
This commit is contained in:
@@ -42,3 +42,6 @@ packages/static-build/test/cache-fixtures
|
||||
|
||||
# redwood
|
||||
packages/redwood/test/fixtures
|
||||
|
||||
# gatsby-plugin-vercel-analytics
|
||||
packages/gatsby-plugin-vercel-analytics
|
||||
|
||||
@@ -4,3 +4,6 @@
|
||||
packages/cli/test/dev/fixtures/edge-function-error/api/edge-error-syntax.js
|
||||
packages/cli/test/fixtures/unit/commands/build/node-error/api/typescript.ts
|
||||
examples/sveltekit-1
|
||||
|
||||
# gatsby-plugin-vercel-analytics
|
||||
packages/gatsby-plugin-vercel-analytics
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"presets": [["babel-preset-gatsby-package", { "browser": true }]]
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
name: Publish
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [16.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: yarn install, publish
|
||||
run: |
|
||||
yarn install --frozen-lockfile --check-files
|
||||
yarn semantic-release --branches main
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN_ELEVATED }}
|
||||
@@ -1,5 +1,2 @@
|
||||
/gatsby-browser.js
|
||||
/gatsby-node.js
|
||||
node_modules
|
||||
/web-vitals.js
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2020 Vercel, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -1,5 +1,15 @@
|
||||
# gatsby-plugin-vercel
|
||||
|
||||
---
|
||||
|
||||
⚠️ This repo was migrated from https://github.com/vercel/gatsby-plugin-vercel
|
||||
|
||||
It requires Node.js v12 to be built, so the latest, built version of `gatsby-plugin-vercel` has been committed to this repo _temporarily_ so that they can be included in the initial v1 publish of `@vercel/gatsby-plugin-vercel-analytics`.
|
||||
|
||||
At some point in the future, this plugin will be updated to Node.js v16 so that it can be included in the rest of this monorepo's build tooling.
|
||||
|
||||
---
|
||||
|
||||
This plugin sends [Core Web Vitals](https://web.dev/vitals/) to Vercel Analytics. This plugin is configured by default on Vercel. You **do not** need to install it manually. For more information, [read this post](https://vercel.com/blog/gatsby-analytics).
|
||||
|
||||
## Install
|
||||
|
||||
34
packages/gatsby-plugin-vercel-analytics/gatsby-browser.js
Normal file
34
packages/gatsby-plugin-vercel-analytics/gatsby-browser.js
Normal file
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.onClientEntry = void 0;
|
||||
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
|
||||
var _webVitals = require("./web-vitals");
|
||||
|
||||
var onClientEntry = function onClientEntry(_, pluginOptions) {
|
||||
if (pluginOptions === void 0) {
|
||||
pluginOptions = {};
|
||||
}
|
||||
|
||||
var options = (0, _extends2.default)({
|
||||
debug: false
|
||||
}, pluginOptions, {
|
||||
analyticsId: process.env.GATSBY_VERCEL_ANALYTICS_ID
|
||||
});
|
||||
|
||||
if (!options.analyticsId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.debug || process.env.NODE_ENV === "production") {
|
||||
(0, _webVitals.webVitals)({
|
||||
options: options
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
exports.onClientEntry = onClientEntry;
|
||||
@@ -1,46 +1,21 @@
|
||||
{
|
||||
"name": "gatsby-plugin-vercel",
|
||||
"version": "0.0.0-development",
|
||||
"name": "@vercel/gatsby-plugin-vercel-analytics",
|
||||
"version": "0.0.1",
|
||||
"description": "Track Core Web Vitals in Gatsby projects with Vercel Analytics.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "babel src --out-dir .",
|
||||
"test": "exit 0",
|
||||
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||
"watch": "babel -w src --out-dir .",
|
||||
"prepublishOnly": "yarn test",
|
||||
"semantic-release": "semantic-release",
|
||||
"commit": "git-cz"
|
||||
},
|
||||
"files": [
|
||||
"gatsby-browser.js",
|
||||
"web-vitals.js"
|
||||
],
|
||||
"scripts": {},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "7.12.1",
|
||||
"web-vitals": "0.2.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "7.12.1",
|
||||
"@babel/core": "7.12.3",
|
||||
"babel-preset-gatsby-package": "0.5.3",
|
||||
"cross-env": "7.0.2",
|
||||
"cz-conventional-changelog": "3.2.0",
|
||||
"husky": "^4.2.5",
|
||||
"prettier": "^2.0.5",
|
||||
"pretty-quick": "^2.0.1",
|
||||
"semantic-release": "19.0.5"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "./node_modules/cz-conventional-changelog"
|
||||
}
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "pretty-quick --staged"
|
||||
}
|
||||
},
|
||||
"prettier": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vercel/gatsby-plugin-vercel.git"
|
||||
"url": "https://github.com/vercel/vercel.git",
|
||||
"directory": "packages/gatsby-plugin-vercel-analytics"
|
||||
},
|
||||
"keywords": [
|
||||
"gatsby",
|
||||
@@ -54,9 +29,5 @@
|
||||
"lcp",
|
||||
"ttfb"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/vercel/gatsby-plugin-vercel/issues"
|
||||
},
|
||||
"homepage": "https://github.com/vercel/gatsby-plugin-vercel#readme"
|
||||
"license": "MIT"
|
||||
}
|
||||
|
||||
115
packages/gatsby-plugin-vercel-analytics/web-vitals.js
Normal file
115
packages/gatsby-plugin-vercel-analytics/web-vitals.js
Normal file
@@ -0,0 +1,115 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.webVitals = webVitals;
|
||||
|
||||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
||||
|
||||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
||||
|
||||
var _webVitals2 = require("web-vitals");
|
||||
|
||||
var isRegistered = false;
|
||||
|
||||
function onError(err) {
|
||||
console.error("[gatsby-plugin-vercel]", err); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
function onDebug(label, payload) {
|
||||
console.log(label, payload); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
function sendToAnalytics(metric, options) {
|
||||
var _$exec, _document$querySelect;
|
||||
|
||||
// Scrape the intial component name from the DOM:
|
||||
var pageScript = [].slice.call((_$exec = /^\/component---(.+)\-(.+?)\-.{20}\.js$/.exec((_document$querySelect = document.querySelector("script[src^=\"/component---\"]")) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.getAttribute("src"))) !== null && _$exec !== void 0 ? _$exec : []).slice(1).join("-");
|
||||
var chunkMapping = self.___chunkMapping ? typeof self.___chunkMapping === "string" ? JSON.parse(self.___chunkMapping) : self.___chunkMapping : {}; // Verify page name is correct:
|
||||
|
||||
var pageName = "component---" + pageScript in chunkMapping ? pageScript : null;
|
||||
|
||||
if (options.debug && !pageName) {
|
||||
onDebug("[gatsby-plugin-vercel]", "Unable to detect Page Name, skipping reporting.");
|
||||
}
|
||||
|
||||
var body = {
|
||||
dsn: options.analyticsId,
|
||||
id: metric.id,
|
||||
page: pageName !== null && pageName !== void 0 ? pageName : "",
|
||||
href: location.href,
|
||||
event_name: metric.name,
|
||||
value: metric.value.toString(),
|
||||
speed: "connection" in navigator && navigator["connection"] && "effectiveType" in navigator["connection"] ? navigator["connection"]["effectiveType"] : ""
|
||||
};
|
||||
|
||||
if (options.debug) {
|
||||
onDebug(metric.name, JSON.stringify(body, null, 2));
|
||||
}
|
||||
|
||||
var blob = new Blob([new URLSearchParams(body).toString()], {
|
||||
// This content type is necessary for `sendBeacon`:
|
||||
type: "application/x-www-form-urlencoded"
|
||||
});
|
||||
var vitalsUrl = "https://vitals.vercel-analytics.com/v1/vitals";
|
||||
navigator.sendBeacon && navigator.sendBeacon(vitalsUrl, blob) || fetch(vitalsUrl, {
|
||||
body: blob,
|
||||
method: "POST",
|
||||
credentials: "omit",
|
||||
keepalive: true
|
||||
});
|
||||
}
|
||||
|
||||
function webVitals(_x) {
|
||||
return _webVitals.apply(this, arguments);
|
||||
}
|
||||
|
||||
function _webVitals() {
|
||||
_webVitals = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
|
||||
var options;
|
||||
return _regenerator.default.wrap(function _callee$(_context) {
|
||||
while (1) {
|
||||
switch (_context.prev = _context.next) {
|
||||
case 0:
|
||||
options = _ref.options;
|
||||
|
||||
if (!isRegistered) {
|
||||
_context.next = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
return _context.abrupt("return");
|
||||
|
||||
case 3:
|
||||
isRegistered = true;
|
||||
|
||||
try {
|
||||
(0, _webVitals2.getFID)(function (metric) {
|
||||
return sendToAnalytics(metric, options);
|
||||
});
|
||||
(0, _webVitals2.getTTFB)(function (metric) {
|
||||
return sendToAnalytics(metric, options);
|
||||
});
|
||||
(0, _webVitals2.getLCP)(function (metric) {
|
||||
return sendToAnalytics(metric, options);
|
||||
});
|
||||
(0, _webVitals2.getCLS)(function (metric) {
|
||||
return sendToAnalytics(metric, options);
|
||||
});
|
||||
(0, _webVitals2.getFCP)(function (metric) {
|
||||
return sendToAnalytics(metric, options);
|
||||
});
|
||||
} catch (err) {
|
||||
onError(err);
|
||||
}
|
||||
|
||||
case 5:
|
||||
case "end":
|
||||
return _context.stop();
|
||||
}
|
||||
}
|
||||
}, _callee);
|
||||
}));
|
||||
return _webVitals.apply(this, arguments);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
17
yarn.lock
17
yarn.lock
@@ -614,6 +614,13 @@
|
||||
"@babel/helper-simple-access" "^7.10.1"
|
||||
babel-plugin-dynamic-import-node "^2.3.3"
|
||||
|
||||
"@babel/runtime@7.12.1":
|
||||
version "7.12.1"
|
||||
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.1.tgz#b4116a6b6711d010b2dad3b7b6e43bf1b9954740"
|
||||
integrity sha512-J5AIf3vPj3UwXaAzb5j1xM4WAQDX3EMgemF8rjCP3SoW09LfRKAXQKt6CoVYl230P6iWdRcBbnLDDdnqWxZSCA==
|
||||
dependencies:
|
||||
regenerator-runtime "^0.13.4"
|
||||
|
||||
"@babel/template@^7.10.1", "@babel/template@^7.4.4":
|
||||
version "7.10.1"
|
||||
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.1.tgz#e167154a94cb5f14b28dc58f5356d2162f539811"
|
||||
@@ -11543,6 +11550,11 @@ regenerate@^1.4.0:
|
||||
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.1.tgz#cad92ad8e6b591773485fbe05a485caf4f457e6f"
|
||||
integrity sha512-j2+C8+NtXQgEKWk49MMP5P/u2GhnahTtVkRIHr5R5lVRlbKvmQ+oS+A5aLKWp2ma5VkT8sh6v+v4hbH0YHR66A==
|
||||
|
||||
regenerator-runtime@^0.13.4:
|
||||
version "0.13.11"
|
||||
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
|
||||
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
|
||||
|
||||
regex-not@^1.0.0, regex-not@^1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
|
||||
@@ -13596,6 +13608,11 @@ wcwidth@^1.0.0, wcwidth@^1.0.1:
|
||||
dependencies:
|
||||
defaults "^1.0.3"
|
||||
|
||||
web-vitals@0.2.4:
|
||||
version "0.2.4"
|
||||
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-0.2.4.tgz#ec3df43c834a207fd7cdefd732b2987896e08511"
|
||||
integrity sha512-6BjspCO9VriYy12z356nL6JBS0GYeEcA457YyRzD+dD6XYCQ75NKhcOHUMHentOE7OcVCIXXDvOm0jKFfQG2Gg==
|
||||
|
||||
webidl-conversions@^3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
|
||||
|
||||
Reference in New Issue
Block a user