Skip to content

Instantly share code, notes, and snippets.

View viduraperera's full-sized avatar

Vidura Umayanga Perera viduraperera

View GitHub Profile
npm i firebase-dynamic-links
const updateServiceWorker = () => {
const registrationWaiting = serviceWorkerRegistration.waiting;
if (registrationWaiting) {
registrationWaiting.postMessage({ type: 'SKIP_WAITING' });
registrationWaiting.addEventListener('statechange', e => {
if (e.target.state === 'activated') {
window.location.reload();
}
const initialState = {
serviceWorkerInitialized: false,
serviceWorkerUpdated: false,
serviceWorkerRegistration: null,
}
const isServiceWorkerInitialized = useSelector(
state => state.serviceWorkerInitialized,
);
import { createStore } from 'redux';
import rootReducer from './reducers';
function configureStore() {
return createStore(rootReducer, );
}
export default configureStore;
export const SW_INIT = 'SW_INIT';
export const SW_UPDATE = 'SW_UPDATE';
serviceWorkerRegistration.register({
onSuccess: () => store.dispatch({ type: SW_INIT }),
onUpdate: reg => store.dispatch({ type: SW_UPDATE, payload: reg }),
});
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
@viduraperera
viduraperera / browser-image-compression-download-image
Created November 22, 2021 08:05
browser-image-compression-download-image
<div className="col-xl-4 col-lg-4 col-md-12 col-sm-12 mt-3">
<Card.Img variant="top" src={this.state.compressedLink}></Card.Img>
{this.state.clicked ? (
<div className="d-flex justify-content-center">
<a
href={this.state.compressedLink}
download={this.state.outputFileName}
className="mt-2 btn btn-dark w-75"
>
Download
@viduraperera
viduraperera / browser-image-compression-click-function
Created November 22, 2021 07:24
browser-image-compression-click-function
click = async e => {
const ORIGINAL_IMAGE = this.state.originalImage;
e.preventDefault();
const imageSize = (ORIGINAL_IMAGE.size / (1024 * 1024)).toFixed(1);
const maxSize = 150 / 1024; // max size allowed
const minSize = 100 / 1024; // min size possible (after adjustment)