How to disable site/deployment/auth temporarily? (Vercel)

I would like to have a "killswitch" to teporarily disable my site on Vercel. This is just a safety measure if anything goes wrong. What would be the easiest way to do so? The only thing I found is the "Password protection", but it is not allowed for hobby projects. It would be fine for me to have a way to disable the whole deployment as well. Additionally, it would be nice to be able to disable sign in/sign up with NextAuth.js temporarily. Is there something easy one can do? The session callback does not allow return false or null, so even returning false from the signIn callback will still allow users with session to get authenticated.
2 Replies
Tomsize
Tomsize2y ago
You could use the new Edge Config store from Vercel and redirect users to a maintenance page from the middleware. This way you could dynamically disable the whole website or some specific routes. The only cons is that you would have to read the store on every request matched by the middleware (which is really fast!), but it might increase the bill (you have 1 million free read with Pro, then it's 3$ per million) https://vercel.com/docs/concepts/edge-network/edge-config/get-started
// middleware.ts

import { NextResponse } from 'next/server';
import { get } from '@vercel/edge-config';

export const config = { matcher: "/" };

export async function middleware(request: NextRequest) {
const isMaintenance = get('maintenance');

if (isMaintenance) {
return NextResponse.redirect('/maintenance');
}

return NextResponse.next();
};
// middleware.ts

import { NextResponse } from 'next/server';
import { get } from '@vercel/edge-config';

export const config = { matcher: "/" };

export async function middleware(request: NextRequest) {
const isMaintenance = get('maintenance');

if (isMaintenance) {
return NextResponse.redirect('/maintenance');
}

return NextResponse.next();
};
Vercel Documentation
Getting Started with Edge Config
You can get started with reading data from Edge Config within minutes by following this quickstart guide.
bennettdev
bennettdevOP2y ago
Thanks, @Tomsize ! I don't want to have this middleware slow down every request, but it made me realize I can just keep this file in my app and comment it out completely and then uncomment the file as soon as I need it. I also chose a slightly different implementation that does not rely on the env vars/edge config:
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

const maintenancePath = '/maintenance'

export function middleware(request: NextRequest) {
// allowed paths, even in maintenance mode
if (
request.nextUrl.pathname.startsWith(maintenancePath) ||
request.nextUrl.pathname.startsWith('/_next/static') ||
request.nextUrl.pathname.startsWith('/favicon.ico')
) {
return NextResponse.next()
}

// all API requests respond with an error
if (request.nextUrl.pathname.startsWith('/api')) {
return NextResponse.error()
}

// all routes will be redirected to maintenance page
return NextResponse.redirect(new URL(maintenancePath, request.url))
}
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

const maintenancePath = '/maintenance'

export function middleware(request: NextRequest) {
// allowed paths, even in maintenance mode
if (
request.nextUrl.pathname.startsWith(maintenancePath) ||
request.nextUrl.pathname.startsWith('/_next/static') ||
request.nextUrl.pathname.startsWith('/favicon.ico')
) {
return NextResponse.next()
}

// all API requests respond with an error
if (request.nextUrl.pathname.startsWith('/api')) {
return NextResponse.error()
}

// all routes will be redirected to maintenance page
return NextResponse.redirect(new URL(maintenancePath, request.url))
}
Want results from more Discord servers?
Add your server