Skip to content

Instantly share code, notes, and snippets.

View amitsingh-007's full-sized avatar
:octocat:
Working from home

Amit Singh amitsingh-007

:octocat:
Working from home
View GitHub Profile
@amitsingh-007
amitsingh-007 / GlobalMetaTags.js
Created July 11, 2021 07:38
Add reference to manifest file
import React, { memo } from "react";
import Helmet from "react-helmet";
/* This should load on every page so that no matter on which page the user lands,
* we fetch the manifest.json file
*/
export const GlobalMetaTags = memo(() => (
<Helmet
titleAttributes={{ itemprop: "name" }}
htmlAttributes={{
@amitsingh-007
amitsingh-007 / manifest.webmanifest.json
Created July 11, 2021 07:33
PWA manifest.json File
{
"short_name": "React Boilerplate App",
"name": "React Boilerplate App",
"description": "A PWA boilerplate/reference application in React",
"icons": [
{
"src": "/vercel-icon-192x192.png",
"type": "image/png",
"sizes": "192x192",
"purpose": "any maskable"
@amitsingh-007
amitsingh-007 / logReactHydrationError.js
Last active December 5, 2022 14:48
Monkey patch console.error() to process React hydration errors
//Update this array to catch more errors
const HYDRATION_ERROR_MSGS = [
"Warning: Did not expect server HTML to contain",
"Warning: Text content did not match. Server",
];
//Can use any npm packag instead of this
const interpolate = (template, params) =>
params.reduce((p, c) => p.replace(/%s/, c), template);
@amitsingh-007
amitsingh-007 / LighthouseAggregator.js
Last active October 20, 2020 13:22
Get mean data from multiple lighthouse reports
const glob = require("glob");
const path = require("path");
const configs = [];
glob.sync("./lh-reports/*.json").forEach(file => {
configs.push(require(path.resolve(file)));
});
const stats = {
FCP: { value: 0, unit: "s" },
@amitsingh-007
amitsingh-007 / BrowserDetection.cs
Last active September 27, 2020 15:53
Detect if request is from modern browser or not
namespace PWAUtils
{
public static class BrowserDetection
{
static readonly Dictionary<string, string> browserMapping = new Dictionary<string, string>
{
//For mobile devices
{ "Chrome Mobile", "71" },
{ "Firefox Mobile", "64" },
{ "Samsung Browser", "8.2" },
@amitsingh-007
amitsingh-007 / modernWebpack.config.js
Last active September 27, 2020 14:50
Modern webpack config for ES6+ code
const path = require("path");
const modernBabelConfig = require("babel.modern.config.js");
module.exports = {
context: path.resolve(__dirname, "src"),
resolve: {
extensions: [".js", ".jsx"],
alias: {...},
modules: [
path.resolve(__dirname, "src"),
@amitsingh-007
amitsingh-007 / legacyWebpack.config.js
Last active September 27, 2020 14:51
Legacy Webpack config for ES5 code
const path = require("path");
const legacyBabelConfig = require("babel.legacy.config.js");
module.exports = {
context: path.resolve(__dirname, "src"),
resolve: {
extensions: [".js", ".jsx"],
alias: {...},
modules: [
path.resolve(__dirname, "src"),
@amitsingh-007
amitsingh-007 / babel.modern.config.js
Last active September 15, 2020 17:59
ES6 supporting browser babel config
const commonPlugins = require("./commonPlugins");
module.exports = api => {
api && api.cache(false);
const presets = [
[
"@babel/preset-env",
{
targets: { esmodules: true }, //For ES6 supporting browsers
useBuiltIns: "usage",
corejs: 2,
@amitsingh-007
amitsingh-007 / babel.legacy.config.js
Last active September 15, 2020 17:54
Legacy babel config
const commonPlugins = require("./commonPlugins");
module.exports = api => {
api && api.cache(false);
const presets = [
[
"@babel/preset-env",
{
targets: { browsers: ["safari >= 7"] }, //To support old browsers
useBuiltIns: "usage",
corejs: 2,
@amitsingh-007
amitsingh-007 / execute.js
Created July 4, 2020 07:03
Execute command to run jest command
/*
* Use spawn instead of exec for large ouput buffer
* https://www.hacksparrow.com/nodejs/difference-between-spawn-and-exec-of-node-js-child-rocess.html
*/
const liveOuput = (command, args) => {
const childProcess = spawn(command, args, { shell: true });
childProcess.stdout.on("data", data => {
console.log(data.toString());
});