Skip to content

Instantly share code, notes, and snippets.

View Alimjanov-Ibragim's full-sized avatar
🎯
Focusing

Ibragim Alimjanov-Ibragim

🎯
Focusing
View GitHub Profile
@gaearon
gaearon / like_button.js
Last active November 19, 2020 06:02
JSX version of LikeButton
'use strict';
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
@KamaMakh
KamaMakh / base64Methods.js
Created December 10, 2020 10:35
Methods for base64
{
toBase64: file => new Promise((resolve, reject) => {
if (file) {
const reader = new FileReader()
reader.readAsDataURL(file.raw)
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
} else {
reject()
}
@KamaMakh
KamaMakh / axiosTemp.js
Last active December 10, 2020 11:18
axios template
let axios = require("axios")
let axiosInstance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
transformRequest: [(data, headers) => {
headers["Accept-Language"] = "en";
if (/* token */) {
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
@felipeskroski-zz
felipeskroski-zz / degToCard.js
Last active February 24, 2022 20:27
Javascript function to convert wind direction in degrees to cardinal.
var degToCard = function(deg){
if (deg>11.25 && deg<=33.75){
return "NNE";
}else if (deg>33.75 && deg<=56.25){
return "ENE";
}else if (deg>56.25 && deg<=78.75){
return "E";
}else if (deg>78.75 && deg<=101.25){
return "ESE";
}else if (deg>101.25 && deg<=123.75){
@KamaMakh
KamaMakh / UA parser
Created June 24, 2022 05:22
Detect Browser, Engine, OS, CPU, and Device
export const isSafari = () => {
const ua = window.navigator.userAgent.toLowerCase();
return (
ua.includes('safari') &&
!ua.includes('crios') &&
!ua.includes('fxios') &&
!ua.includes('android')
);
};

How to force a page to reload from server at browser launch with JavaScript

I spent weeks finding a way to refresh a page or a tab from the server once a user starts or restarts the browser. I was very surprised to find nothing about this topic.

Let's dig into the context, tries and the solution I came up with.

Context

Typically, once a user opens a browser and gets to a page, it directly provides the page in cache without refreshing any data from the server. Obviously, this only applies if the user is not in a private navigation.

In this case, as a user, I always need to open the browser and then manually reload the page to get the last updated data. What? Am I too lazy? Sure I am a developer!

@ali-sabry
ali-sabry / useMediaQuery.js
Created March 26, 2023 13:34
This custom hook will allow me to easily check if the current screen size matches a specific media query (e.g., desktop or mobile). By using this hook, I can conditionally render components based on the screen size and optimize my app for different devices.
//=== Custom Hook useMediaQuery
import { useState, useEffect } from 'react';
export const useMediaQuery = (query) => {
const [matches, setMatches] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia(query);
/* window.matchMedia() match if the media query you passed
is match with current media query and return boolean value
@ali-sabry
ali-sabry / useDebounce.js
Created March 25, 2023 20:07
This hook will allow me to debounce any function that I want to run after a certain delay. For example, if I have an input field that triggers a search function on every keystroke, this hook will prevent unnecessary API calls by delaying the search function until the user stops typing for a certain period.
import { useState, useEffect } from 'react';
export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);
@yunusga
yunusga / .htaccess
Last active June 15, 2023 09:46
RewriteRule для WordPress директории uploads. На случай если нет возможности держать файлы загрузок на своем сервере при тестировании.
# за редирект отвечает
# RewriteRule ^wp-content/uploads/(.*)$ https://<production site>/wp-content/uploads/$1 [R=302,NC,L]
# должна быть сразу после RewriteEngine On
# <wp test directory> необходим если сайт лежит в директории основного домена, например site.com/dev
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^wp-content/uploads/(.*)$ https://<production site>/wp-content/uploads/$1 [R=302,NC,L]
# RewriteRule ^wp-content\/uploads\/(?!(2022\/08)\/)(.*)$ https://divina-bellezza.ru/wp-content/uploads/$2 [R=302,NC,L]
RewriteBase /<wp test directory>/
RewriteRule ^index\.php$ - [L]