Skip to content

Instantly share code, notes, and snippets.

View jpvincent's full-sized avatar

Jean-Pierre Vincent jpvincent

View GitHub Profile
// idée originale : https://developers.google.com/web/fundamentals/performance/user-centric-performance-metrics#load_abandonment
window.__trackAbandons = () => {
// Remove the listener so it only runs once.
document.removeEventListener('visibilitychange', window.__trackAbandons);
const ANALYTICS_URL = 'https://www.google-analytics.com/collect';
const GA_COOKIE = document.cookie.replace(
/(?:(?:^|.*;)\s*_ga\s*\=\s*(?:\w+\.\d\.)([^;]*).*$)|^.*$/, '$1');
const TRACKING_ID = 'UA-XXXXX-Y';
const CLIENT_ID = GA_COOKIE || (Math.random() * Math.pow(2, 52));
const timings = performance.getEntriesByType('navigation')[0]
const allEntries = performance.getEntries()
const customMetrics = allEntries
.filter(entry => ['mark', 'measure'].includes(entry.entryType )) // array of PerformanceMark / PerformanceMeasure objects
.reduce((final, current) => { // make an object
final[current.entryType.toUpperCase() + ' — ' + current.name] // key name : "MARK — loadInAsync que/query_grid_webpart start"
= Math.round(
current.entryType === 'measure' ? current.duration : current.startTime
)
@jpvincent
jpvincent / lazy_modal_svelte.jsx
Created October 24, 2023 09:01
Lazy loading svelte component
<script>
let lazyModalComponent;
let isPending = false;
function openModal() {
isPending = true;
lazyModalComponent = import(`./Modal.svelte`);
lazyModalComponent.catch((error) => {
// gestion en cas d'erreur de connexion ici
}).finally(() => {
@jpvincent
jpvincent / lazy_modal_react.jsx
Created October 24, 2023 09:02
lazy load react component
import { lazy, useState, useTransition } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
const Modal = lazy(() => import('./Modal.jsx'));
export default function App() {
const [isPending, startTransition] = useTransition();
const [opened, setOpened] = useState(false);
function openModal() {