Skip to content

Instantly share code, notes, and snippets.

@jordanmaslyn
jordanmaslyn / middleware.ts
Last active April 24, 2023 16:12
Proxy your Next.JS sitemaps to Yoast sitemaps using the latest middleware!
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const url = request.nextUrl;
url.pathname = "/api/sitemap";
return NextResponse.rewrite(url);
}
@jordanmaslyn
jordanmaslyn / fetchWordPressRedirects.js
Created September 23, 2022 15:01
Fetch redirects at build-time from the Redirection plugin in WordPress, taken from JonnyTurbo in the Headless WP Discord - https://discord.com/channels/836253505944813629/836658991253553172/1012026516718223492
async function fetchWordPressRedirects() {
if(!process.env.WORDPRESS_USERNAME
|| !process.env.WORDPRESS_PASSWORD
|| !process.env.REDIRECTION_API_ENDPOINT) {
return [];
}
const base64UsernamePasswordToken = Buffer.from(
process.env.WORDPRESS_USERNAME + ':' + process.env.WORDPRESS_PASSWORD
).toString('base64');
@jordanmaslyn
jordanmaslyn / _middleware.ts
Created June 23, 2022 20:27
Force Basic Auth for your Next.JS site
import { NextRequest, NextResponse } from "next/server";
// eslint-disable-next-line import/prefer-default-export
export function middleware(req: NextRequest) {
const basicAuth = req.headers.get("authorization");
// if the SITE_USERNAME and SITE_PASSWORD env values exist
// then we will require authentication for all requests
if (process.env.SITE_USERNAME && process.env.SITE_PASSWORD) {
@jordanmaslyn
jordanmaslyn / _middleware.ts
Last active February 23, 2022 15:37
Use Next.js Middleware to standardize around one domain (e.g. force www, redirect from a platform subdomain to custom domain, etc.).
import { NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest) {
// PRIMARY_DOMAIN should be a full URL including protocol, e.g. https://www.google.com
const hasEnvVariable = !!process.env.PRIMARY_DOMAIN;
const isDevelopment = process.env.NODE_ENV === "development";
if (!hasEnvVariable) {
!isDevelopment &&
console.error(
@jordanmaslyn
jordanmaslyn / functions.php
Created January 6, 2022 23:33
Fix Yoast sitemaps when working with headless WP
<?php
/*
* Replacing domain for rest api requests from Gutenberg editor if youre using
* WP headless and WP_SITEURL & WP_HOME are not the same domain
* (has nothing to do with yoast)
*/
add_filter('rest_url', function($url) {
$url = str_replace(home_url(), site_url(), $url);
return $url;
@jordanmaslyn
jordanmaslyn / server.ts
Last active September 14, 2021 11:36
Use NextJS custom server for apex domain to www redirects
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const port = process.env.APP_PORT || 8080;
app.prepare().then(() => {
@jordanmaslyn
jordanmaslyn / sitemap.xml.ts
Created September 13, 2021 14:06
Example sitemap sync from WP to NextJS
import { GetServerSidePropsContext } from "next";
const defaultWpUrl = 'https://example.wpengine.com';
const localHost = 'localhost:3000';
const prodHost = 'example.com';
export default function Sitemap () {};
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
@jordanmaslyn
jordanmaslyn / app.html
Last active August 14, 2018 15:41 — forked from zewa666/app.html
Aurelia Store gist
<template>
<h1>Frameworks</h1>
<ul>
<li repeat.for="framework of state.frameworks">${framework}</li>
</ul>
</template>
@jordanmaslyn
jordanmaslyn / 0_reuse_code.js
Created March 6, 2017 22:23
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@jordanmaslyn
jordanmaslyn / functions.php
Created May 19, 2016 18:15
Filter a Wordpress URL
function custom_site_url( $url ) {
if( is_admin() ) {
return $url;
} // you probably don't want this in admin side
$replaced = array("buzzed", "blogspot.");
foreach ($replaced as $replace) {
if (strpos( parse_url($url, PHP_URL_HOST), $replace ) !== false) {
return str_replace($replaced, "", $url);