Skip to content

Instantly share code, notes, and snippets.

@timkelty
timkelty / _category.twig
Last active May 27, 2025 20:26
Craft CMS element cache tags
{#
Tags for /blog/category/foo
--------------------
element
element::craft\elements\Category
element::23 (category: Foo)
craft\elements\GlobalSet
element::craft\elements\GlobalSet::*
element::craft\elements\Entry
element::craft\elements\Entry::*
export default {
async fetch(request): Promise<Response> {
const originRequest = new Request('https://myproxy.com', request);
const originResponse = await fetch(originRequest, {
cf: {
cacheEverything: true,
},
});
// cacheTag will be null, even though the response from the origin server sent it
<link rel="apple-touch-icon" sizes="180x180" href="{{ cloud.artifactUrl('apple-touch-icon.png') }}">
<div style="background-image: {{ cloud.artifactUrl('images/bottombg.svg') }}; width: 100%; height: 30px; background-size: cover;"> </div>

Alternatively, you can use the @artifactBaseUrl alias in a url function:

{{ url('@artifactBaseUrl/images/bottombg.svg') }}
@timkelty
timkelty / Module.php
Created August 10, 2024 20:14
Craft 5 afterSave element bug
<?php
namespace modules\testmodule;
use Craft;
use craft\base\Element;
use craft\base\Model;
use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
use craft\events\ModelEvent;
@timkelty
timkelty / twigpack.php
Created June 12, 2024 18:59
Craft Cloud + Laravel Mix + Twigpack
<?php
use craft\helpers\App;
return [
// Global settings
'*' => [
// If `devMode` is on, use webpack-dev-server to all for HMR (hot module reloading)
'useDevServer' => false,
// Enforce Absolute URLs on includes
@timkelty
timkelty / app.php
Last active May 9, 2024 01:50
Craft 5 Logging Example
<?php
use craft\helpers\App;
return [
'id' => App::env('CRAFT_APP_ID') ?: 'CraftCMS',
'components' => [
'log' => [
'targets' => [
function() {
const original = new URL('https://foo.com/boo/bar/baz/boo?qux');
const kvData = {};
kvData.redirects = [
{
// expected result: https://foo.com/boo/baz/boo
from: '^/(foo|boo)/bar/(.*)',
to: '/$1/$2',
status: 301,
pathOnly: true,
@timkelty
timkelty / routes.php
Last active March 5, 2024 19:15
Redirect wp-content to Cloud assets
<?php
/**
* Site URL Rules
*
* You can define custom site URL rules here, which Craft will check in addition
* to routes defined in Settings → Routes.
*
* Read all about Craft’s routing behavior, here:
* https://craftcms.com/docs/4.x/routing.html
*/
@timkelty
timkelty / docker-compose.local.yaml
Created December 13, 2023 16:36
Bref service for DDEV
services:
bref:
build:
context: bref
image: bref/php-82-fpm-dev:2
container_name: ddev-${DDEV_SITENAME}-bref
expose:
- "8000"
volumes:
- ${DDEV_APPROOT}:/var/task:ro
import {httpbis} from 'http-message-signatures';
function createMyVerifier() {
return {
id: 'test-key',
algs: ['hmac-sha256'],
async verify(data, signature, parameters) {
const keyData = new TextEncoder().encode('123456789');
const algorithm = { name: 'HMAC', hash: 'SHA-256' };
const key = await crypto.subtle.importKey('raw', keyData, algorithm, false, ['verify']);