Skip to content

Instantly share code, notes, and snippets.

View z4nr34l's full-sized avatar
💻
QmVlcC1ib29wLCBib29wLWJlZXA=

Z4NR34L z4nr34l

💻
QmVlcC1ib29wLCBib29wLWJlZXA=
View GitHub Profile
@z4nr34l
z4nr34l / updater.sh
Last active February 27, 2025 10:10
Updater script for usage in cron to update any project on remote machine. Just add it as * * * * * /opt/projects/project/updater.sh
#!/bin/bash
# Project settings
PROJECT_DIR="/opt/projects" # CHANGE PATH
# Function to log messages with date and time
log_message() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1"
}
@z4nr34l
z4nr34l / middleware_rfc.md
Last active October 11, 2024 12:20
RFC: Next.js Dynamic Middleware

RFC: Next.js Dynamic Middleware

Summary

This RFC proposes a new approach to implementing middleware in Next.js, moving away from a single middleware.ts file to a more dynamic, route-specific implementation similar to the current handling of dynamic segments, pages, and layouts.

Motivation

The current middleware implementation in Next.js, while powerful, lacks the flexibility and modularity offered by other Next.js features like dynamic routing. This proposal aims to bring middleware in line with these other features, allowing for more granular control and easier management of middleware across a complex application.

@z4nr34l
z4nr34l / middleware.ts
Last active August 6, 2024 11:37
next-easy-middlewares multipath middleware chain
import { createMiddleware } from '@rescale/nemo';
import { internalMiddleware } from '@/app/(platform)/(personal)/_middleware';
const middlewares = {
'/(home|settings){/:path}?': internalMiddleware,
};
export const middleware = createMiddleware(
middlewares as never,
);
@z4nr34l
z4nr34l / updater.sh
Created May 31, 2024 08:14
Auto project update on dedicated server, just put it in cron :D
#!/bin/bash
# Project settings
PROJECT_DIR="<PROJECT DIR>"
# Function to log messages with date and time
log_message() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1"
}
@z4nr34l
z4nr34l / route.ts
Last active May 28, 2024 08:18
@rescale/cron example usage
import type { NextRequest, NextResponse } from 'next/server';
import { CronJob, type ErrorResponse } from '@rescale/cron';
// [...]
interface CombinedResponse {
updateSubscriptions: UpdateSubscriptionResponse[];
sendNewsletter: SendNewsletterResponse[];
}
@z4nr34l
z4nr34l / README.md
Created May 20, 2024 07:19
Tinybird github + vercel integration for nextjs projects.

As there is no officially supported integration that really works I've prepared a simple CI/CD pipelines to cover vercel's preview and production environments.

Instruction

1. Create 3 Tinybird workspaces:

  • <name>_dev,
  • <name>_stage,
  • <name>
@z4nr34l
z4nr34l / next-real-ip.ts
Created April 10, 2024 08:49
Simple function to fetch users real IP address in nextjs app on vercel including cloudflare proxy
/**
* The fallback IP address to use if the real IP address cannot be determined.
*/
const FALLBACK_IP_ADDRESS = '0.0.0.0';
/**
* Returns the real IP address of the client.
* @param request - The incoming request.
* @param cfProxy - Whether the client is behind a Cloudflare proxy.
* @returns The real IP address of the client.
@z4nr34l
z4nr34l / middleware.ts
Created January 7, 2024 19:03
Next.js default middleware with path matching
// example code source: https://github.com/vercel/next.js/blob/canary/examples/middleware/middleware.ts
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/about') {
return NextResponse.redirect(new URL('/redirected', request.url))
}
if (request.nextUrl.pathname === '/another') {
return NextResponse.rewrite(new URL('/rewrite', request.url))