Skip to content

Instantly share code, notes, and snippets.

View developit's full-sized avatar
🦊
write, the codes

Jason Miller developit

🦊
write, the codes
View GitHub Profile

Modern Script Loading

Loading modern code for modern browsers while still supporting older browsers should be possible via module/nomodule:

<script type="module" src="/modern.js"></script>
<script nomodule src="/legacy.js"></script>

... however this results in over-fetching of scripts in Edge and Safari.

export function Promise(initiator) {
var value;
var error;
var state = 0;
initiator(v => {
value = v;
state = 1;
q.forEach(this._with);
}, e => {
error = e;
@developit
developit / nextjs-local-development.md
Last active November 7, 2019 03:26
Test a local copy of Next.js against apps

Given a directory structure like the following:

📂projects
  📂next    <-- your Next.js checkout
  📂demo-app

Here's how to build your local copy of Next.js. You'll want to do this every time you make a change to Next itself.

/** Converts destructured parameters with default values to non-shorthand syntax, fixing Edge 16 & 17. */
module.exports = (babel, options = {}) => {
const { types: t } = babel;
const isArrowParent = p => p.parentKey=='params' && p.parentPath && p.parentPath.isArrowFunctionExpression();
return {
name: "transform-edge-default-parameters",
visitor: {
AssignmentPattern(path) {
export default code => code.replace(/(?:[\s\t]+\/\*[\s\S]*?\*\/[\s\t]*|\s*(\n)\s*\/\/.*(?:\n\s*\/\/.*)*)/g, '$1')
# scaffold your project
npx preact-cli@rc create default my-app && cd my-app
# upgrade to Preact X
npm i -D preact-cli@rc && npm i preact@latest preact-router@latest preact-render-to-string@latest
npm rm preact-compat
const IS_SERVER = typeof window === 'undefined';
export function RenderOnServer(props) {
return IS_SERVER ? props.children : null;
}
export function RenderOnClient(props) {
return IS_SERVER ? null : return props.children;
}
<!DOCTYPE html>
<html>
<head>
<title>Demo: Treat Safari 11 and Prior as nomodule</title>
</head>
<body>
<script>
if (/Safari\/5/.test(navigator.userAgent)) {
var $fss = function () {
for (var i of document.scripts) {
@developit
developit / package.json
Created May 30, 2019 15:19
Support for string styles in Preact X: <div style="color: red;">
{
"name": "preact-string-styles",
"main": "preact-string-styles.js",
"module": "preact-string-styles.js",
"version": "0.1.0",
"homepage": "https://gist.github.com/developit/d55dc3c47dd1fe81d84f5ee0ab5bfc58"
}
@developit
developit / babel-plugin-optimize-tagged-templates.js
Created May 29, 2019 20:04
Babel plugin to optimize tagged templates by inlining string/number/boolean values.
module.exports = (babel, options = {}) => {
const { types: t } = babel;
return {
name: "optimize-tagged-templates",
visitor: {
TemplateLiteral(path) {
for (let i=0; i<path.node.expressions.length; i++) {
const expr = path.node.expressions[i];
let isLiteral = t.isStringLiteral(expr);