Skip to content

Instantly share code, notes, and snippets.

// Private property, cannot from the importing file
let bar = 'baz'
export default class Foo {
constructor () {
console.log(bar)
}
}
@trezy
trezy / Head.jsx
Last active February 28, 2017 16:26
Handling Google Analytics with Next.js
import React from 'react'
export default Head extends React.Component {
componentWillMount () {
ga.push(location.pathname) // This probably isn't quite right, but you get the idea.
}
render () {
<div>
<script dangerouslySetInnerHTML={{__html: 'GA Snippet'}} />
// Component imports
import i18next from './i18next'
export default class extends React.Component {
constructor (props) {
super(props)
'use strict'
/******************************************************************************\
Module imports
\******************************************************************************/
const { URL } = require('url')
const cookie = require('koa-cookie')
const next = require('next')
const path = require('path')
@trezy
trezy / Page.jsx
Last active September 14, 2017 17:47
// Module imports
import { bindActionCreators } from 'redux'
import {
connect,
Provider,
} from 'react-redux'
import React from 'react'
@trezy
trezy / Page.jsx
Last active September 14, 2017 20:37
// Module imports
import React from 'react'
import { bindActionCreators } from 'redux'
import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper'
@trezy
trezy / Head.jsx
Last active October 10, 2019 02:54
import NextHead from 'next/head'
import React from 'react'
import ReactGA from 'react-ga'
import Router from 'next/router'
/*****************************************************************************\
function etag(options) {
return function etag(ctx, next) {
return next()
// `getResponseEntity` checks to see if the response has a body, and
// stringifies it depending on what kind of body it has (JSON, HTML,
// image, etc)
.then(() => getResponseEntity(ctx))
// `setEtag` calculates the ETag from the response body if it exists,
// then sets that ETag as a header
function conditional() {
return function conditional(ctx, next) {
return next().then(() => {
if (ctx.fresh) {
ctx.status = 304;
ctx.body = null;
}
});
}
}
// `ctx` is an object that Koa.js passes through its middleware, allowing the
// middleware to modify it as they see fit. `ctx.request` and `ctx.response` are
// objects that reflect the contents of the HTTP request and response,
// respectively
var oldEtag = ctx.request.headers['if-none-match']
var newEtag = ctx.response.headers['etag']
ctx.fresh = (oldEtag !== newEtag)