Skip to content

Instantly share code, notes, and snippets.

View nibtime's full-sized avatar
💭
Level 6 Laser Lotus

nibtime

💭
Level 6 Laser Lotus
View GitHub Profile
@nibtime
nibtime / utils.ts
Created June 9, 2022 22:05
convenience utils for working with JSON in JS/TS scripting tasks
import { promises, createReadStream } from 'fs';
import StreamArray from 'stream-json/streamers/StreamArray';
export const jsonFromFile = async <T>(filepath: string): Promise<T> => {
return promises
.readFile(filepath, {
encoding: 'utf-8',
})
.then((jsonString) => JSON.parse(jsonString));
};
@nibtime
nibtime / revalidate-pages.function.ts
Created April 20, 2022 18:38
instantly publish DatoCMS pages with Next 12.1 On-Demand ISR
// pages/api/webhooks/datocms/revalidate-pages.function.ts
// custom page extensions in next.config.js: pageExtensions: ['page.tsx', 'function.ts', 'page.jsx', 'function.js'],
import type { NextApiRequest, NextApiResponse } from 'next';
import { i18n } from 'next.config';
import type { Payload } from './types';
import {
getModelId,
getWebhookHeaders,
revalidateMultiplePaths,
@nibtime
nibtime / _document.tsx
Last active July 3, 2022 18:20
strict CSP + reporting with _middleware
// pages/_document.tsx
import Document, {
provideComponents,
} from '@next-safe/middleware/dist/document';
import { Html, Main } from 'next/document';
import React from 'react';
export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {