Skip to content

Instantly share code, notes, and snippets.

@surma
surma / staleWhileRevalidate.js
Last active April 8, 2024 22:38
ServiceWorker that implements “Stale-while-revalidate”
// Implements stale-while-revalidate
self.addEventListener('fetch', event => {
const cached = caches.match(event.request);
const fetched = fetch(event.request);
const fetchedCopy = fetched.then(resp => resp.clone());
// Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache.
// If there’s nothing in cache, wait for the fetch.
// If neither yields a response, return a 404.
@telekosmos
telekosmos / uniq.js
Last active November 15, 2022 17:13
Remove duplicates from js array (ES5/ES6)
var uniqueArray = function(arrArg) {
return arrArg.filter(function(elem, pos,arr) {
return arr.indexOf(elem) == pos;
});
};
var uniqEs6 = (arrArg) => {
return arrArg.filter((elem, pos, arr) => {
return arr.indexOf(elem) == pos;
});
@mattmcmanus
mattmcmanus / Learning.markdown
Last active February 8, 2019 19:03
React Best Practices - Lessons learned after a couple months of using React

React Best Practices - Lessons learned after a couple months of using React

These are some things I've found helpful after doing React for a couple months. It's possible these practices are short sighted and will cause other problems, I just haven't hit those bumps in the road yet.

This list started after reading this phenomincal article on React Tips and Best Practicies.

1. Always define your propTypes. Always.

Think of propTypes as a love letter to whatever developer will work on a component after you. Your components should be as declarative as possible. React's error messaging is so clear and helpful and this will make things so much easier.

@gerardroche
gerardroche / sublime-clean
Last active December 17, 2022 06:15
Clean Sublime Text caches and optionally clean out any sessions
#!/bin/sh
set -e
unset CDPATH
unset IFS
show_usage() {
cat <<USAGE
Usage: [PROJECTS_PATH=<PATH>] $(basename "$0") [--exclude-sessions] [--exclude-workspaces]
@bennadel
bennadel / ng-if-false.htm
Created January 12, 2015 13:42
Creating A Pre-Bootstrap Loading Screen In AngularJS
<!-- BEGIN: Pre-Bootstrap Loading Screen. -->
<div ng-if="false">
<p>
This will show until the application is bootstrapped.
Then, the ngIf directive will immediately rip it out of
the page.
</p>
</div>
@msh9
msh9 / olingoWrapperService.js
Last active August 29, 2015 14:12
Olingo--AngularJS
'use strict';
myApp.factory('olingoWrapperService', ['localStorageService','$location', '$q', '$rootScope', function (localStorageService, $location, $q, $rootScope) {
var _recipeService = {};
var _oldOlingoODataClient = odatajs.oData.net.defaultHttpClient;
var _authSensitiveOlingoClient = {
request: function (request, success, error) {
request.headers = request.headers || {};
var authData = localStorageService.get('authorizationData');
@AbhishekGhosh
AbhishekGhosh / Google-Sitelinks-JSON-LD.php
Created November 3, 2014 17:15
Google-Sitelinks-JSON-LD for AdSense
@iolo
iolo / typeahead.js
Created October 14, 2014 14:02
simple angular directive to support twitter's typeahead.js
angular.module('io.utils', [])
module.directive('ioTypeahead', [
'$parse',
function ($parse) {
return {
restrict: "AE",
replace: true,
transclude: false,
compile: function (element, attrs) {
var modelAccessor = $parse(attrs.ioTypeahead);
@addyosmani
addyosmani / README.md
Last active April 2, 2024 20:18 — forked from 140bytes/LICENSE.txt
108 byte CSS Layout Debugger

CSS Layout Debugger

A tweet-sized debugger for visualizing your CSS layouts. Outlines every DOM element on your page a random (valid) CSS hex color.

One-line version to paste in your DevTools

Use $$ if your browser aliases it:

~ 108 byte version

@rolftimmermans
rolftimmermans / compress-folder-with-tinypng.jsx
Created June 25, 2014 14:24
Compressing all PNG images in a folder and its subfolders with TinyPNG
#target photoshop
/* Open the given file, and compress with TinyPNG. */
function compressFile(file) {
var document = open(file);
if (document.mode == DocumentMode.INDEXEDCOLOR) {
document.changeMode(ChangeMode.RGB);
}