Skip to content

Instantly share code, notes, and snippets.

View xjamundx's full-sized avatar

Jamund Ferguson xjamundx

View GitHub Profile

Dealing with some serious issues regarding dev/build mismatch in gatsby and finally found the reason fo rit. It revolves around wrapRootElement and replaceComponentRenderer being treated differently during the hydration process. Look what happens when you return this:

gatsby-ssr.js

export const wrapRootElement = () => {
  return <div className="wrapper">text</div>
}
@xjamundx
xjamundx / gatsby-source-properties.js
Last active September 26, 2018 20:24
gatsby properties source plugin
// i am expecting this to generate an allPropertiesFile thing in graphqil
// but it doesn't seem to be reflected in there at all
exports.sourceNodes = async ({ actions, createNodeId }) => {
const { createNode } = actions
// sample data will actually come from files.
const data = {
'myfolder/file2': { text: 'hi', buttonThing: 'hey' },
'myfolder/file1': { header: 'hi', footer: 'bye' },

This seems fine

code:

// @flow
const data = document.body.getAttribute('my-attribute') || '{}'
const result = JSON.parse(data)
document.body.removeAttribute('my-attribute')
// idea 1 - use JOI (or similar) at the router level
let params = {
query: {
limit: Joi.number().integer().min(1).max(100).default(10)
}
}
server.get("/horse", enforceParams(params), controller.myRoute)
// idea 2 - use JOI (or similar) at the controller level
async function myRoute(req, res) {
function handleRequest(req, res) {
// all the important request logic...
// send back the request id
res.send(requestId)
// if sending to an known sender...
if (senderId) {
await optInToExperiment(senderId)
}
// no types
function getFields(fields) {
return fields.reduce((prev, curr) => (prev[curr] = curr, prev), {})
}
// no return type
function getFields(fields: Array<string>) {
return fields.reduce((prev, curr) => (prev[curr] = curr, prev), {})
}
@xjamundx
xjamundx / webpack-unused-files.sh
Last active November 20, 2020 12:51
Quickly identify files unused by webpack
# ----------------------------------- #
webpack --display-modules | awk '{print $2}' | grep ^\.\/ > files-processed.txt;
cd public/js/; # assumes all your files are here
find . -name "*.js" | grep -v eslint | grep -v __ > ../../files-all.txt; # excludes __tests__ and .eslintrc files
cd ../..;
cat files-all.txt | xargs -I '{}' sh -c "grep -q '{}' files-processed.txt || echo '{}'";
rm files-processed.txt files-all.txt;
# ----------------------------------- #
@xjamundx
xjamundx / promise-anti-patterns.md
Last active March 4, 2021 17:49
Promise Anti-Patterns

Trying Flow on my node.js code…Let’s see how this goes

Finding the getting started guide:

  1. google flow
  2. click “getflow.com”
  3. was something else
  4. google flow javascript
  5. found flowtype.org
  6. Click “get started” and landed here https://flowtype.org/docs/getting-started.html#_
// catch any errors in the promise and either forward them to next(err) or ignore them
const catchErrors = fn => (req, res, next) => fn(req, res, next).catch(next)
const ignoreErrors = fn => (req, res, next) => fn(req, res, next).catch(() => next())
// wrap my routes in those helpers functions to get error handling
app.get('/sendMoney/:amount', catchErrors(sendMoney))
// use our ignoreErrors helper to ignore any errors in the logging middleware
app.get('/doSomethingElse', ignoreErrors(logSomething), doSomethingElse)