Skip to content

Instantly share code, notes, and snippets.

View mtorre4580's full-sized avatar
🏠
Working from home

Matias Daniel Torre mtorre4580

🏠
Working from home
View GitHub Profile
@mtorre4580
mtorre4580 / bundlesize.config.json
Last active March 8, 2023 01:37
Example bundle-size budget config for Next.js
{
"files": [
{
"path": ".next/static/**/main-*.js",
"maxSize": "30 kB",
"compression": "brotli"
},
{
"path": ".next/static/chunks/pages/my-page-*.js",
"maxSize": "8 kB",
@mtorre4580
mtorre4580 / lighthouserc.js
Last active March 8, 2023 01:36
Example configuration for lighthouse budget for CI
// Budget to check the mininum in each step
const PERFORMANCE = 0.8;
const ACCESSIBILITY = 0.8;
const BEST_PRACTICES = 0.8;
const SEO = 0.8;
// Extra headers
const headers = {};
// URLs to check
@mtorre4580
mtorre4580 / next.config.js
Last active March 8, 2023 01:36
Example to add external react and react-dom library in Next.js using CDN
module.exports = {
webpack: (config, options) => {
const { isServer, webpack } = options;
if (isServer) {
// Use the react from node_modules
} else {
config.externals = {
react: 'React',
'react-dom': 'ReactDOM',
};
@mtorre4580
mtorre4580 / prefetch.js
Last active March 8, 2023 01:35
Prefetch resources example via xhr
var resources = [
'https://unpkg.com/react@18/umd/react.production.min.js',
'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js',
];
/** Apply prefetch for subsequences */
function prefetchResources(resources) {
for (var i = 0; i < resources.length; i++) {
var xhrRequest = new XMLHttpRequest();
xhrRequest.open('GET', resources[i], true);
@mtorre4580
mtorre4580 / useVisibilityChange.jsx
Last active March 8, 2023 01:35
Hook to check the current visibility of the page using the API Page Visibility
import { useEffect, useState } from "react";
/**
* Hook to check the current visibility of the page
* Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
* @returns boolean
*/
const useVisibilityChange = () => {
const [isVisible, setIsVisible] = useState(true);
@mtorre4580
mtorre4580 / matrix.js
Created March 8, 2023 01:30
Helper to create a matrix with rows, columns and initial value
function matrix(rows, cols, initial = 0) {
const arr = [];
for (let i = 0; i < rows; i++) {
arr[i] = Array.from(new Array(cols)).fill(initial);
}
return arr;
}
@mtorre4580
mtorre4580 / app.js
Last active April 9, 2023 17:01
Example handle errors in listener and process in Node.js with express.js
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
// Replace with your logger
const logger = console;
// Endpoint for health check API
@mtorre4580
mtorre4580 / http-client.js
Created April 9, 2023 02:32
Example http-client with circuit breaker support and keep alive connection with axios in Node.js
const axios = require("axios");
const Agent = require("agentkeepalive");
const CircuitBreaker = require("opossum");
// Replace with your configs
const TIMEOUT_DEFAULT = 2500;
const MAX_SOCKETS = 100;
const MAX_FREE_SOCKET = 10;
const TIME_OUT_KEEP_ALIVE = 60000;
const FREE_SOCKET_TIMEOUT = 30000;
@mtorre4580
mtorre4580 / error-handler.js
Last active April 9, 2023 02:44
Example error handler in node.js for APIs
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
// Replace with your logger
const logger = console;
/**
@mtorre4580
mtorre4580 / cluster_app.js
Last active April 9, 2023 16:48
Example clustering mode in Node.js
const express = require("express");
const cluster = require("cluster");
const os = require("os");
const PORT = process.env.PORT || 3001;
// Replace with your logger
const logger = console;
// If the current is master, create the fork for all the cpus