Skip to content

Instantly share code, notes, and snippets.

@janit
janit / sw.js
Last active October 31, 2019 11:26
Service Worker example for controlling what is cached - See https://malloc.fi/fix-duplicate-redirects-service-worker-caching
self.addEventListener('install', function (event) {
event.waitUntil(preLoad());
});
var preLoad = function () {
// console.log('[PWA Builder] Install Event processing');
return caches.open('pwabuilder-offline').then(function (cache) {
// console.log('[PWA Builder] Cached index and offline page during Install');
return cache.addAll(['/offline.html', '/']);
});
@ryanoglesby08
ryanoglesby08 / server.js
Last active April 4, 2024 13:25
A node.js SPA server that serves static files and an index.html file for all other routes.
/*
Incredibly simple Node.js and Express application server for serving static assets.
DON'T USE THIS IN PRODUCTION!
It is meant for learning purposes only. This server is not optimized for performance,
and is missing key features such as error pages, compression, and caching.
For production, I recommend using an application framework that supports server-side rendering,
such as Next.js. https://nextjs.org
@javilobo8
javilobo8 / download-file.js
Last active April 9, 2024 12:01
Download files with AJAX (axios)
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
@romach
romach / concatenate-strings-in-thymeleaf.html
Created April 22, 2017 14:25
Concatenate strings in Thymeleaf
<p th:text="${bean.field + '!' + bean.field}">Static content</p>
th:text="'static part' + ${bean.field}"
th:text="${'static part' + bean.field}"
@yajra
yajra / axios-401-response-interceptor.js
Last active September 20, 2023 06:24
Axios 401 response interceptor.
// Add a 401 response interceptor
window.axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (401 === error.response.status) {
swal({
title: "Session Expired",
text: "Your session has expired. Would you like to be redirected to the login page?",
type: "warning",
showCancelButton: true,
@3zzy
3zzy / curl_vs_getheaders.php
Created December 23, 2016 03:23
cURL vs get_headers() Speed Test
<?php
// Get headers for 100 unique random domains with get_headers() and cURL to determine which is faster.
/*
TL;DR -> cURL is significantly faster.
get_headers (GET) vs cURL:
Execution time : 139.95884609222 seconds
Execution time : 65.998840093613 seconds

FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.


Effective Engineer - Notes

What's an Effective Engineer?

@saeedseyfi
saeedseyfi / direction.scss
Created September 2, 2016 11:43
RTL/LTR support using sass mixins.
$layout-direction: ltr !default;
@mixin ltr {
@if $layout-direction == ltr {
@content;
}
}
@mixin rtl {
@if $layout-direction == rtl {
@content;
@matheusagcosta
matheusagcosta / podbot.md
Last active December 25, 2022 11:58
Podbot Counter Strike 1.6 on Mac OSX

1 - Make sure you have Counter Strike installed

2 - Download YAPB http://forums.bots-united.com/showthread.php?t=9986

3 - Extract the zip and move the folder addons with all the content to ~/Library/Application Support/Steam/steamapps/common/Half-Life/cstrike

4 - After that, open the file liblist.gam in the folder above and replace gamedll_osx "dlls/cs.dylib" with this content:

//gamedll_osx "dlls/cs.dylib"
@Rich-Harris
Rich-Harris / service-workers.md
Last active April 21, 2024 16:24
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.

I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.

Use Canary for development instead of Chrome stable

Chrome 51 has some pretty wild behaviour related to console.log in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.