Skip to content

Instantly share code, notes, and snippets.

View hannesl's full-sized avatar

Hannes Lilljequist hannesl

View GitHub Profile
@hannesl
hannesl / _preview.tsx
Last active February 25, 2022 00:03
An alternative, cookie-less solutions for previewing CMS content in Next.js.
/**
* @file Supports displaying previews of pages.
*
* Should be queried like this: https://my-site/_preview/?slug=my-page&preview_key={MY_SECRET_KEY}
*/
import { GetServerSideProps, GetStaticProps, NextPage } from "next";
import _ from "lodash";
import { ParsedUrlQuery } from "querystring";
@hannesl
hannesl / autocomplete.vue
Last active May 4, 2018 11:32
A few fixes to the Vue.js autocomplete component published here: https://alligator.io/vuejs/vue-a11y-autocomplete/
<script>
export default {
name: 'autocomplete',
props: {
items: {
type: Array,
required: false,
default: () => [],
},
isAsync: {
@hannesl
hannesl / gtm-ga-virtual-page-load.js
Created April 4, 2018 09:54
Trigger page load events in Google Analytics with JavaScript when using Google Tag Manager
if (typeof ga === "function") {
ga.getAll().forEach((tracker) => {
tracker.set('page', '/my/path'); // <- change here
tracker.send('pageview');
});
}
// ES5 version for backward compatibility
if (typeof ga === "function") {
ga.getAll().forEach(function (tracker) {
@hannesl
hannesl / detect-browser-skills.js
Created January 25, 2017 22:32
Detect browser capabilities reliably with JS
document.addEventListener("DOMContentLoaded", function () {
var touchDetect = function () {
document.documentElement.classList.add("has-touch");
document.removeEventListener("touchstart", touchDetect);
};
var hoverDetect = function () {
document.documentElement.classList.add("has-hover");
document.removeEventListener("mouseenter", hoverDetect);
};
@hannesl
hannesl / my_controller_test.rb
Created September 8, 2016 10:50
Testing http basic authentication in Rails 5
class MyControllerTest < ActionDispatch::IntegrationTest
test "authentication" do
params = {test: "object"}
auth_headers = {"Authorization" => "Basic #{Base64.encode64('test:test')}"}
post '/my-controller', params: params, as: :json
assert_response 401
post '/my-controller', params: params, as: :json, headers: auth_headers
assert_response :success
end
@hannesl
hannesl / php.json
Created August 28, 2016 20:19
CleverCloud: Insert environment variables in declarative config files at deploy
{
...
"hooks": {
"postDeploy": "./clevercloud/post-deploy.sh"
},
...
}
@hannesl
hannesl / Extension.php
Last active October 19, 2015 14:58
Bolt: Workaround for issue with missing templates
<?php
// See https://github.com/bolt/bolt/issues/4290
namespace Bolt\Extension\Stoco\ConfigCacheFix;
use Bolt\Application;
use Bolt\BaseExtension;
use Symfony\Component\HttpFoundation\Request;
@hannesl
hannesl / gist:22523ac60a6a5f632115
Created April 9, 2015 10:56
Another take on opening external links in a new window.
$(function() {
// Get the current domain name without any sub domains.
var internalDomain = (function () {
var domainParts = [],
hostnameParts = location.hostname.split("."),
i;
if (hostnameParts.length == 1) {
return location.hostname;
@hannesl
hannesl / field.tpl.php
Created March 4, 2015 21:02
Drupal: bring some sanity to the field markup.
<?php if (!empty($items) && !empty($items[0]) && !empty($items[0]['#markup'])): ?>
<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if ($multiple): ?>
<?php if (!$label_hidden && $label) : ?>
<h3 class="field-label"<?php print $title_attributes; ?>><?php print $label ?></h3>
<?php endif; ?>
<ul class="field-items">
<?php foreach ($items as $item) : ?>
<li class="field-item"><?php print render($item); ?></li>
@hannesl
hannesl / close_comments.php
Created January 27, 2015 09:42
Drupal: Close comments after a specified time.
<?php
$breakpoint = time() - 3 * 30 * 24 * 60 * 60;
$q = db_select('node');
$q->addField('node', 'nid');
$q->addField('node', 'vid');
$q->condition('comment', 2);
$q->condition('type', 'article');
$q->condition('created', $breakpoint, '<');