Skip to content

Instantly share code, notes, and snippets.

View kinngh's full-sized avatar
Keep Your Friends Close and Repos Open

Harshdeep Singh Hura kinngh

Keep Your Friends Close and Repos Open
View GitHub Profile
@kinngh
kinngh / screw-your-optimization.js
Last active April 17, 2025 16:11
Load your JS despite "Speed Optimization Apps" monkey patching basic document events
//Previous
document.addEventListener("DOMContentLoaded", async function () => {});
//Patched
/**
*
* First, pull your callback into an independent function
* Here we call it `boot` since that's what I wanna shove up
* "optimization" apps' butts.
*
@kinngh
kinngh / PolarisViz.js
Created January 29, 2025 19:15
Import PolarisViz client side in Next.js pages router so it works as expected with proper exports.
import dynamic from "next/dynamic";
const dynamicPolarisVizImport = (componentName) => {
return dynamic(
() =>
import(
`@shopify/polaris-viz/build/esm/components/${componentName}/${componentName}.js`
).then((mod) => mod[componentName]),
{
loading: () => <></>,
ssr: false,
@kinngh
kinngh / index.html
Created December 31, 2024 11:32
Make a `fetch` even if it's being overwritten
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Overwritten Fetch Example</title>
</head>
<body>
<script>
// Overwrite fetch to emulate a broken or hijacked fetch
function fetch() {
@kinngh
kinngh / unit-converter.liquid
Created December 11, 2024 12:29
Convert given measurement into current locale's unit
{% comment %}
Measurement translation based on locale
- US/UK/Myanmar: Imperial (inches, feet)
- Rest of world: Metric (cm, m)
{% endcomment %}
{% assign current_measurement = "25 inches" %}
{% assign measurement = current_measurement | downcase | strip %}
{% assign locale = request.locale.iso_code | downcase %}
@kinngh
kinngh / reroute.jsx
Created November 24, 2024 15:23
Enter a Shopify store without manually typing in password
import { useEffect } from "react";
const ShopifyRedirect = () => {
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const store = params.get("store");
const password = params.get("password");
if (!store) {
alert("Store param is missing");
@kinngh
kinngh / pull-url.js
Created November 6, 2024 11:13
Pull passwordless demo store URL
async function getHrefFromUrl(url) {
try {
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
Connection: "keep-alive",
@kinngh
kinngh / .env.example
Last active November 3, 2024 20:00
Starting MySQL server locally
DATABASE_URL="mysql://root:@localhost:3306/shopify-app"
@kinngh
kinngh / usePrefetchRoutes.js
Created March 20, 2024 21:44
Prefetch Next.js routes to speed up navigation
import { useRouter } from "next/router";
import { useEffect } from "react";
/**
* Prefetch routes to make navigation between pages faster
*
* @param {Array<string>} paths An array of paths (URLs) to prefetch.
*/
const usePrefetchRoutes = (paths) => {
const router = useRouter();
@kinngh
kinngh / rich-text-parser.liquid
Last active February 26, 2024 17:43
Packing slips doesn't support rich text fields via the `| metafield_tag` just yet, this parses the rich text JSON into usable HTML
{% assign parsed_content = order.metafields.namespace.key | json_parse %}
{% for item in parsed_content.children %}
{% case item.type %}
{% when 'paragraph' %}
<p>
{% for child in item.children %}
{% if child.bold and child.italic %}
<strong><em>{{ child.value }}</em></strong>
{% elsif child.bold %}
@kinngh
kinngh / Modal.jsx
Last active February 20, 2024 01:37
AppBridge CDN Modal Wrapper
import { Box } from "@shopify/polaris";
import { useEffect, useRef } from "react";
/**
* @typedef {Object} Action
* @property {string} content
* @property {'critical'} [tone]
* @property {() => void} onAction
*/